-5

I have a question that is related to changing character in Java

For instance :

this is my string value

Sue sells sea shellS on the seashore!

How to change the capital letter of "S" to small letter and change the small letter of "s" to capital letter. this only change the letter "s" and "S" to uppercase and lowercase not other letter.

I've tried this:

String phrase= new String("Sue sells sea shells on the seashore!"); 
String r= ""; 
for(int z=0; z<phrase.length();++z)
{ 
    Character c = phrase.charAt(z); 
    if(Character.isLowerCase(c)) 
        r += Character.toUpperCase(c); 
    else if(Character.isUpperCase(c)) 
        r += Character.toLowerCase(c);
    else r += c; 
}
System.out.println(r);

but the problem is the code changes whole characters not only change letter "s" and "S". so, how to change it?

fabian
  • 80,457
  • 12
  • 86
  • 114
Agus Maloco
  • 11
  • 1
  • 1
  • 6
  • Please provide the code you already tried. We're not doing your homework. – MrTux Aug 17 '14 at 15:15
  • 1
    What have you tried? Did you check out the replace() method from the `String` API? – Keppil Aug 17 '14 at 15:15
  • String phrase= new String ("Sue sells sea shells on the seashore!"); String r= ""; for(int z=0; z – Agus Maloco Aug 17 '14 at 15:16
  • 4
    Read the javadoc of String. Search how to transform it to a char array. Use a loop to iterate over the char array. Read the documentation of StringBuilder to know how to build a new String by appending characters. – JB Nizet Aug 17 '14 at 15:21
  • 2
    @AgusMaloco post the code in your question, by editing it. Not in comments where the code is unreadable. How about `if (c == 's')`? – JB Nizet Aug 17 '14 at 15:22
  • I have no idea at all. this is my first time using java. – Agus Maloco Aug 17 '14 at 15:33
  • @Agus: JB Nizet has given you all you should need. If you don't understand you probably need to read a few Java tutorials/books before you start writing programs. – Keppil Aug 17 '14 at 15:35
  • Thank you Keppil.. Really appreciate your help. it's really useful. – Agus Maloco Aug 17 '14 at 15:42

1 Answers1

0
String s1 = "Sue sells sea shellS on the seashore";
String s2 = "";
String s3 = "";

for(int i = 0; i < s1.length(); i++){
    if(s1.charAt(i) == 's') {
        s2 += 'S';
        s3 += 's';
    }
    else if(s1.charAt(i) == 'S') s2 += 's';
    else {
        s2 += s1.charAt(i);
        s3 += s1.charAt(i);
    }
}

System.out.println(s2);

Edit: Code updated after your commend

This will print:

"sue SellS Sea Shells on the SeaShore"

gkrls
  • 2,618
  • 2
  • 15
  • 29
  • Can I ask one more question. This question is the continuous question from the code above. how if I want to remove the capital letter "S" and print it to the console?. The code above remain the same, but there is another system.out.println that prints out only sentences without capital letter "S".For example, sue ell ea hells on the eahore". If you don't mind, please help me again, I'm still on learning process. Thank you – Agus Maloco Aug 17 '14 at 16:15
  • Updated my answer. Now `s3` holds `s1` without the capita `S` – gkrls Aug 17 '14 at 16:21
  • @Agus Maloco If this helped you please accept the answer – gkrls Aug 19 '14 at 21:10