This is a menu driven program with string manipulation.
import java.io.*;
class String1
{
String s1,s2;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
private void takeName() throws IOException
{
System.out.print("\nEnter Name: ");
s1=br.readLine();
}
private void wordCaseName()
{
char ch;
s2="";
int i,len;
len=s1.length();
for(i=0;i<len;i++)
{
ch=s1.charAt(i);
if(i==0)
s2=s2+Character.toUpperCase(ch);
else if(ch==' ')
{
s2=s2+" ";
s2=s2+Character.toUpperCase(s1.charAt(i+1));
i++;
}
else
s2=s2+Character.toLowerCase(ch);
}
s1=s2;
System.out.println("\nName in Sentence Case: "+s1);
}
private void shortName()
{
char ch;
s2="";
int i,len,sp=0;
len=s1.length();
for(i=0;i<len;i++)
{
if(s1.charAt(i)==' ')
sp++;
}
for(i=0;i<len;i++)
{
ch=s1.charAt(i);
if(i==0)
s2=s2+Character.toUpperCase(ch)+".";
else if(ch==' ' && sp!=1)
{
s2=s2+" ";
s2=s2+Character.toUpperCase(s1.charAt(i+1))+".";
i++;
sp--;
}
else if(ch==' ' && sp==1)
{
s2=s2+" ";
s2=s2+Character.toUpperCase(s1.charAt(i+1));
i++;
sp--;
}
else if(sp==0)
s2=s2+Character.toLowerCase(ch);
}
System.out.println("\nName in Short Form: "+s2);
}
private void replaceWord() throws IOException
{
String str1,str2,s3,s4;
int i;
s2="";
s3=s1.trim()+" ";
System.out.print("\nEnter the word which is to be replaced: ");
str1=br.readLine();
System.out.print("\nEnter the word which will replace: ");
str2=br.readLine();
while(true)
{
i=s3.indexOf(' ');
if(i<0)
break;
s4=s3.substring(0,i);
s3=s3.substring(i+1);
if(s4.equalsIgnoreCase(str1))
s2=s2+" "+str2;
else
s2=s2+" "+s4;
}
s2=s2.trim();
System.out.println("\nModified Name: "+s2);
}
private void surnameFirst()
{
String s3,s4;
int i;
s2="";
s3=s1;
i=s3.lastIndexOf(' ');
s2=s2+s3.substring(i+1);
s3=s3.substring(0,i)+ " ";
while(true)
{
i=s3.indexOf(' ');
if(i<0)
break;
s2=s2+" "+s3.substring(0,i);;
s3=s3.substring(i+1);
}
s2=s2.trim();
System.out.println("\nModified Name With Surname First: "+s2);
}
public static void main(String args[])throws IOException
{
int choice=1;
boolean bool=true;
String parts1, parts2, parts3;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String1 ob=new String1();
ob.takeName();
while(choice!=0)
{
System.out.println("Enter 1 to view Name In Sentence Case\n 2 to view Name in Short Form\n 3 To replace a Word with Another\n 4 To Display Surname First and Then Name\n 0 For Exit: ");
choice=Integer.parseInt(br.readLine());
switch(choice)
{
case 1:
ob.wordCaseName();
break;
case 2:
ob.shortName();
break;
case 3:
ob.replaceWord();
break;
case 4:
ob.surnameFirst();
break;
case 0:
System.out.print("\nEnd of Program.");
break;
default:
System.out.print("\nWrong Choice.");
}
}
}
}
Sample Input Output
Enter Name: amal KANTI maZUMder
Enter 1 to view Name In Sentence Case
2 to view Name in Short Form
3 To replace a Word with Another
4 To Display Surname First and Then Name
0 For Exit:
1
Name in Sentence Case: Amal Kanti Mazumder
Enter 1 to view Name In Sentence Case
2 to view Name in Short Form
3 To replace a Word with Another
4 To Display Surname First and Then Name
0 For Exit:
2
Name in Short Form: A. K. Mazumder
Enter 1 to view Name In Sentence Case
2 to view Name in Short Form
3 To replace a Word with Another
4 To Display Surname First and Then Name
0 For Exit:
3
Enter the word which is to be replaced: Kanti
Enter the word which will replace: Kumar
Modified Name: Amal Kumar Mazumder
Enter 1 to view Name In Sentence Case
2 to view Name in Short Form
3 To replace a Word with Another
4 To Display Surname First and Then Name
0 For Exit:
4
Modified Name With Surname First: Mazumder Amal Kanti
Enter 1 to view Name In Sentence Case
2 to view Name in Short Form
3 To replace a Word with Another
4 To Display Surname First and Then Name
0 For Exit:
0
No comments:
Post a Comment