Miguel
Miguel

Reputation: 1177

Replacing multiple characters at a time

I'm trying to replace the string "abc" with certain patterns:

When 'a' replace with 'b', when 'b' replace with 'c', when 'c' replace with 'a'.

The code i have right now is :

String alp = "abc";
String alteredAlp = "";
char [] charAlp = alp.toLowerCase().toCharArray();

for (int i = 0; i < charAlp.length() - 1; i++)
{
    if(charAlp[i] == 'a')
        alteredAlp += 'b';
    else if(charAlp[i] == 'b')
        alteredAlp += 'c';
    else if(charAlp[i] == 'c')
        alteredAlp += 'a';
}

I tried using a 'replaceAll' and I was running into a couple issue after the first iteration.

Any ideas on how to make this better? Thanks in advance!

Upvotes: 1

Views: 445

Answers (4)

sushicutta
sushicutta

Reputation: 1536

A little more generic way:

The Alphabet must be defined without gaps!

But can also be "abcdefgh" :)

public class CharObfuscator {

    public static final String ALPHABET = "abc";

    public static String obfuscate(String obfuscateThis) {

        char bottomLevel = ALPHABET.charAt(0); // its an 'a' (int 97)
        char topLevel = ALPHABET.charAt(ALPHABET.length() - 1); // its an 'c' (int 99)

        StringBuilder stringBuilder = new StringBuilder();
        for (char character : obfuscateThis.toLowerCase().toCharArray()) {
            if ((character - bottomLevel + 1) % (topLevel - bottomLevel + 1) != 0) {
                stringBuilder.append(++character);
            } else {
                stringBuilder.append(bottomLevel);
            }
        }

        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        System.out.println(obfuscate("abccbaaabbcc"));
    }

}

Result:

bcaacbbbccaa

Upvotes: 1

Maroun
Maroun

Reputation: 95948

There is no such method as length() for a char[]. Change that to charAlp.length

Next, change charAlp.length - 1 to charAlp.length and your code will work.

Instead, you can do something like this: (with no additional String)

for (int i = 0; i < charAlp.length ; i++)
{
   if(charAlp[i] == 'a')
    charAlp[i]= 'b';
   else if(charAlp[i] == 'b')
    charAlp[i]= 'c';
   else if(charAlp[i] == 'c')
    charAlp[i]= 'a';
}
System.out.println(charAlp);

Now, you changed charAlp to be "bca" instead of "abc" (as you wanted)

Upvotes: 5

gashu
gashu

Reputation: 494

You can also use a StringBuilder for the same, as below:

public class CharReplace {

    public static void main(String[] args) {
        String alp = "abc";
        StringBuilder alteredAlp = new StringBuilder();

        for (int i = 0; i < alp.length(); i++) {
            switch(alp.charAt(i)) {
                case 'a':
                    alteredAlp.append('b');
                    break;
                case 'b':
                    alteredAlp.append('c');
                    break;
                case 'c':
                    alteredAlp.append('a');
                    break;
            }
        }

        System.out.println(alteredAlp.toString());
    }
}

Upvotes: 0

Achintya Jha
Achintya Jha

Reputation: 12843

public static void main(String[] args) {
    String alp = "abc";
    String alteredAlp = "";
    for(int i=0; i<alp.length(); i++){
        if(alp.charAt(i)=='a')
            alteredAlp=alteredAlp + 'b';
        else if(alp.charAt(i)=='b')
            alteredAlp = alteredAlp+'c';
        else if(alp.charAt(i)=='c')
            alteredAlp=alteredAlp+'a';

    }
    System.out.println(alteredAlp);
}

Try this : No need to create character array.

Output:

bca

Upvotes: 0

Related Questions