STRING Solved

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

STRING PROGRAMS

1. Design a class Unique, which checks whether a word begin and ends with a vowel. [2022 Sem-I]
Ex. APPLE, ARORA etc.
The details of the members of the class are given below:
Class Name Unique
Data members / instance variables
word : stores a word
len : to store the length of the word
Methods / Members functions
Unique( ) : default constructor
void acceptword( ) : to accept the word in UPPER CASE.
boolean checkUnique( ) : checks and returns true if the word starts and ends with a vowel
otherwise return false.
void display( ) : displays the word along with an appropriate message.
Define the main( ) function to create an object and call the functions accordingly to enable the task.

import java.util.*;
class Unique
{
String word;
int len;

Unique()
{
word="";
len=0;
}

void acceptword()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the word");
word=sc.next();
word=word.toUpperCase();
len=word.length();
}

boolean checkUnique()
{
char ch1,ch2;
ch1=word.charAt(0);
ch2=word.charAt(len-1);
if((ch1=='A'||ch1=='E'||ch1=='I'||ch1=='O'||ch1=='U')&&
(ch2=='A'||ch2=='E'||ch2=='I'||ch2=='O'||ch2=='U'))
return true;
else
return false;
}
void display()
{
if(checkUnique()==true)
System.out.println("Word starts with and ends with an Vowels");
else
System.out.println("Word not starts with and ends with an Vowels");
}

public static void main()


{
Unique obj=new Unique();
obj.acceptword();
obj.display();
}
}
2. Design a class NoRepeat which checks where a word has no repeated alphabets in it. [2022 Sem-II]
Ex. COMPUTER has no repeated alphabets but SCIENCE has repeated alphabets.
The details of the class are given below:
Class Name : NoRepeat
Data members/ instance variable:
word : to store a word
len : to store the length of the word
Methods/ Member functions:
NoRepeat( String wd) : parameterized constructor to initialize word=wd.
boolean check( ) : check whether a word has no repeated alphabets and return true
else return false.
void print( ) : displays the word along with an appropriate message.
Define the main( ) function to create an object and call the functions accordingly to enable the task.

import java.util.*;
class NoRepeat
{
String word;
int len;
NoRepeat(String wd)
{
word=wd;
len=word.length();
}
boolean check()
{
int count=0;
for(char i='A';i<='Z';i++)
{
int c=0;
for(int j=0;j<len;j++)
{
char ch=word.charAt(j);
if(ch==i)
c++; }
if(c>0)
count++;
}
if(count==0)
return true;
else
return false;
}

void print()
{
if(check()==true)
System.out.println("word has no repeated alphabets");
else
System.out.println("word has repeated alphabets");
}

public static void main()


{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any word ");
String WORD=sc.next();
NoRepeat obj=new NoRepeat(WORD);
obj.print();
}
}
3. A class Rearrange has been defined to modify a word by bringing all the vowels in the word
at the beginning followed by the consonants. [2019]
Example : ORIGINAL becomes OIIARGNL
Some of the members of the class are given below:
class name : Rearrange
wrd : to store a word
newwrd : to store a rearranged word
Member functions/Methods:
Rearrange( ) : default constructor
void readword( ) : to accept the word in UPPER CASE.
void freq_vow_con( ) : find the frequency of vowels and consonants in the word and displays
them with an appropriate message.
void arrange( ) : rearranges, the word by bringing the vowels at the beginning followed
by consonants.
void display( ) : displays the original word along with the rearrange word.
Define the main( ) function to create an object and call the functions accordingly to enable the task.
import java.util.*;
class Rearrange
{
String wrd,newwrd;
Rearrange()
{
wrd="";
newwrd=""; }
void readword()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the word");
wrd=sc.next();
wrd=wrd.toUpperCase();
}

void freq_vow_con()
{
int vw=0,cs=0;
for(int i=0;i<wrd.length();i++)
{
char ch=wrd.charAt(i);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
vw++;
else if(ch>='A' && ch<='Z')
cs++;
}
System.out.println("No. of vowels = "+vw);
System.out.println("No. of consonants = "+cs);
}

void arrange()
{
String v_word="",c_word="";
for(int i=0;i<wrd.length();i++)
{
char ch=wrd.charAt(i);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
v_word=v_word+ch;
else if(ch>='A' && ch<='Z')
c_word=c_word+ch;
}
newwrd=v_word+c_word;
}
void display()
{
System.out.println("Original word = "+wrd);
System.out.println("Rearranged word = "+newwrd);
}
public static void main()
{
Rearrange obj=new Rearrange();
obj.readword();
obj.freq_vow_con();
obj.arrange();
obj.display();
}
}
4. A class Capital has been defined to check whether a sentence has words beginning with a capital
letter or not. [2018]
class name : Capital
sent : to store a sentence
freq : stores the frequency of words beginning with a capital letter
Member functions/ methods
Capital( ) : default constructor
void input( ) : to accept the sentence
boolean isCap(String w) : checks and returns true if word begins with a capital letter,
otherwise return false.
void display( ) : displays the sentence along with the frequency of the words beginning
with a capital letter.
Define the main( ) function to create an object and call the functions accordingly to enable the task.

import java.util.*;
class Capital
{
String sent;
int freq;
Capital()
{
sent="";
freq=0;
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter sentence");
sent=sc.nextLine();
}
boolean isCap(String w)
{
char ch1=w.charAt(0);
if(Character.isUpperCase(ch1))
return true;
else
return false;
}
void display()
{
String word="";
sent=sent.trim();
sent=sent+" ";
for(int i=0;i<sent.length();i++)
{
char ch=sent.charAt(i);
if(ch!=' ')
word=word+ch;
else
{
if(isCap(word)==true)
{
freq++;
System.out.println(word);
}
word="";
}
}
System.out.println("Frequency of word beginning with capital letter = "+freq);
}
public static void main()
{
Capital obj=new Capital();
obj.input();
obj.display();
}
}

5. Design a class Check which checks whether a word is a Palindrome or not. (Sample)
(Palindrome words are those which spell the same from either ends)
Ex. MADAM, LEVEL etc.
The details of the members of the class are given below:
Class name : Check
wrd : stores a word
len : to store the length of the word
Methods/ Member functions:
Check( ) : default constructor
void acceptword( ) : to accept the word
boolean palindrome( ) : checks and returns ‘true’ if the word is palindrome otherwise return ‘false’
void display( ) : displays the word along with an appropriate message.
Define the main( ) function to create an object and call the functions accordingly to enable the task.

import java.util.*;
class Check
{
String wrd;
int len;
Check()
{
wrd="";
len=0;
}
void acceptword()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any word");
wrd=sc.next();
len=wrd.length();
}
boolean palindrome()
{
String rev="";
for(int i=len-1;i>=0;i--)
{
char ch=wrd.charAt(i);
rev=rev+ch;
}
if(rev.equals(wrd))
return true;
else
return false;
}
void display()
{
if(palindrome()==true)
System.out.println("It is Palindrome word");
else
System.out.println("It is not a Palindrome word");
}
public static void main()
{
Check obj=new Check();
obj.acceptword();
obj.display();
}
}

6. Design a class VowelWord to accept a sentence and calculate the frequency of words that begin with
a vowel. The words in the input string are separated by a single blank space and terminated by a full
stop. The description of the class is given below:

Class name : VowelWord


Data members/instance variables:
str : to store a sentence
freq : store the frequency of the words beginning with a vowel
Member functions:
VowelWord( ) : constructor to initialize data members to legal initial value
voidreadstr( ) : to accept a sentence
voidfreq_vowel( ) : counts the frequency of the words that begin with a vowel.
void display( ) : to display the original string and the frequency of the words that begin
with a vowel.
Also define the main( ) function to create an object and call the methods accordingly to
enable the task. [2012]

import java.util.*;
class VowelWord
{
String str;
int freq;
VowelWord( )
{
str="";
freq= 0;
}

void readstr( )
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any sentence");
str=sc.nextLine();
}
void freq_vowel()
{
int len;
char ch;
String word="";
str=str.trim();
str=str+" ";
len=str.length();
for(int i=0;i<len;i++)
{
ch=str.charAt(i);
if(ch!=' ')
word=word+ch;
else
{
char ch1=word.charAt(0);
if(ch1=='A'||ch1=='E'||ch1=='I'||ch1=='O'||ch1=='U')
freq++;
word="";
}
}
}
void display()
{
System.out.println("Original String = "+str);
System.out.println("No. of words that begin with vowels "+freq);
}

public static void main()


{
VowelWord obj=new VowelWord();
obj.readstr();
obj.freq_vowel();
obj.display();
}
}
7. Input a sentence from the user and count the number of times, the words “an” and “and” are present in
the sentence. Design a class frequency using the description given below: [2011]
Class name : Frequency
Data members/variables:
text : stores the sentence
countand : to store the frequency of the word “and”
len : stores the length of the string
Member functions/Methods :
Frequency : constructor to initialize the instance variables
void accept(String n) : to assign n to text, where the value of the parameter n should be in lower case.
void checkandfreq( ) : to count the frequency of “and”.
void display( ) : to display the number of “and” and “an” with appropriate message.
Also define the main( ) function to create an object and call methods accordingly to enable the task.

import java.util.*;
class Frequency
{
String text;
int countand,len;
Frequency()
{
text="";
countand=0;
len=0;
}
void accept(String n)
{
text=n;
}
void checkandfreq()
{
int len;
char ch;
String wd="";
text=text.trim();
text=text+" ";
len=text.length();
for(int i=0;i<len;i++)
{
ch=text.charAt(i);
if(ch!=' ')
wd=wd+ch;
else
{
if(wd.equalsIgnoreCase("and"))
countand++;
wd="";
}
}
}
void display()
{
System.out.println("Frequency of the word 'and' = "+countand);
}
void main()
{
Frequency obj=new Frequency();
Scanner sc=new Scanner(System.in);
String s;
System.out.println("Enter any string");
s=sc.nextLine();
obj.accept(s);
obj.checkandfreq();
obj.display();
}
}

8. Design a class Exchange to accept a sentence and interchange the first alphabets with the last alphabet
for each word in the sentence, with single letter word remaining unchanged. The words in the input
sentence are separated by a single blank space and terminated by a full stop.
Example: Input: It is a warm day
Output: tI si a marwyad
Some of the data members and member functions are given below:
Class name : Exchange
Data members/instance variables:
sent : stores the sentence
rev : to store the new sentence
size : stores the length of the sentence
Member function:
Exchange( ) : default constructor
void readsentence( ) : to accept the sentence
void exfirstlast( ) : extract each word and interchange the first and last alphabet of the
word and form a new sentence rev using the changed words.
void display( ) : display the original sentence along with the new changed sentence.
Define the main ( ) function to create an object and call the functions accordingly to enable the task.

[2013]
import java.util.*;
class Exchange
{
String sent,rev;
int size;

Exchange()
{
sent="";
rev="";
size=0;
}
void readsentence()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any sentence");
sent=sc.nextLine();
}
void exfirstlast()
{
String word="";
char ch1,ch2;
StringTokenizer stringTokenizer = new StringTokenizer(sent);
while(stringTokenizer.hasMoreTokens())
{
word=stringTokenizer.nextToken();
ch1=word.charAt(0);
ch2=word.charAt(word.length()-1);
rev=rev+ch2+word.substring(1,word.length()-1)+ch1+" ";
}
}
void display()
{
System.out.println("original sentence = "+sent);
System.out.println("Changes sentence = "+rev);
}
public static void main()
{
Exchange obj=new Exchange();
obj.readsentence();
obj.exfirstlast();
obj.display();
}
}
9. A class TheStringaccept a string of a maximum of 100 characters with only one blank space between
the words.Some of the members of the class are as follows: [2015]
Class name : TheString
Data member/instance variable:
str : to store a string
len : integer to store the length of the string
wordcount : integer to store the number of words
cons : integer to store the number of consonant
Member functions/methods:
The String( ) : default constructor to initialize the data members
TheString(String ds) : parameterized constructor to assign str=ds.
voidcountFreq( ) : to count the number of words and the number of consonants
and store them in wordcount and cons respectively.
void Display( ) : to display the original string, along with the number of words
and the number of consonants.
Define the main( ) function to create an object and call the functions accordingly to enable the task.
importjava.util.*;
classTheString
{
String str;
int len,wordcount,cons;
TheString()
{
str="";
len=0;
wordcount=0;
cons=0;
}
TheString(String ds)
{
str=ds;
len=str.length();
}
voidcountFreq()
{
char ch;
for(int i=0;i<len;i++)
{
ch=str.charAt(i);
if(ch==' ')
wordcount++;
else if(!(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'))
cons++;
}
}
void display()
{
System.out.println("Original word = "+str);
System.out.println("no. of words = "+(wordcount+1));
System.out.println("no. of consonant = "+cons);
}
public static void main()
{
Scanner sc=new Scanner(System.in);
String s;
System.out.println("Enter the string");
s=sc.nextLine();
TheString obj=new TheString(s);
obj.countFreq();
obj.display();
}
}
10. A class Stringfun for the following operations. [2006]
Class name : Stringfun
Data members/instance variable:
str : to store a string
len : integer to store the length of the string
Member functions/Methods:
Stringfun : default constructor to initialize the data members
void input( ) : to accept the string
void words( ) : to find and display the no. of words, no. of vowels, and no. of upper case
letters in the string.
void frequency( ) : to display frequency of each character within the string.

import java.util.*;
class StringFun
{
String str;
int len;
StringFun()
{
str="";
len=0;
}

void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
str=sc.nextLine();
len=str.length();
}

void words()
{
int spc=0,vw=0,up=0;
str=str.toUpperCase();
for(int i=0;i<len;i++)
{
charch=str.charAt(i);
if(ch==' ')
spc++;
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
vw++;
if(Character.isUpperCase(ch))
up++;
}
System.out.println("No. of words = "+(spc+1));
System.out.println("No. of vowels = "+vw);
System.out.println("No. of UpperCase letter = "+up);
}
void frequency()
{
str=str.toUpperCase();
for(char i='A';i<='Z';i++)
{
intfreq=0;
for(int j=0;j<len;j++)
{
char ch=str.charAt(j);
if(ch==i)
freq++;
}
if(freq>0)
System.out.println(i+"\t"+freq);
}
}
}
11. A class Modify has been defined with the following details:
Class name : Modify
Data members/ Instance varaibale:
st : stores a string
len : to stores the length of the string
Member Functions:
void read( ) : to accept the string in Upper case alphabets
voidputin(int, char) : to insert a character at the Specified position in the string
and display the changed string. .
void takeout(int) : to remove character from the Specified position in the string
and display the changed string.
void change( ) : to replace each character in the original string by the character which
is at a distance of 2 moves ahead. For example:
“ABCD” becomes “CDEF”
“XYZ” becomes “ZAB”. [2008]
import java.util.*;
class Modify
{
String st;
int len;
void read()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any string");
st=sc.nextLine();
len=st.length();
}
void putin(int pos1,char x)
{
String Nword="";
Nword=st.substring(0,pos1)+x+st.substring(pos1,len);
System.out.println("New String after Insertion = "+Nword);
}
void takeout(int pos2)
{
String Dword="";
Dword=st.substring(0,pos2)+st.substring(pos2+1,len);
System.out.println("New String after deletion = "+Dword);
}
void change()
{
String Nword="";
int Asc;
char ch;
for(int i=0;i<st.length();i++)
{
ch=st.charAt(i);
Asc=ch+2;
if(Asc>90)
Asc=Asc-26;
Nword=Nword+(char)Asc;
}
System.out.println("Original word ="+st);
System.out.println("new Word ="+Nword);
}
}
12. A Class MyString has been defined with following details: [2007]
Class name : Mystring
Data Members/ Instance variable:
str : to store the string
len : to store the length of string
Member function/Methods:
Mystring( ) : default constructor to initialize the data members
voidreadstring( ) : to accept the string
int code(int index) : returns, ASCII code for the character at position index.
void word( ) : display longest word in the string.

import java.util.*;
class MyString
{
String str;
int len;
MyString()
{
str="";
len=0;
}
void readstring()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
str=sc.nextLine();
}
int code(int index)
{
char ch1=str.charAt(index);
return (int)ch1;
}

void word()
{
char ch;
String wd="",L_word="";
str=str.trim();
str=str+" ";
len=str.length();

for(int i=0;i<len;i++)
{
ch=str.charAt(i);
if(ch!=' ')
wd=wd+ch;
else
{
if(wd.length()>L_word.length())
L_word=wd;
wd="";
}
}

System.out.println("Longest word = "+L_word);


}
}

13. A class ConsChange has been defined with these following details: [2016]
Class name : ConsChange
Data members/instance variables:
word : store the word
len : stores the length of the word
Cword : stores the changed word
Sword : stores the shifted word
Member functions/methods:
ConsChange( ) : default constructor
void readword( ) : accepts the word in lowercase
void shiftcons( ) : shifts all the consonant of the word at the beginning followed by the
vowels (e.g. spoon becomes spnoo)
void changeword( ) : change the case of all occurring consonant of the shifted word uppercase,
for e.g. (spnoo becomes SPNoo)
void show( ) : displays the original word, shifted word and the change word.
Define the main( ) function to create an object and call the functions accordingly to enable the task.
import java.util.*;
class ConsChange
{
String word,Sword,Cword;
int len;
ConsChange()
{
word="";
Sword="";
Cword="";
len=0;
}
void readword()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a word");
word=sc.nextLine();
len=word.length();
}
void shiftcons()
{
String vowel="",cons="";
char ch;
for(int i=0;i<len;i++)
{
ch=word.charAt(i);
if(ch=='A'||ch=='a'||ch=='E'||ch=='e'||ch=='I'||ch=='i'||ch=='O'||ch=='o'||ch=='U'||ch=='u')
vowel=vowel+ch;
else
cons=cons+ch;
}
Sword=cons+vowel;
}
void changeword()
{
char ch;
for(int j=0;j<len;j++)
{
ch=Sword.charAt(j);
if(!(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'))
ch=Character.toUpperCase(ch);
Cword=Cword+ch;
}
}
void show()
{
System.out.println("Original word = "+word);
System.out.println("Shifted word = "+Sword);
System.out.println("Changed word = "+Cword);
}
public static void main()
{
ConsChange obj=new ConsChange();
obj.readword();
obj.shiftcons();
obj.changeword();
obj.show();
}
}

14. A class SwapSort has been defined to perform string related operations on a word input.
Some of the members of the class are as follows: [2017]
Class name : SwapSort
Data members/instance variable:
wrd : to store a word
len : integer to store length of the word
swapwrd : to store the swapped word
sortwrd : to store the sorted word
Member functions/methods:
SwapSort( ) : default constructor to initialize data members with legal initial values.
void readword( ) : to accept word in UPPER CASE.
void swapchar( ) : to interchange/swap the first and last characters of the word in ‘wrd’
and stores the new word in ‘swapwrd’.
void sortword( ) : sorts the characters of the original word in alphabetical order
and stores it in ‘sortwrd’.
Define the main( ) function to create an object and call the functions accordingly to enable the task.

import java.util.*;
class SwapSort
{
String wrd;
int len;
String swapwrd;
String sortwrd;
SwapSort()
{
wrd="";
swapwrd="";
sortwrd="";
len=0;
}
void readword()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any string");
wrd=sc.nextLine();
len=wrd.length();
}
void swapchar()
{
swapwrd=wrd.charAt(len-1)+wrd.substring(1,len-1)+wrd.charAt(0);
System.out.println("Word after swapping = "+swapwrd);
}
void sortword()
{
for(char i='A';i<='Z';i++)
{
for(int j=0;j<len;j++)
{
char ch=wrd.charAt(j);
if(ch==i)
sortwrd=sortwrd+ch;
}
}
System.out.println("String after sorting = "+sortwrd);
}
public static void main()
{
SwapSort obj=new SwapSort();
obj.readword();
obj.swapchar();
obj.sortword();
}
}

15. A sequence of Fibonacci strings is generated as follows: [2014]


S0 = “a”, S1=”b”, Sn=S(n-1) + S(n-2) where ‘+’ denotes concatenation.
Thus the sequence is a, b ,ba ,bab, babba, babbabab, …….n terms

Design a class FiboString to generate Fibonacci strings. Some of the members of the class are given
below:
Class name : FiboString
Data members/instance variables:
x : to store the first string
y : to store the second string
z : to store the concatenation of the previous strings
n : to store the number of terms
Member functions/methods:
FiboString( ) : Constructor to assign x=”a”,y=”b”,z=”ba”.
void accept( ) : to accept the number of terms ‘n’.
void generate( ) : to generate and print the Fibonacci strings.
The sum of (‘+’ is concatenation) first two strings is the third string.
Eg. “a” is the first string, “b” is second string then the third will be “ba”,
and fourth will be “bab” and so on.
Define the main( ) function to create objects and call the functions accordingly to enable the task.
import java.util.*;
class FiboString
{
String x,y,z;
int n;
FiboString()
{
x="a";
y="b";
z="ba";
n=0;
}
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of terms");
n=sc.nextInt();
}
void generate()
{
System.out.print(x+","+y+","+z);
for(int i=1;i<=n-3;i++)
{
x=y;
y=z;
z=y+x;
System.out.print(","+z);
}
}
public static void main()
{
FiboString obj=new FiboString();
obj.accept();
obj.generate();
}
}

16. Input a word inuppercase 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 contain 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:
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 UPPER CASE
void convert( ) : converts the word into its changed form and stores it in string Cxt.
void display( ) : displays the original and the changed word.
Define a main( ) function to create an object and call the function accordingly to enable the task. [2010]

17. A class Sort Word has been defined with the following details:
Class name : SortWord
Data members/instance variables:
txt : store the word
len : stores the length of the word
Member functions/methods:
SortWord( ) : default constructor
void readTxt( ) : to accept the word in lower case.
voidsortTxt( ) : to sort the word in alphabetical order of characters using bublesort
technique and display it.
void changeTxt( ) : to change the case of vowels in the word to UPPER case
( for e.g. school becomes schOOl)
void disp( ) : to display the changed string.
You need not to write the main function. [2009]

import java.util.*;
class SortWord
{
String txt;
int len;
SortWord()
{
txt="";
len=0;
}
void readTxt()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any string ");
txt=sc.nextLine();
len=txt.length();
}
void sortTxt()
{
char T[]=new char[len];
int z=0;
char ch,temp;
for(int i=0;i<len;i++)
{
ch=txt.charAt(i);
T[z++]=ch;
}

for(int i=0;i<T.length-1;i++)
{
for(int j=0;j<T.length-1-i;j++)
{
if(T[j]>T[j+1])
{
temp=T[j];
T[j]=T[j+1];
T[j+1]=temp;
}
}
}
System.out.println("Array in Ascending order");
for(int i=0;i<T.length;i++)
System.out.print(T[i]);
}
void changeTxt()
{
String Nword="";
for(int i=0;i<len;i++)
{
char ch1=txt.charAt(i);
if(ch1=='A'||ch1=='E'||ch1=='I'||ch1=='O'||ch1=='U'||ch1=='a'||ch1=='e'||ch1=='i'||ch1=='o'||
ch1=='u')
ch1=Character.toUpperCase(ch1);
Nword=Nword+ch1;
}
System.out.println("Vowel in uppercase = "+Nword);
}
}

18. A class Revstrdefine a recursive function to reverse a string and check whether it is a Palindrome.
The details of the class are given below:
Class name : Revstr
Data members/instance variable:
str : stores the string
Revst : stores the reverse of the string
Member functions/methods:
void getStr( ) : to accept the string
void recReverse(int) : to reverse the string using the Recursive Technique.
void check( ) : to display the original string. its reverse and whether the string is a
palindrome or not.
The main function need not be written. [2008]

import java.util.*;
class Revstr
{
String str,Revst;
int len;
Revstr()
{
str="";
Revst="";
len=0;
}
void getstr()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any sentece");
str=sc.nextLine();
len=str.length();
}
void recReverse(int c)
{
if(c>=0)
{
char ch=str.charAt(c);
Revst=Revst+ch;
recReverse(c-1);
}
}
void main()
{
Revstr obj=new Revstr();
obj.getstr();
obj.recReverse(len-1);
System.out.println(Revst);
}
}

You might also like