Write a program in java to accept a string from user and alter the string in such a way that the alphabet next to a vowel gets replaced by an equivalent opposite case alphabet.
import java.io.*;
class Satavisha
{
String str,str1="";
int i,len,flag;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
void takeString()throws Exception
{
System.out.println("Enter the sentence:");
str=br.readLine();
}
void show()
{
char ch;
len=str.length();
for(i=0;i< len-1;i++)
{
ch=str.charAt(i);
check(ch);
if(flag==1)
{
str1=str1+ch;
str1=str1+alter(str.charAt(i+1));
i++;
}
else
str1=str1+ch;
}
ch=str.charAt(i);
str1=str1+ch;
System.out.print("Modified sentence="+str1);
}
private void check(char ch)
{
flag=0;
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'I':
case 'i':
case 'o':
case 'O':
case 'u':
case 'U':
flag=1;
}
}
private char alter(char ch)
{
if(Character.isUpperCase(ch))
ch=Character.toLowerCase(ch);
else
ch=Character.toUpperCase(ch);
return ch;
}
public static void main(String args[])throws Exception
{
Satavisha ob=new Satavisha();
ob.takeString();
ob.show();
}
}
Technical analysis of the program
Four user defined functions are used in this program and they are void takeString(), void show(), private void check(char ch) and private char alter(char ch).
Function void takeString() takes the string from the user. void show () function extracts each character of the string and passes the character to function private void check (char ch) which checks whether it is a vowel or not. If the character is a vowel, another function private char alter(char ch) is called from function void show () to get the alternate case alphabet of the character next to vowel. All the characters, whether alternate case or original are appended to another string object. The loop in the void show () function extracts upto last but one characters from the string. This is because if the last character happens to be a vowel, there would be no next character to be altered. The last character is appended in the string below the loop body.
No comments:
Post a Comment