Question
Input a word in uppercase and check for the position of the first occurring vowel and perform the
following operation.
(i)Words that begin with a vowel are concatenated with “Y”.
For example:- EUROPE becomes EUROPEY.
(ii) Words that contains a vowel in-between should have the first part from the position of the vowel till end , followed by the part of the string from beginning till position of the vowel and is concatenated by “C”.
For example – PROJECT becomes OJECTPRC.
(iii) Words which do not contain a vowel are concatenated with “N”.
For example – SKY becomes SKYN.
Design a class Rearrange using the description of the data members and member functions given below:
Classname: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
Specify the class Rearrange giving the details of the constructor( ), void readword( ),void convert() and void display() . Define a main() function to create an object and call the function accordingly to enable the task.
Input:
ENTER THE SIZE OF ARRAY:10
ENTER THE ELEMENT IN THE ARRAY:
1 2 3 4 5 6 7 8 9 10
ARRAY AFTER DELETING EVEN POSITION ELEMENTS:
2 4 6 8 10
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class Rearrange
{
String Txt,Cxt;
int len;
Scanner sc = new Scanner(System.in);
public Rearrange()
{
Txt="";
Cxt="";
len=0;
}
public void readword()
{
System.out.println("enter word in UPPERCASE");
Txt=sc.nextLine();
len=Txt.length();
}
public void convert()
{
int flag=-1,i=0;
char ch=' ';
for(i=0;i< len;i++)
{
ch=Txt.charAt(i);
if(ch=='A'|| ch=='E'||ch=='I'||ch=='O'|| ch=='U')
{
flag=i;
break;
}
}
if(flag==-1)
{
Cxt=Txt.concat("N");
}
else if(flag==0)
{
Cxt=Txt.concat("Y");
}
else
{
String d=Txt.substring(flag,len);
String e=Txt.substring(0,flag);
Cxt=d+e+"C";
}
}
public void display()
{
System.out.println("Originalword=" +Txt);
System.out.println("Changedword="+Cxt);
}
public static void main()
{
Rearrange ob1=new Rearrange();
ob1.readword();
ob1.convert();
ob1.display();
}
}