import java.io.*; import java.util.StringTokenizer; class StringOp { //read the task2 class name public static void main(String args[ ]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); //read the string System.out.println("Enter the string: "); String s=br.readLine(); StringTokenizer st=new StringTokenizer(s); System.out.println("1.Convert sentence to uppercase"); //giving possibilities System.out.println("2.Convert sentence to lowercase"); System.out.println("3.Display each word in the sentence"); System.out.println("4.Search for a word in the sentence"); System.out.println("5.Replace space with $ symbol in the sentence"); System.out.println("6.Quit the Program"); System.out.println("Enter your choice:"); //choice reading int x=Integer.parseInt(br.readLine()); switch(x){ case 1: System.out.println(s.toUpperCase()); //converting to uppercase break; case 2: System.out.println(s.toLowerCase()); //converting to lower case break; case 3: for(;st.hasMoreElements();){ //prints each token System.out.println(st.nextToken()); //System.out.println(); break; } case 4: System.out.println("Enter sub string"); //reads substring and compares String ss=br.readLine(); int intIndex = s.indexOf(ss); if(intIndex == - 1){ System.out.println("Hello not found"); }else{ System.out.println("Found Hello at index " + intIndex); } break; case 5: System.out.println( s=s.replace(" ", "$") ); //replace the string space with $ break; case 6: System.exit(0); //exit from the system default: System.out.println("tell me what to do.. i m quit"); } } }
OUTPUT:
Enter the string: Programming9 1.Convert sentence to uppercase 2.Convert sentence to lowercase 3.Display each word in the sentence 4.Search for a word in the sentence 5.Replace space with $ symbol in the sentence 6.Quit the Program Enter your choice: 1 PROGRAMMING9