Programming of String/ Library Classes
1) Write a program to input a string, reverse it and check it is palindrome or not.
import [Link].*;
class Palindrome
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a word");
String s=[Link]();
String r=""; int l=[Link]();
for(int i=0;i<l;i++)
{
char ch=[Link](i);
r=ch+r;
}
[Link]("Reverse is "+r);
if([Link](r))
[Link]("Palindrome");
else
[Link]("Not Palindrome");
}
}
2) Write a program to input a sentence and print all the words in separate line.
import [Link].*;
class Word
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a sentence");
String s=[Link]();
String wd="";
s=s+" ";
int l=[Link]();
for(int i=0;i<l;i++)
{
char ch=[Link](i);
if(ch!=' ')
wd=wd+ch;
else
{
[Link](wd);
wd="";
}
}
}
}
3) Write a program to input a sentence and print the longest word.
import [Link].*;
class Longest
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a sentence");
String s=[Link]();
String wd="",max="";
s=s+" ";
int l=[Link]();
for(int i=0;i<l;i++)
{
char ch=[Link](i);
if(ch!=' ')
wd=wd+ch;
else
{
if([Link]()>[Link]())
max=wd;
wd="";
}
}
[Link]("Longest word is "+max);
}
}
4) Write a program to input a sentence and print smallest word.
import [Link].*;
class Smallest
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a sentence");
String s=[Link]();
String wd="",min=s;
s=s+" ";
int l=[Link]();
for(int i=0;i<l;i++)
{
char ch=[Link](i);
if(ch!=' ')
wd=wd+ch;
else
{
if([Link]()<[Link]())
min=wd;
wd="";
}
}
[Link]("Longest word is: "+min);
}
}
5) Write a program to input a string, count the number of vowels, consonants, number of digits and
number of special character.
import [Link].*;
class Count
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a sentence");
String s=[Link]();
s=[Link]();
int cv=0,cc=0,cd=0,cs=0;
int l=[Link]();
for(int i=0;i<l;i++)
{
char ch=[Link](i);
if([Link](ch))
{
if(ch=='A'|| ch=='E'|| ch=='I'|| ch=='O'|| ch=='U')
cv++;
else cc++;
}
else if([Link](ch))
cd++;
else cs++;
}
[Link]("No of vowels="+cv);
[Link]("No of consonats="+cc);
[Link]("No of digits="+cd);
[Link]("No of special characters="+cs);
}
}
6) Write a program to input a string, convert the upper case letter to lower case and lower case to
upper case.
import [Link].*;
class Convert
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a sentence");
String s=[Link]();
int l=[Link]();
for(int i=0;i<l;i++)
{
char ch=[Link](i);
if(ch>='A' && ch<='Z')
ch+=32;
else if(ch>='a' && ch<='z')
ch-=32;
[Link](ch);
}
}
}
7) Write a program to input a sentence and print all the words in reverse order without reversing the
string. Ex: Input : India is my country.
Output: aidnI si ym urtnuoc
import [Link].*;
class RevWd
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a sentence");
String s=[Link]();
String wd="";
s=s+" ";
int l=[Link]();
for(int i=0;i<l;i++)
{
char ch=[Link](i);
if(ch!=' ')
wd=ch+wd;
else
{
[Link](wd+" ");
wd="";
}
}
}
}
8) Write a program to input a sentence and display it in reverse order without reversing the word
Input : India is my country.
Output: country my is India
import [Link].*;
class RevString
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a sentence");
String s=[Link]();
String wd="";
s=" "+s;
int l=[Link]();
for(int i=l-1;i>=0;i--)
{
char ch=[Link](i);
if(ch!=' ')
wd=ch+wd;
else
{
[Link](wd+" ");
wd="";
}
}
}
}
9) Write a program to input a sentence, convert it into Title Case.
Input: this is a book.
Output: This Is A Book.
import [Link].*;
class Title
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a sentence");
String s=[Link]();
s=[Link]();
String wd="",nw="";
s=s+" ";
int l=[Link]();
for(int i=0;i<l;i++)
{
char ch=[Link](i);
if(ch!=' ')
wd=wd+ch;
else
{
char ch1=[Link](0);
ch1-=32;
nw=nw+ch1+[Link](1)+" ";
wd="";
}
}
[Link]("Resutlt is");
[Link](nw);
}
}
10) Write a program to input a sentence, convert it into Toggle Case.
Input: this is a book.
Output: tHIS iS a bOOK.
import [Link].*;
class Toggle
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a sentence");
String s=[Link]();
s=[Link]();
String wd="",nw="";
s=s+" ";
int l=[Link]();
for(int i=0;i<l;i++)
{
char ch=[Link](i);
if(ch!=' ')
wd=wd+ch;
else
{
char ch1=[Link](0);
ch1+=32;
nw=nw+ch1+[Link](1)+" ";
wd="";
}
}
[Link]("Resutlt is");
[Link](nw);
}
}
11) Write a program to input a name and print its initials.
Input : Atal Bihari Vajpayee
Output: A.B.V.
import [Link].*;
class Initials
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter name");
String s=[Link]();
s=[Link]();
s=" "+s;
int l=[Link]();
for(int i=0;i<l;i++)
{
char ch=[Link](i);
if(ch==' ')
[Link]([Link](i+1)+".");
}
}
}
12) Write a program to input a name and print its initials except its surmame.
Input : Atal Bihari Vajpayee
Output: [Link]
import [Link].*;
class Surname
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter name");
String s=[Link]();
s=[Link]();
s=" "+s;int k=[Link](' ');
for(int i=0;i<k;i++)
{
char ch=[Link](i);
if(ch==' ')
[Link]([Link](i+1)+".");
}
[Link]([Link](k+1));
}
}
13) Write a program to input a name and print its surname then its initials.
Input : Atal Bihari Vajpayee
Output: Vajpayee, A. B.
import [Link].*;
class SurnameFirst
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter name");
String s=[Link]();
s=[Link]();
s=" "+s;int k=[Link](' ');
[Link]([Link](k+1)+",");
for(int i=0;i<k;i++)
{
char ch=[Link](i);
if(ch==' ')
[Link]([Link](i+1)+".");
}
}
}
14) Write a input a string, convert into uppercase letters, count the frequency of each character.
import [Link].*;
class Frequency
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a string");
String str=[Link]();
str=[Link]();
for(char i='A';i<='Z';i++)
{
int f=0;
for(int k=0;k<[Link]();k++)
{
char ch=[Link](k);
if(ch==i)
f++;
}
if(f>0)
[Link]("Frequency of "+i+" "+f);
}
}
}
15) Write a input a string and display it in alphabetical order.
import [Link].*;
class Alphabetical
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a string");
String str=[Link]();
str=[Link]();
for(char i='A';i<='Z';i++)
{
for(int k=0;k<[Link]();k++)
{
char ch=[Link](k);
if(ch==i)
[Link](ch);
}
}
}
}
16) Write a program to input a sentence, print all the palindrome words and also display its frequency.
Ex: Input: Mom and dad are not at home at noon.
Output: Mom
Dad
Noon
Frequency: 3
import [Link].*;
class palin
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a sentence");
String s=[Link]();
s=s+" ";int f=0;
int l=[Link]();
String wd="",rev="";
for(int i=0;i<l;i++)
{
char ch=[Link](i);
if(ch!=' ')
{
wd=wd+ch;
rev=ch+rev;
}
else
{
if([Link](rev))
{
[Link](wd);
f++;
}
wd=rev="";
}
}
[Link]("Frequency="+f);
}
}
17) Write a program to input a sentence and print all the words in alphabetical order.
Input: This is our pen.
Output: ihsT is oru enp
18) Write a program to input a sentence and print all the words in ascending order of their length.
Input: This is our pencil.
Output: is our this pencil
19) Write a program to input a word, convert it into uppercase and convert it into Piglatin form.
Ex: input: TROUBLE output : OUBLETRAY
20) Design a class FiboString to generate Fibonacci strings. Some of the members of the class are given
below:
Class Name : FiboString [2014]
Data Members:
x : to store the first string
y : to store the second string
z : to store the concatenation of the previous two strings
n : to store number of terms
Member functions:
FiboString() : constructor to assign x=”a”, y=”b”, and z=”ba”
void accept() : to accept the number of terms n
void generate() : to generate and print the Fibonacci strings. The sum of first two strings is
the third string. Eg. “a” is the first string, “b” is the second string then the third will be “ba”, and fourth will be
“bab” and so on
Write main function.
import [Link].*;
class FiboString
{
String x,y,z;
int n;
Scanner sc=new Scanner([Link]);
FiboString()
{
x="a";
y="b";
z="ba";
n=0;
}
void accept()
{
[Link]("Enter no of terms");
n=[Link]();
}
void generate()
{
[Link](x+" "+y+" ");
for(int i=1;i<=n-2;i++)
{
z=y+x;
[Link](z+" ");
x=y;
y=z;
}
}
public static void main()
{
FiboString ob=new FiboString();
[Link]();
[Link]();
}
}
21) Design a class Exchange with the following details: [2013]
Data Members:
Sent : store the sentence
Rev : to store the new sentence
Size : stores the length of sentence
Member Functions:
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
Ex: Input: It is a warm day.
Output: tI si a marw yad.
Void display() : display the original sentence along with new changed sentence.
Write the main function.
import [Link].*;
class Exchange
{
String sent,rev;
int size;
Scanner sc=new Scanner([Link]);
Exchange()
{
sent=rev="";
size=0;
}
void readsentence()
{
[Link]("Enter a sentence");
sent=[Link]();
}
void exfirstlast()
{
String wd="";
sent=sent+" ";
size=[Link]();
for(int i=0;i<size;i++)
{
char ch=[Link](i);
if(ch!=' ')
wd=wd+ch;
else
{
int lw=[Link]();
if(lw==1)
rev=rev+wd+" ";
else
{
char x=[Link](0);
char y=[Link](lw-1);
String z=[Link](1,lw-1);
rev+=y+z+x+" ";
}
wd="";
}
}
}
void display()
{
[Link]("Original sentence is\n"+sent);
[Link]("New sentence is\n"+rev);
}
public static void main()
{
Exchange ob=new Exchange();
[Link]();
[Link]();
[Link]();
}
}
22) Design a class VowelWord to accept a sentence and calculate the frequency of the words that begin
with a vowel.
[2012]
Data Member:
Str : to store a sentence
Freq : store the frequency of the words beginning with a vowel
Member functions:
VowelWord() : default constructor
void readstr() : to accept a sentence
void freq_vowel() : count 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.
Write the main function.
import [Link].*;
class VowelWord
{
String str;
int freq;
Scanner sc=new Scanner([Link]);
VowelWord()
{
str="";
freq=0;
}
void readstr()
{
[Link]("Enter a sentence");
str=[Link]();
}
void freq_vowel()
{
str=" "+str;String v="AEIOUaeiou";
for(int i=0;i<[Link]();i++)
{
char ch=[Link](i);
if(ch==' ')
{
char ch1=[Link](i+1);
if([Link](ch1)>=0)
freq++;
}
}
}
void display()
{
[Link]("Original sentence is\n"+str);
[Link]("Frequency is "+freq);
}
public static void main()
{
VowelWord ob=new VowelWord();
[Link]();
ob.freq_vowel();
[Link]();
}
}
23) Design a class Stringfun to perform various operations on strings without using built-in functions
except for finding the length of the string. Some of the member functions/data members are given below:
Class name : Stringfun
Data members:
Str : to store the string
Member functions:
void input() : to store the string
void words() : to find and display the number of words, number of vowels and number of
uppercase characters in the string.
void frequency() : to display frequency of each character within the string.
Write the main function.
import [Link].*;
class Stringfun
{
Scanner sc=new Scanner([Link]);
String str;
void input()
{
[Link]("Enter a string");
str=[Link]();
}
void words()
{
int cw=0,cv=0,cu=0;String v="AEIOUaeiou";
for(int i=0;i<[Link]();i++)
{
char ch=[Link](i);
if(ch==' ')
cw++;
else
if([Link](ch)>=0)
cv++;
if(ch>='A' && ch<='B')
cu++;
}
[Link]("No of words="+(cw+1)+"\nno of vowels="+cv+"\nno of uppercase="+cu);
}
void frequency()
{
str=[Link]();
for(char i='A';i<='Z';i++)
{
int f=0;
for(int k=0;k<[Link]();k++)
{
char ch=[Link](k);
if(ch==i)
f++;
}
if(f>0)
[Link]("Frequency of "+i+" "+f);
}
}
public static void main()
{
Stringfun ob=new Stringfun();
[Link]();
[Link]();
[Link]();
}
}
24) Input a word in uppercase and check for the position of the first occurring vowel and perform the
following operations:
i) Words that begin with a vowel and concatenated with “Y”. EX. ”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 from beginning till positions of the vowel and concatenated by “C”. Ex. “PROJECT”
becomes “OJECTPRC”
iii) Words which do not contain a vowel are concatenated with “N”. ex. “SKY” becomes “SKYN”
class name : Rearrange
txt : to store a word
cxt : to store rearranged word
len : to store the length of the word
Member functions:
Rearrange() : default constructor
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
Write the main function.
import [Link].*;
class Rearrange
{
String txt,cxt;
int len;
Scanner sc=new Scanner([Link]);
Rearrange()
{
txt=cxt="";
len=0;
}
void readword()
{
[Link]("Enter a word");
txt=[Link]();
txt=[Link]();
len=[Link]();
}
void convert()
{
int flag=0,i;String v="AEIOU";
for(i=0;i<len;i++)
{
char ch=[Link](i);
if([Link](ch)>=0)
{
flag=1;
break;
}
}
if(flag==0)
cxt=txt+"N";
else if(i==0)
cxt=txt+"Y";
else
{
String a=[Link](i);
String b=[Link](0,i);
cxt=a+b+"C";
}
}
void display()
{
[Link]("Original string is");
[Link](txt);
[Link]("Changed string is");
[Link](cxt);
}
public static void main()
{
Rearrange ob=new Rearrange();
[Link]();
[Link]();
[Link]();
}
}
28) A class TheString accepts 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 members:
str : to store a word
len : to store the length of the string
wordscount : integer to store the number of words
cons : integer to store the number of cons
Member functions:
TheString() : default constructor
TheString(String ds) : parameterized constructor to assign str=ds
void countFreq() : to count the number of words and number of consonants and store them in
wordscount and consonants.
void display() : displays the original, along with number of words and the number of
consonants.
Define main method
import [Link].*;
class TheString
{
String str;
int len,wordscount,cons;
TheString(String ds)
{
str=ds;
}
void countFreq()
{
len=[Link]();String v="AEIOUaeiou";
for(int i=0;i<len;i++)
{
char ch=[Link](i);
if(ch==' ')
wordscount++;
else if([Link](ch))
{
if([Link](ch)==-1)
cons++;
}
}
}
void display()
{
[Link]("Original sentecne is");
[Link](str);
[Link]("no of words="+(wordscount+1));
[Link]("No of consonants="+cons);
}
public static void main()
{
TheString ob=new TheString("This is a pencil");
[Link]();
[Link]();
}
}
29) A class ConsChange has been defined with the following details: [2016]
class name : ConsChange
Data members:
word : to store a word
len : to store the length of the word
Member functions:
ConsChange() : default constructor
void readword() : accept the word in lowercase
void shiftcons() : shifts all the constants of the word at the beginning followed by
the vowels (e.g. spoon becomes spnoo)
void changeword() : changes the case of all occurring constants of the shifted word to
uppercase, for e.g. (spnoo becomes SPNoo)
void display() : display the original word, shifted word and the changed word
Define main method.
import [Link].*;
class ConsChange
{
String word;
int len;
Scanner sc=new Scanner([Link]);
ConsChange()
{
word="";
len=0;
}
void readword()
{
[Link]("Enter a word");
word=[Link]();
word=[Link]();
len=[Link]();
}
void shiftcons()
{
String s1="",s2="";
String v="aeiou";
for(int i=0;i<len;i++)
{
char ch=[Link](i);
if([Link](ch))
{
if([Link](ch)==-1)
s1+=ch;
else s2+=ch;
}
}
[Link](s1+s2);
}
void changeword()
{
String s1="",s2="";
String v="aeiou";
for(int i=0;i<len;i++)
{
char ch=[Link](i);
if([Link](ch))
{
if([Link](ch)==-1)
s1+=ch;
else s2+=ch;
}
}
s1=[Link]();
[Link](s1+s2);
}
void display()
{
[Link]("Original word is:"+word);
[Link]("Shifted word is:");
shiftcons();
[Link]("Changed word is:");
changeword();
}
public static void main()
{
ConsChange ob=new ConsChange();
[Link]();
[Link]();
}
}
30) A class SwapSort has been defined to perform string related operations on a word input. [2017]
Some of the members of the class are as follows:
Class name : SwapSort
Data Members:
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:
SwapSort() : default constructor to initialize data members with legal initial values.
Void readword() : to accept a word in UPPER CASE
Void swapchar() : to interchange/swap in first and last character 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’
void display() : displays the original word, swapped word and sorted word.
Write main function.
import [Link].*;
class SwapSort
{
Scanner sc=new Scanner([Link]);
String Wrd,Swapword,Sortword;
int Len;
SwapSort()
{
Wrd=Swapword=Sortword="";
Len=0;
}
void readword()
{
[Link]("Enter a word");
Wrd=[Link]();
Wrd=[Link]();
Len=[Link]();
}
void swapchar()
{
if(Len==1)
Swapword=Wrd;
else
{
char x=[Link](0);
char y=[Link](Len-1);
Swapword=y+[Link](1,Len-1)+x;
}
}
void sortword()
{
for(char i='A';i<='Z';i++)
{
for(int k=0;k<Len;k++)
{
char ch=[Link](k);
if(ch==i)
Sortword+=i;
}
}
}
void display()
{
[Link]("Original word is");
[Link](Wrd);
[Link]("Swapped Word is");
[Link](Swapword);
[Link]("Sowted word is");
[Link](Sortword);
}
public static void main()
{
SwapSort ob=new SwapSort();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
31) A class Capital has been defined to check whether a sentence has words beginning with a Capital letter
or not.
Some of the members of the class are given below: [2018]
Class name : Capital
Data members :
sent : to store a sentence
freq : stores the frequency of words beginning with a capital letter
Member functions :
Capital() : Default constructor
void input() : to accept a sentence
boolean isCap(String v) : checks and returns true if word begins with a capital letter, otherwise
returns false
void display() : displays the sentence along with the frequency of the words beginning
with a capital letter.
Write main function.
import [Link].*;
class Capital
{
Scanner sc=new Scanner([Link]);
String sent;
int freq;
Capital()
{
sent="";
freq=0;
}
void input()
{
[Link]("Enter a sentence");
sent=[Link]();
}
boolean isCap(String v)
{
char c=[Link](0);
if([Link](c))
return true;
else return false;
}
void display()
{
sent=sent+" ";
String wd="";
for(int i=0;i<[Link]();i++)
{
char ch=[Link](i);
if(ch!=' ')
wd+=ch;
else
{
if(isCap(wd))
freq++;
wd="";
}
}
[Link]("Original sentence is");
[Link](sent);
[Link]("Frequency="+freq);
}
public static void main()
{
Capital ob=new Capital();
[Link]();
[Link]();
}
}
32) A class Rearrange has been defined to modify a word by bringing all the vowels in the word at the
beginning followed by the consonatns. [2019]
Ex: ORIGINAL becomes OIIARGNL
Some of the members of the class are given below:
Class name : Rearrange
Data members :
wrd : to store a word
newwrd : to store a rearrange word
Member functions :
Rearrange() : Default constructor
void readword() : to accept a word
void freq_vow_con() : finds the frequency of vowels and consonatns in the word and displays
them with an appropriate message.
Void arrange() : rearranges the word by brnging the vowels at the beginning followed by
cononants.
void display() : displays the original word along with the rearranged word.
Write main function.
import [Link].*;
class Rearrange
{
Scanner sc=new Scanner([Link]);
String wrd,newwrd;
Rearrange()
{
wrd=newwrd="";
}
void readword()
{
[Link]("Enter a word");
wrd=[Link]();
wrd=[Link]();
}
void freq_vow_con()
{
int cv=0,cc=0;
String v="AEIOU";
for(int i=0;i<[Link]();i++)
{
char ch=[Link](i);
if([Link](ch))
{
if([Link](ch)>=0)
cv++;
else
cc++;
}
}
[Link]("no of vowels="+cv);
[Link]("no of consonants="+cc);
}
void arrange()
{
String s1="",s2="";
String v="AEIOU";
for(int i=0;i<[Link]();i++)
{
char ch=[Link](i);
if([Link](ch))
{
if([Link](ch)>=0)
s1+=ch;
else s2+=ch;
}
}
newwrd=s1+s2;
}
void display()
{
[Link]("Original word is:"+wrd);
[Link]("Rearrange word is:"+newwrd);
}
public static void main()
{
Rearrange ob=new Rearrange();
[Link]();
ob.freq_vow_con();
[Link]();
[Link]();
}
}
33) Write a program to accept a sentence which may be terminate by either ‘.’ ,’?’, or ‘!’ only. The words
are to separated by a single blank space and are in UPPERCASE. Perform the following task: [2019 practical]
a) Check for the validity of the accepted sentence
b) Convert for the non-palindrome words of the sentence into palindrome word by concatenating the
word by its reverse (excluding the last character).
Ex: The reverse of the word HELP would be LEH (omitting the last alphabet) and by concatenating both,
the new palindrome word is HELPLEH. Thus, the word HELP becomes HELPLEH.
NOTE: The words which end with repeated alphabets, for ex. ABB would becomes ABBA and not
ABBBA and XAZZZ becomes XAZZZAX
c) Display the original sentence along with the converted sentence.
Ex: INPUT: THE BIRD IS FLYING
OUTPUT: THEHT BIRDRIB ISI FLYINGNIYLF
import [Link].*;
class Pract2019
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a string");
String s=[Link]();
s=[Link]();
char x=[Link]([Link]()-1);
if(!(x=='.'|| x=='?' || x=='!'))
{
[Link]("Invalid Input");
[Link](0);
}
s=[Link](0,[Link]()-1);
s=s+" "; int l=[Link]();
String wd="",nw="";
for(int i=0;i<l;i++)
{
char ch=[Link](i);
if(ch!=' ')
wd+=ch;
else
{
if(isPalin(wd))
nw+=wd+" ";
else
{
wd=getPalin(wd);
nw=nw+wd+" ";
}
wd="";
}
}
[Link]("Result is");
[Link](nw);
}
static boolean isPalin(String w)
{
String rev="";
for(int i=0;i<[Link]();i++)
rev=[Link](i)+rev;
if([Link](rev))
return true;
else return false;
}
static String getPalin(String w)
{
String p="";
int lw=[Link]();
if([Link](lw-1)==[Link](lw-2))
p=getRepeatPalin(w);
else
{
String r="";
for(int i=0;i<lw-1;i++)
r=[Link](i)+r;
p=w+r;
}
return p;
}
static String getRepeatPalin(String w)
{
String p=w;
int i=[Link]()-1;
while(i>=1 && [Link](i)==[Link](i-1))
i--;
i--;
while(i>=0)
p=p+[Link](i--);
return p;
}
}
34) The names of the teams participating in a competition should be displayed on a banner vertically to
accommodate as many teams as possible in a single banner. Design a program to accept the names of N
teams, where 2<N<9 and display them in a vertical order, side by side with a horizontal tab (i.e. eight spaces).
Ex 1:
Input: N=3
Team 1: Emus
Team 2: Road Rols
Team 3: Coyote
Output:
E R C
m o o
u a y
s d o
t
R e
o
l
s
import [Link].*;
class Team
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter no of teams");
int N=[Link]([Link]());
String t[]=new String[N];
[Link]("Enter name of teams");
for(int i=0;i<N;i++)
t[i]=[Link]();
String max="";
for(int i=0;i<N;i++)
{
if(t[i].length()>[Link]())
max=t[i];
}
for(int i=0;i<[Link]();i++)
{
for(int k=0;k<N;k++)
{
if(i<t[k].length())
[Link](t[k].charAt(i)+"\t");
else
[Link]("\t");
}
[Link]();
}
}
}
35) Write a program to accept a sentence which may be terminated by either ‘.’, ‘?’,’!’ only. The words may
be separated by more than one blank space and are in UPPER CASE.
Perform the following tasks:
a) Find the number of words beginning and ending with a vowel.
b) Place the words which begin and end with a vowel at the beginning followed by the remaining words
as they occur in the sentence.
Test your program with the sample data and some random data:
EX 1:
INPUT : ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE.
OUTPUT : NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL=3
ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL
EX 2:
INPUT : YOU MUST AIM TO BE A BETTER PERSON TOMORROW THAN YOU ARE
TODAY.
OUTPUT : NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL=2
A ARE YOU MUST AIM TO BE BETTER PERSON TOMORROW THAN YOU
TODAY.
EX 3:
INPUT : HOW ARE YOU@
OUTPUT : INVALID INPUT
import [Link].*;
class Sfift
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a string");
String s=[Link]();
s=[Link]();
char x=[Link]([Link]()-1);
if(!(x=='.'|| x=='?' || x=='!'))
{
[Link]("Invalid Input");
[Link](0);
}
s=[Link](0,[Link]()-1);
s=s+" "; int l=[Link]();
String wd="",nw="",s1="",s2="";
int f=0; String v="AEIOU";
for(int i=0;i<l;i++)
{
char ch=[Link](i);
if(ch!=' ')
wd+=ch;
else
{
char p=[Link](0);
char q=[Link]([Link]()-1);
if([Link](p)>=0 && [Link](q)>=0)
{
f++;
s1+=wd+" ";
}else s2+=wd+" ";
wd="";
}
}
nw=s1+s2;
[Link]("No of words beginnin and ending with vowel is:"+f);
[Link](nw);
}
}
36) A class Mix has been defined to mix two words, character by character, in the following manner:
The first character of the first word is followed by the second word and so on. If the words are of
different length, the remaining characters of the longer word are put at the end.
Ex: If the First word is “JUMP” and the second word is “STROLL”, then the required word will be
“JSUTMRPOLL”
Some of the following members of the class are given below: [2020]
Class name : Mix
Data members :
wrd : to store a word
len : to store length of the word
Member functions :
Mix() : Default constructor
void feeddword() : to accept the word word in UPPER case
void mix_word(Mix P, Mix Q) : mixes the words of objects P and Q as stated above and stores the
resultant word in the current object.
void display() : display the word
Write main function.
import [Link].*;
class Mix
{
Scanner sc=new Scanner([Link]);
String wrd;
int len=0;
Mix()
{
wrd="";
len=0;
}
void feedword()
{
[Link]("Etner a word");
wrd=[Link]();
wrd=[Link]();
len=[Link]();
Dry Run
}
void mix_word(Mix P, Mix Q) Ob1/P ob2/Q ob3/this
{
int i=0,j=0;
while(true) wrd=”” wrd=”” wrd=””
{
len=0 len=0 len=0
if(i<[Link] && j<[Link])
{
[Link]+=[Link](i);
[Link]+=[Link](j);
}else break;
i++;j++;
}
if(i<[Link])
[Link]+=[Link](i);
if(j<[Link])
[Link]+=[Link](j);
}
void display()
{
[Link](wrd);
}
public static void main()
{
Mix ob1=new Mix();
Mix ob2=new Mix();
Mix ob3=new Mix();
[Link]();
[Link]();
ob3.mix_word(ob1,ob2);
[Link]("first string is");[Link]();
[Link]("Second string is");[Link]();
[Link]("Result is ");[Link]();
}
}
37) Write a program to accept a sentence which may be terminated by either ‘.’ , ‘?’, ‘!’ only. The words
are to separated by a single blank space are in UPPER CASE. [2020 Pract.]
Perform the following tasks:
a) Check for the validity of the accepted sentence only for the terminating character.
b) Arrange the words in ascending order of their length. If two or more words have the same length, then
sort them alphabetically.
c) Display the original sentence along with the converted sentence.
Ex:
INPUT : AS YOU SOW SO SHALL YOU REAP.
OUTPUT: AS YOU SOW SO SHALL YOU REAP
AS SO SOW YOU YOU REAP SHALL
import [Link].*;
class Pract20
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a sentence");
String s=[Link]();
s=[Link]();
int l=[Link]();
int ch=[Link](l-1);
if(ch=='.'|| ch=='?'|| ch=='!')
{
s=[Link](0,l-1);
String ar[]=[Link](" ");
for(int i=0;i<[Link]-1;i++)
{
for(int j=0;j<[Link]-i-1;j++)
{
if(ar[j].length()>ar[j+1].length())
{
String t=ar[j];
ar[j]=ar[j+1];
ar[j+1]=t;
}
else if((ar[j].length()==ar[j+1].length())&& (ar[j].compareTo(ar[j+1])>0))
{
String t=ar[j];
ar[j]=ar[j+1];
ar[j+1]=t;
}
}
}
[Link]("Original string is");
[Link](s);
[Link]("New string is");
for(int i=0;i<[Link];i++)
[Link](ar[i]+" ");
}
else [Link]("Invalid string");
}
}
38. Write a program to read a paragraph containing maximum 10 sentences. Each sentence should terminate
with ‘.’ or ‘?’ or ‘!’. Count the number of sentence and number of words, vowels and consonants in each
sentence and print it in following format.
Ex:
Input: Hello Friend! How are You? I am fine and you.
Output: Number of sentences are: 3
Sentence No 1 :
No of words : 2
No of Vowels : 4
No of Consonatnts : 7
Sentence No 2 :
No of words : 3
No of vowels : 5
No of Consonants : 4
And so on..
import [Link].*;
class StringPrg
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a paragraph");
String p=[Link]();
StringTokenizer st=new StringTokenizer(p,"?.!");
int c=[Link]();
[Link]("No of sentences are:"+c);
if(c>10)
{
[Link]("Out of range");
[Link](0);
}
for(int i=1;i<=c;i++)
{
String sent=[Link]();
int l=[Link]();
int cw=0,cv=0,cc=0;String v="AEIOUaeiou";
for(int k=0;k<l;k++)
{
char ch=[Link](k);
if(ch==' ')
cw++;
else
if([Link](ch))
{
if([Link](ch)>=0)
cv++;
else
cc++;
}
}
[Link]("Sentence no "+i+":");
[Link]("No of words="+(cw+1));
[Link]("No of vowels="+cv);
[Link]("No of consonants="+cc);
}
}
}