Java Program For String Manipulation

//String Manipulation Program

import java.lang.*;
import java.io.*;
public class StringManipulation
{
   public static void main(String args[])   throws IOException
{
int choice,i,j = 0,num=0;
String str[] = {"Now", "is", "the", "time", "for", "all", "good", "men",
"to", "come", "to", "the", "aid", "of", "their", "country"};
num = str.length;
String searchString;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
boolean flag=true;
while(flag)
{
System.out.println("\n\n   Menu");
System.out.println("1. Sort and Display");
System.out.println("2. Display the List");
System.out.println("3. Enter the List of names");
System.out.println("4. Search");
System.out.println("5. Quit");
System.out.print("Enter your choice: ");
choice= Integer.parseInt(br.readLine());
switch(choice){
case 1:       for(j = 0; j < num; j++) {
 for(i = j + 1; i < num; i++) {
 if(str[i].compareTo(str[j]) < 0) {
 String t = str[j];
 str[j] = str[i];
 str[i] = t;
 }
 }
 }
 for(i=0; i<num; i++)
 System.out.println((i+1)+" "+str[i]);
 break;

case 2: for(i=0; i<num; i++)
 System.out.println((i+1)+" "+str[i]);
 break;

case 3:      System.out.print("How many names? ");
 num= Integer.parseInt(br.readLine());
 for(i=0;i<num;i++){
 System.out.println("Enter the name");
 str[i]=br.readLine();
 }
 break;

case 4: System.out.println("Enter the search name");
 searchString=br.readLine();
 for(i=0; i<num; i++)
 if(str[i].equals(searchString))
 System.out.println((i+1)+" "+str[i]);
 break;

case 5: flag=false;
 break;

default:System.out.println("Wrong Choice");
}
}
System.out.println("Program is over");
}
}
Bhanu Namikaze

Bhanu Namikaze is an Ethical Hacker, Security Analyst, Blogger, Web Developer and a Mechanical Engineer. He Enjoys writing articles, Blogging, Debugging Errors and Capture the Flags. Enjoy Learning; There is Nothing Like Absolute Defeat - Try and try until you Succeed.

No comments:

Post a Comment