Sana
Sana

Reputation: 23

How to make alternate characters in a string to uppercase?

I wrote the following code but similar characters are always in the same case. What's wrong in this code and How can this problem be solved??

private void genBTActionPerformed(java.awt.event.ActionEvent evt) {                                      
    String str = new String(strTF.getText());
    int n = str.length();
    char ch;
    int i;
    for(i = 0; i < n; i++) {
        if(i % 2 == 0) {
            ch = Character.toLowerCase(str.charAt(i));
            str = str.replace(str.charAt(i), ch);
        } else {
            ch = Character.toUpperCase(str.charAt(i));
            str = str.replace(str.charAt(i), ch);
        }
    }
    jumTF.setText(str);
}   

Upvotes: 1

Views: 11965

Answers (3)

Neeraj Jain
Neeraj Jain

Reputation: 7720

Since String are immutable in java , you can use StringBuilder or StringBuffer to solve this problem

StringBuilder str=new StringBuilder(inputString);

You can use your own logic just with slight change instead of using

str = str.replace(str.charAt(i), ch);//since  it replaces in whole string

Use

str.setCharAt(i,ch);

So your final Program looks like this :

for(i = 0; i < n; i++) {
        if(i % 2 == 0) {
            ch = Character.toLowerCase(str.charAt(i));
            str.setCharAt(i,ch);
        } else {
            ch = Character.toUpperCase(str.charAt(i));
            str.setCharAt(i,ch);
        }
    }

Suppose InputString is : stackoverflow then output is : sTaCkOvErFlOw

Upvotes: 0

user3545242
user3545242

Reputation: 71

In your program you were using replace() which replaces characters/CharSequences in the whole input what you need to do is

  • Put the string into an array.
  • Iterate over said array.
  • convert that array back into string

    private void genBTActionPerformed(java.awt.event.ActionEvent evt) {

    String str = new String(strTF.getText());
    char [] chr= str.toCharArray();
    int n = chr.length;
    char ch;
    int i;
    for(i = 0; i < n; i++) {
        if(i % 2 == 0) {
            ch = Character.toLowerCase(chr[i]);
           chr[i]=ch;
        } else {
            ch = Character.toUpperCase(chr[i]);
         chr[i]=ch;
        }
    }
    jumTF.setText(new String(chr)); }
    

hope this will help you :)

Upvotes: 0

fge
fge

Reputation: 121790

Unlike what its name says, .replace() replaces characters/CharSequences in the whole input. The difference with .replaceAll() is that it takes literals as arguments and not regexes/regex replacements strings (and that it has an overload taking two chars as arguments). That is the second worst misnamed method of the String class after matches().

Moreover you create a new String on each character you replace, so you have n+1 strings for a n character long string. Do it like this instead:

final char[] chars = str.toCharArray();

final int len = chars.length;

char c;

for (int i = 0; i < len; i++) {
    c = chars[i];

    chars[i] = i % 2 == 0
        ? Character.toLowerCase(c)
        : Character.toUpperCase(c);
}

jumTF.setText(new String(chars));

Upvotes: 6

Related Questions