Sentence case changer

sravan953 0 Tallied Votes 248 Views Share

A java program which accepts a string from a user, and gives various options to change it case.

Options include:

  1. UPPER CASE
  2. lower case
  3. Sentence case
  4. Title Case
  5. tOGGLE cASE
/* Importing Java IO class */
import java.io.*;
class sentence_cases
{
static void toggle()throws IOException
    {
        InputStreamReader in=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(in);
        System.out.println("Enter a sentence: ");
        String s=br.readLine();
        /* Trimming the sentence incase the user enters unwanted whitespaces. */
        s.trim();
        System.out.print("What do you want to do?\n1)UPPERCASE\n2)lowercase\n3)Sentence case\n4)Title Case\n5)tOGGLE cASE\n6)Enter another sentence\n7)Quit\n\nOption: " );
        String s1=br.readLine();
        /* Parsing the choice the user entered to an integer. */
        int x=Integer.parseInt(s1);
        while (x!=7)
            {
                if(x==1)
                {
                    System.out.println(s.toUpperCase());
                }
                else if(x==2)
                {
                    System.out.println(s.toLowerCase());
                }
                else if(x==3)
                {
                    String s2="";
                    s2+=Character.toUpperCase(s.charAt(0));
                    for(int i=1;i<s.length();i++)
                    {
                        if(Character.isUpperCase(s.charAt(i)))
                        {
                            s2+=Character.toLowerCase(s.charAt(i));
                        }
                        else
                        {
                            s2+=s.charAt(i);
                        }
                    }
                    System.out.println(s2);
                }
                else if(x==4)
                {
                    String s2="";
                    s2+=Character.toUpperCase(s.charAt(0));
                    for(int i=1;i<s.length();i++)
                    {
                        if(s.charAt(i)==' ')
                        {
                            s2+=" ";
                            s2+=Character.toUpperCase(s.charAt(i+1));
                            i++;
                        }
                        else
                        {
                            s2+=s.charAt(i);
                        }
                    }
                    System.out.println(s2);
                }
                else if(x==5)
                {
                    String s2="";
                    for(int i=0;i<s.length();i++)
                    {
                        if(Character.isLowerCase(s.charAt(i)))
                        {
                            s2+=Character.toUpperCase(s.charAt(i));
                        }
                        else if(Character.isUpperCase(s.charAt(i)))
                        {
                            s2+=Character.toLowerCase(s.charAt(i));
                        }
                        else if(s.charAt(i)==' ')
                        {
                            s2+=s.charAt(i);
                        }
                    }
                    System.out.println(s2);
                }
                else if(x==6)
                {
                    System.out.println("\nEnter another sentence:");
                    s=br.readLine();
                    System.out.print("\nWhat do you want to do?\n1)UPPERCASE\n2)lowercase\n3)Sentence case\n4)Title Case\n5)tOGGLE cASE\n6)Enter another sentence\n7)Quit\n\nOption: ");
                    s1=br.readLine();
                    x=Integer.parseInt(s1);
                    continue;
                }                                                                    
                System.out.print("\nWhat do you want to do?\n1)UPPERCASE\n2)lowercase\n3)Sentence case\n4)Title Case\n5)tOGGLE cASE\n6)Enter another sentence\n7)Quit\n\nOption: ");
                s1=br.readLine();
                x=Integer.parseInt(s1);
            }
        }
    }
smartbloggerz 0 Newbie Poster

Looks like it will work..Will give it a try now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.