Rearrange Word ISC Computer Science 2010 Theory
Input a word in uppercase and check for the position of the first occurring vowel and perform the following operations:
- Words that begin with a vowel are concatenated with "Y".
For example, EUROPE becomes EUROPEY. - Words that contain a vowel in-between should have the first part from the position of the vowel till the end, followed by the part of the string from beginning till the position before the vowel and is concatenated by "C".
For example, PROJECT becomes OJECTPRC. - Words that do not contain a vowel are concatenated with "N".
For example, SKY becomes SKYN.
Class name: Rearrange
Data members/instance variables:
txt: to store a word.
cxt: to store the rearranged word.
len: to store the length of the word.
Member functions:
Rearrange(): constructor to initialize the instance variables.
void readWord(): to accept the word input in uppercase.
void convert(): converts the word into its changed form and stores it in string cxt.
void display(): displays the original and the changed word.
import java.util.Scanner;
class Rearrange{
String txt;
String cxt;
int len;
public Rearrange(){
txt = new String();
cxt = new String();
len = 0;
}
public void readWord(){
Scanner in = new Scanner(System.in);
System.out.print("Enter the word: ");
txt = in.next().toUpperCase();
len = txt.length();
}
public void convert(){
int pos = -1;
loop:
for(int i = 0; i < len; i++){
char ch = txt.charAt(i);
switch(ch){
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
pos = i;
break loop;
}
}
if(pos == -1)
cxt = txt + "N";
else if(pos == 0)
cxt = txt + "Y";
else
cxt = txt.substring(pos) + txt.substring(0, pos) + "C";
}
public void display(){
System.out.println("Original word: " + txt);
System.out.println("Changed word: " + cxt);
}
public static void main(String args[]){
Rearrange obj = new Rearrange();
obj.readWord();
obj.convert();
obj.display();
}
}
class Rearrange{
String txt;
String cxt;
int len;
public Rearrange(){
txt = new String();
cxt = new String();
len = 0;
}
public void readWord(){
Scanner in = new Scanner(System.in);
System.out.print("Enter the word: ");
txt = in.next().toUpperCase();
len = txt.length();
}
public void convert(){
int pos = -1;
loop:
for(int i = 0; i < len; i++){
char ch = txt.charAt(i);
switch(ch){
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
pos = i;
break loop;
}
}
if(pos == -1)
cxt = txt + "N";
else if(pos == 0)
cxt = txt + "Y";
else
cxt = txt.substring(pos) + txt.substring(0, pos) + "C";
}
public void display(){
System.out.println("Original word: " + txt);
System.out.println("Changed word: " + cxt);
}
public static void main(String args[]){
Rearrange obj = new Rearrange();
obj.readWord();
obj.convert();
obj.display();
}
}
Comments
Post a Comment