Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert Lower case letters to upper case letters & and upper case letters to lower case letters

Alternately display any text that is typed in the textbox

//     in either Capital or lowercase depending on the original
//     letter changed.  For example:  CoMpUtEr will convert to
//     cOmPuTeR and vice versa.
    Switch.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e )

            String characters = (SecondTextField.getText()); //String to read the user input
            int length = characters.length();  //change the string characters to length

         for(int i = 0; i < length; i++)  //to check the characters of string..
         {             
            char character = characters.charAt(i);          

            if(Character.isUpperCase(character)) 
            {
                SecondTextField.setText("" + characters.toLowerCase());

            }
            else  if(Character.isLowerCase(character))
            {
                 SecondTextField.setText("" + characters.toUpperCase()); //problem is here, how can i track the character which i already change above, means lowerCase**
                }               
         }}     
    });
like image 963
akki0996 Avatar asked Feb 20 '13 03:02

akki0996


7 Answers

If you look at characters a-z, you'll see that all of them have the 6th bit is set to 1. Where in A-Z 6th bit is not set.

A = 1000001    a = 1100001
B = 1000010    b = 1100010
C = 1000011    c = 1100011
D = 1000100    d = 1100100     
...
Z = 1011010    z = 1111010

So all we need to do is to iterate through each character from a given string and then do XOR(^) with 32. In this way, the 6th bit can swap.

Look at the below code for simply changing the string case without using any if-else conditions.

public final class ChangeStringCase {
    public static void main(String[] args) {
        String str = "Hello World";
        for (int i = 0; i < str.length(); i++) {
            char ans = (char)(str.charAt(i) ^ 32);
            System.out.print(ans); // Final Output: hELLO wORLD
        }
    }
}

Time Complexity: O(N) where N = Length of the string.

Space Complexity: O(1)

like image 82
Yachareni Krishnakanth Avatar answered Oct 07 '22 15:10

Yachareni Krishnakanth


StringBuilder b = new StringBuilder();

Scanner s = new Scanner(System.in);
String n = s.nextLine();

for(int i = 0; i < n.length(); i++) {
    char c = n.charAt(i);

    if(Character.isLowerCase(c) == true) {
        b.append(String.valueOf(c).toUpperCase());
    }
    else {
        b.append(String.valueOf(c).toLowerCase());
    }
}

System.out.println(b);
like image 41
Ebin Thomas Avatar answered Oct 07 '22 14:10

Ebin Thomas


Methods description:

*toLowerCase()* Returns a new string with all characters converted to lowercase.

*toUpperCase()* Returns a new string with all characters converted to uppercase.

For example:

"Welcome".toLowerCase() returns a new string, welcome

"Welcome".toUpperCase() returns a new string, WELCOME

like image 40
Gautam Sarkar Avatar answered Oct 07 '22 15:10

Gautam Sarkar


setText is changing the text content to exactly what you give it, not appending it.

Convert the String from the field first, then apply it directly...

String value = "This Is A Test";
StringBuilder sb = new StringBuilder(value);
for (int index = 0; index < sb.length(); index++) {
    char c = sb.charAt(index);
    if (Character.isLowerCase(c)) {
        sb.setCharAt(index, Character.toUpperCase(c));
    } else {
        sb.setCharAt(index, Character.toLowerCase(c));
    }
}

SecondTextField.setText(sb.toString());
like image 16
MadProgrammer Avatar answered Oct 07 '22 22:10

MadProgrammer


You don't have to track whether you've already changed the character from upper to lower. Your code is already doing that since it's basically:

1   for each character x:
2       if x is uppercase:
3           convert x to lowercase
4       else:
5           if x is lowercase:
6                convert x to uppercase.

The fact that you have that else in there (on line 4) means that a character that was initially uppercase will never be checked in the second if statement (on line 5).

Example, start with A. Because that's uppercase, it will be converted to lowercase on line 3 and then you'll go back up to line 1 for the next character.

If you start with z, the if on line 2 will send you directly to line 5 where it will be converted to uppercase. Anything that's neither upper nor lowercase will fail both if statements and therefore remain untouched.

like image 5
paxdiablo Avatar answered Oct 07 '22 22:10

paxdiablo


You can use StringUtils.swapCase() from org.apache.commons

like image 4
Pshemo Avatar answered Oct 07 '22 23:10

Pshemo


This is a better method :-

void main()throws IOException
{
    System.out.println("Enter sentence");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String str = br.readLine();
    String sentence = "";
    for(int i=0;i<str.length();i++)
    {
        if(Character.isUpperCase(str.charAt(i))==true)
        {
            char ch2= (char)(str.charAt(i)+32);
            sentence = sentence + ch2;
        }
        else if(Character.isLowerCase(str.charAt(i))==true)
        {
            char ch2= (char)(str.charAt(i)-32);
            sentence = sentence + ch2;
        }
        else
        sentence= sentence + str.charAt(i);

    }
    System.out.println(sentence);
}
like image 4
Shubham Avatar answered Oct 08 '22 00:10

Shubham