adding characters in an array at specific points - java

I'm working on a Caesar cipher and I've gotten the majority of the code to work as planned.
the code is supposed to
remove all special characters and spaces
bring everything to uppercase
add spaces at an inputted interval any additional leftover spaces with x's
so for example if i were to type
plaintext: Hi im Doug
key: 1
buffer: 3
my output should be
IJJ NEP VHX
now I've gotten everything to work but the buffer part
this is my code in its entirety
import java.util.Scanner;
import java.lang.String;
public class Main {
public static void main(String[] args) {
System.out.print("Enter plaintext: ");
Scanner pTextInp = new Scanner(System.in);
String pText = pTextInp.nextLine();
System.out.print("Enter key value: ");
Scanner kInp = new Scanner(System.in);
int key = kInp.nextInt();
pText = normalizeText(pText);
pText = caesarify(pText, key);
System.out.print("Enter desired grouping number: ");
Scanner grpInp = new Scanner(System.in);
int grpInt = grpInp.nextInt();
pText = groupify(grpInt, pText);
System.out.println(pText);
}
// CONVERT STRING TO A CHAR ARRAY
public static char[] sArray(String s) {
int sLen = s.length();
char[] sChar = new char[sLen + 1];
for (int i = 0; i < sLen; i++){
sChar[i] = s.charAt(i);
}
return sChar;
}
public static String caesarify(String s, int k) {
int sLen = s.length();
char cText[] = sArray(s);
for (int i = 0; i < sLen; i++){
int j = cText[i] - 65;
int l = (((j + k) % 26) + 65);
cText[i] = (char) l;
}
s = new String(cText);
return s;
}
// normalizes text (removes all spaces and special characters)
public static String normalizeText(String s) {
int sLen = s.length();
char[] t1 = s.toCharArray();
for (int i = 0; i < sLen; i++ ){
if(t1[i] < 'A' || t1[i] > 'z' || (t1[i] > 'Z' && t1[i] < 'a')) {
t1[i] = ' ';
}
else{
t1[i] = s.charAt(i);
}
}
String t = new String(t1);
t = t.replaceAll(" ", "" );
t = t.toUpperCase();
return t;
}
public static String groupify(int i , String s){
int sLen = s.length();
char[] t = new char[sLen];
for (int j = 0; j < s.length(); j++){
t[j] = s.charAt(j);
if ( j % i == 0) {
t[j] = ' ';
sLen++;
}
}
s = new String(t);
return s;
}
and this is the section in particular that i think is the issue
public static String groupify(int i , String s){
int sLen = s.length();
char[] t = new char[sLen];
for (int j = 0; j < s.length(); j++){
t[j] = s.charAt(j);
if ( j % i == 0) {
t[j] = ' ';
sLen++;
}
}
s = new String(t);
return s;
}
with this this if i input
Hi Im Doug
I get
JJ EP H
as output
Thanks a bunch

Currently your code t[i] += ' ' is adding the space character's value to value in the array. That's not what you want. Rather you want to be storing a space in next position. I also suggest that you use better names for your variable - single character variables should generally only be used for indexes.
int pos = 0;
for (int j = 0; j < input.length(); j++)
result[pos++] = input.charAt(j);
if ( j % group == 0) {
result[pos++] = ' ';
}
}

For the code You have highlighted you are missing curly brackets after the for Loop. Try to define j outside the for-loop and assign the value 0 to j in the for-loop. It would be a better practice if you Store the size of your new Char-Array in a separate variable as it is easier to read when finding bugs in the code.

Related

How to overwrite String in loop

public static String decrypt(final String encryptedText, final int n) {
char [] chars;
String s = encryptedText;
String s1 = "";
int buffer = 0;
int buffer2 = 1;
for (int j = 0; j < n; j++) {
if (j < 1){
chars = s.toCharArray();
}else{
chars = s1.toCharArray();
}
char [] charsUpdate = new char[chars.length];
for (int i = chars.length / 2; i < chars.length; i++) {
if (buffer % 2 == 0 && buffer <= charsUpdate.length){
charsUpdate[buffer] = chars[i];
}
buffer += 2;
}
for (int i = 0; i < chars.length / 2 ; i++) {
if (buffer2 % 2 != 0 && buffer2 < charsUpdate.length){
charsUpdate[buffer2] = chars[i];
}
buffer2 += 2;
}
s = "";
s1 = "";
for (int i = 0; i < charsUpdate.length; i++) {
s = s + charsUpdate[i];
}
s1 = s;
}
return s;
Hi, community. I have some problem here. I try to resolve some task and have stuck in moment when I need to overwrite my String in loop. I need give my old String in start of loop and overwrite this String in end of loop and give new String to start of loop, something like that, but I can't do this, because after first iteration in the start of loop I can see my empty String. Sorry for my shit-code and bad english :)
There are some other issues:
ArrayIndexOutOfBoundsException because the condition buffer <= charsUpdate.length is incorrect
Variables buffer and buffer2 are not reset at the end of the loop, so after recreation of charUpdate array during the 2nd and the following iteration the symbols are not copied from chars to charUpdate
Updated and refactored code may look as follows:
public static String decrypt(final String encryptedText, final int n) {
int len = encryptedText.length();
int half = len / 2;
String s = encryptedText;
for (int j = 0; j < n; j++) {
char[] chars = s.toCharArray();
char[] charsUpdate = new char[len];
for (int i = half, even = 0; i < len && even < len; i++, even += 2) {
charsUpdate[even] = chars[i];
}
for (int i = 0, odd = 1; i < half && odd < len; i++, odd += 2) {
charsUpdate[odd] = chars[i];
}
s = new String(charsUpdate);
}
return s;
}
But I am not sure that it produces valid results.
A simple test shows the following output:
for (int i = 1; i < 5; i++) {
System.out.println("abcdefgh (" + i + ") -> " + decrypt("abcdefgh", i));
}
output:
abcdefgh (1) -> eafbgchd
abcdefgh (2) -> gecahfdb
abcdefgh (3) -> hgfedcba
abcdefgh (4) -> dhcgbfae
----
// for odd length of the input string
abcdefg (1) -> daebfcg
abcdefg (2) -> bdfaceg
abcdefg (3) -> abcdefg
abcdefg (4) -> daebfcg

How to compute the frequency of occurrence of '+' and '++' in Java?

I want to calculate the frequency of the occurrence of all the operators from an input text file. The file contains the operators + and ++. How can I distinguish their respective frequency, as my program treats ++ as 2 distinct + operators rather than 1 ++?
Here is my code (input7.txt is a test file):
public static void main(String[] args) throws IOException {
String string = new String(Files.readAllBytes(Paths.get("input7.txt"))); //String to be counted
int frequencyArray[] = new int[string.length()];
int frequencyArray2[] = new int[string.length()];
char stringArray[] = string.toCharArray(); //Array of characters
int i, j;
//Count characters
for (i = 0; i < string.length(); i++) {
frequencyArray[i] = 1;
//frequencyArray2[i] = 1;
for(j = i + 1; j < string.length(); j++)
{
if(stringArray[i] == stringArray[j])
{
frequencyArray[i]++;
stringArray[j] = '0'; //To avoid revisiting a character
}
}
}
//Display results
System.out.println("Characters and their corresponding frequencies");
for (i = 0; i < frequencyArray.length; i++) {
if (stringArray[i] != ' ' && stringArray[i] != '0') {
System.out.println(stringArray[i] +"-" + frequencyArray[i]);
}
}
}
This works for me:
String s = "sdfasd++ sdfadsf+asdf sdf++sadfasdf++sadfsdf+asdfasdf++";
// create Set with all distinct characters
Set<Character> chars = new HashSet<Character>();
for (int i = 0; i < s.length(); i++) {
chars.add(s.charAt(i));
}
// count distinct characters and put Results in HashMap
Map<Character, Integer> counts = new HashMap<Character, Integer>();
for (Character c : chars) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == c)
count++;
}
counts.put(c, count);
}
// Count double-Character-Operators like this
int countPlusPlus = 0;
for (int i = 0; i < s.length() - 1; i++) {
if (s.substring(i, i + 2).equals("++"))
countPlusPlus++;
}
// Calculate totals like this
int singleplusTotal = counts.get('+');
System.out.println("Single Plus total" + singleplusTotal);
System.out.println("Double Plus total" + countPlusPlus);
System.out.println("Only single Plus" + (singleplusTotal - countPlusPlus * 2));

java, how to find the amount of letter in a string without repeats

I'm trying to build a function, that gets a string of letters, and prints the amount of each letter in the string.
for example:
input: String = "aaabbaccxyxyx"
output: 4a2b2c3x2y
This is what I've come up with:
public class Q1 {
public static String numLetters(String s){
String end = new String();
int counter = 0;
char c,d;
for(int i=0; i<s.length();i++){
c = s.charAt(i);
for(int j=0; j<s.length();j++){
d = s.charAt(j);
if(c == d){
counter++;
}
}
end = end + counter+c;
counter = 0;
}
return end;
}
but, this is the output: 4a4a4a2b2b4a2c2c3x2y3x2y3x
A lot of repeats..
Any help how to make it right?
Keep in mind, the function needs to return a string, not just prints it out.
Thanks! =)
I would make an int array to keep the count of each letter in in the string. Because there are 26 letters, the length of the array should be 26:
public static String numLetters(String s) {
int[] count = new int[26];
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
count[(int)(c - 'a')]++;
}
String ans = "";
for (int i = 0; i < 26; i++) {
if (count[i] != 0) {
ans += String.valueOf(count[i]) + (char)(i + 'a');
}
}
return ans;
}
A straightforward variant could look like this:
public static String countChars(String arg) {
String res = "";
boolean[] counted = new boolean[arg.length()];
for (int i = 0; i < counted.length; i++) {
if (!counted[i]) {
char c = arg.charAt(i);
int counter = 1;
for (int j = i + 1; j < counted.length; j++) {
if (arg.charAt(j) == c) {
counter++;
counted[j] = true;
}
}
res += counter + "" + c;
}
}
return res;
}
If you want to keep your original structure, I suggest using a StringBuilder so that you can delete characters that you have already seen. In case you delete a character, you have to adjust your indexes i and j.
public static String numLetters(String str){
StringBuilder s = new StringBuilder(s);
String end = new String();
int counter = 0;
char c,d;
for(int i=0; i<s.length();i++){
c = s.charAt(i);
for(int j=0; j<s.length();j++){
d = s.charAt(j);
if(c == d){
s.deleteCharAt(j);
if (i >= j) i--;
j--;
counter++;
}
}
end = end + counter+c;
counter = 0;
}
return end;
}
Try this:
int count = StringUtils.countMatches("a.b.c.d", ".");

How to find number of distinct characters in a string

I have to count the number of distinct characters alphabet in the string, so in this case the count will be - 3 (d, k and s).
Given the following String:
String input;
input = "223d323dk2388s";
count(input);
My Code :
public int count(String string) {
int count=0;
String character = string;
ArrayList<Character> distinct= new ArrayList<>();
for(int i=0;i<character.length();i++){
char temp = character.charAt(i);
int j=0;
for( j=0;j<distinct.size();j++){
if(temp!=distinct.get(j)){
break;
}
}
if(!(j==distinct.size())){
distinct.add(temp);
}
}
return distinct.size();
}
Output : 3
Are there any native libraries which return me the number of characters present in that string ?
With java 8 it is much easy. You could use something like this
return string.chars().distinct().count();
One way is to maintain an array and then fill it up and get the total. This checks for all characters including special characters and numbers.
boolean []chars = new boolean[256];
String s = "223d323dk2388s";
for (int i = 0; i < s.length(); ++i) {
chars[s.charAt(i)] = true;
}
int count = 0;
for (int i = 0; i < chars.length; ++i) {
if (chars[i]) count++;
}
System.out.println(count);
Here's an alternative if you want to calculate the count only of letters, not including numbers and special symbols. Note that capital and small alphabets are different.
boolean []chars = new boolean[56];
String s = "223d323dk2388szZ";
for (int i = 0; i < s.length(); ++i) {
char ch = s.charAt(i);
if (ch >=65 && ch <= 90) {
chars[ch - 'A'] = true;
} else if (ch >= 97 && ch <= 122) {
chars[ch - 'a' + 26] = true; //If you don't want to differentiate capital and small differently, don't add 26
}
}
int count = 0;
for (int i = 0; i < chars.length; ++i) {
if (chars[i]) count++;
}
System.out.println(count);
Another way of doing it is using a Set.
String s = "223d323dk2388s";
Set<Character> set = new HashSet<Character>();
for (int i = 0; i < s.length(); ++i) {
set.add(s.charAt(i));
}
System.out.println(set.size());
If you don't want numbers and special symbols.
String s = "223d323dk2388s";
Set<Character> set = new HashSet<Character>();
for (int i = 0; i < s.length(); ++i){
char ch = s.charAt(i);
if ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122))
set.add(s.charAt(i));
}
System.out.println(set.size());
String s="InputString";
String p="";
char ch[]=s.toCharArray();
for(int i=0;i<s.length();i++)
{
for(int j=i+1;j<s.length();j++)
{
if(ch[i]==ch[j])
{
ch[j]=' ';
}
}
p=p+ch[i];
p = p.replaceAll("\\s","");
}
System.out.println(p.length());
To those coming here looking for a solution in Kotlin:
val countOfDistinctCharacters = string.toCharArray().distinct().size

Getting each letter in a String only once

I'm writing a Java program for Horspool's algorithm, and am having a bit of trouble. I'm trying to create an array of chars that will hold each letter in a string, but I don't want duplicates of the letters. Right now this is my code:
public static void main(String[] args)
{
Scanner scanIn = new Scanner (System.in);
int count = 0;
int count2 = 0;
int inc = 0;
//The text to search for the phrase in
String t = "";
//The phrase/pattern to search for
String p = "";
System.out.println(" ");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("Harspool's Algorithm: ");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println(" ");
System.out.println("Please enter the full text: ");
t = scanIn.nextLine();
System.out.println("Please enter the pattern to search for: ");
p = scanIn.nextLine();
char[] text = new char[t.length()];
char[] pattern = new char[p.length()];
char[] alphabet = new char[t.length()];
for (int i = 0; i < alphabet.length; i++)
{
alphabet[i] = ' ';
}
for (int i = 0; i < text.length; i++)
{
text[i] = t.charAt(i);
}
for (int i = 0; i < pattern.length; i++)
{
pattern[i] = p.charAt(i);
}
while (inc < text.length)
{
for (int j = 0; j < text.length; j++)
{
if (text[inc] != alphabet[j])
{
count++;
}
if (count == p.length() - 1 && count2 < text.length)
{
alphabet[count2] = text[inc];
count2++;
count = 0;
inc++;
}
}
}
for (int i = 0; i < alphabet.length; i++)
{
System.out.print(alphabet[i]);
}
}
I believe the problem is in the while loop, but I can't figure out what exactly is going wrong. Right now, it will print out the entire string passed in, when it should be printing each letter only once. Could someone please help?
Instead of counting the occurrences of each character, Use Set<Character>. A set contains unique elements and so you will not have duplicates that way.
You can also convert a Set to an array by doing mySet.toArray(new String[mySet.size()]); or just mySet.toArray(new String[0]);
Your code is not easy to read. You might consider using the following algorithm instead.
int ccount[256];
int ii;
for(ii=0;ii<256;ii++) ccount[ii]=0;
for (ii = 0; ii < text.length; ii++)
{
ccount[t.charAt(i)%256]++;
}
for (ii = 0; ii<256; ii++) {
if(ccount[ii]>0) System.out.printf("%c", ii);
}
EDIT - made sure ccount was initialized, and captured characters outside of range 0-255 with % operator.

Categories

Resources