Chapter -8 String Manipulation
Chapter -8 String Manipulation
Chapter -8 String Manipulation
ii) replace( ) :It is used to replace a character/string from a string with another
character/string. Its general syntax is:
replace(c,d): It is used to replace the character c with character d. For example,
“BEETEL”.replace(‘E’,’O’) will yield “BOOTOL”
iii) trim() : It is used to remove leading and trailing spaces in a string. For example,
“ COMPUTER ”.trim( ) will yield “COMPUTER”
8. Give the difference between the following functions:
i) = = and equals( ) [ICSE 2008]
ii) toLowerCase( ) and toUpperCase( ) [ICSE 2005]
iii) startsWith( ) and endsWith( )
iv) indexOf( ) and lastIndexOf( ) [ICSE 2010]
Ans. i)
== equals( )
Checks whether two strings belong to Checks whether two string quantities
the same memory location or not. are same or not.
Checks whether two quantities are Checks only two string quantities are
equal or not. The quantities may be same or not.
any data type other than string.
ii)
toLowerCase( ) toUpperCase( )
Converts a given character or a string to Converts a given character or a string to
lower case. upper case.
iii)
startsWith( ) endsWith( )
Checks whether a string begins with a Checks whether a string ends with a
certain character or a string or not and certain character or a string or not and
returns true or false accordingly. returns true or false accordingly.
iv)
indexOf( ) lastIndexOf( )
Searches for a certain character or string Searches for a certain character or string
from to left to right in a given string and from to right to left in a given string and
return the index for the first occurrence of return the index for the first occurrence of
the found character or string otherwise the found character or string otherwise
return -1. return -1.
9. Explain compareTo( ) function, with respect to the values that it returns.
[ICSE 2010]
Ans. The compareTo( ) function is used to lexicographically check two strings where
the Unicode differs and accordingly return their difference.
The value returned by the compareTo( ) function is the difference between the
corresponding Unicodes of the characters, where it differs. If at no point the character
differs in the strings a 0 is returned.
For example,
System.out.println("abc".compareTo("abde"));
Will display –1, as the strings differ from the character at position 2 (i.e. 'c' and 'd'). The
Unicode for 'c' is 99 and 'd' is 100 and therefore their difference is –1.
System.out.println("COMPUTRONICS".compareTo("COMPUTER"));
Will display 13, as the pair of strings differ from the character at position 6 (i.e. 'R' and 'E').
The Unicode for 'R' is 82 and 'E' is 69 and therefore their difference is 13.
If there is no index position at which they differ, then the shorter string lexicographically
precedes the longer string. In this case, it returns the difference of the lengths of the strings.
For example,
System.out.println("AB".compareTo("ABCD"));
will display –2 as "AB".length( )–"ABCD".length( )=2 – 4=–2
10. Differentiate between compareTo( ) and equals( ) methods. [ICSE 2006]
Ans.
compareTo( ) equals( )
Checks two strings lexicographically. Checks two strings for equality.
Returns the first difference between the Returns either a true or a false.
Unicode of the characters where it differs.
Return type is int. Return type is Boolean.
11. What is Unicode? How can you get the unicode value of a Java character?
Ans. Unicode is a universal international standard character encoding that is capable of
representing most of the world's written languages. Using this system of encoding it is possible
to represent 65,536 characters.
To get the Unicode value of a Java character simply type-cast it with int. For example,
char x='B';
int p=(int)x; //It will assign the Unicode of the character x into p.
12. Explain what arithmetic operation can be performed with Java characters.
Ans. You can use increment or decrement operator with character variables to get the previous
or next character. For example, if
char x='B';
char y='d';
x++;
y--;
x++ will make the character x to 'C' and will make the character y to 'c'.
B. Answer as directed:
1. Give the output of the following program fragment:
String s=new String("He went to the market");
String r;
r=s.replace("went","is going");
System.out.println(r);
ii) HiequalsHi→true
Hiequalsthere→false
HiequalsHI→false
HiequalsIgnoreCaseHI→true
}
}
14. Write a method to accept a name as parameter and print the initial first and
then the title.
Example,
Parameter: AJAY PRATAP SINGH RATHORE
Output: A.P.S. RATHORE
Ans. import java.util.*;
class Question14
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,w="",last;
int i,l;
char x,y;
System.out.print("Enter a sentence:");
s=sc.nextLine();
s=s.trim();
s=s.toUpperCase();
s=" "+s;
l=s.lastIndexOf(' ');
last=s.substring(l+1);
for(i=0;i<l;i++)
{
if(s.charAt(i)==' ' && s.charAt(i+1)!=' ')
w=w+s.charAt(i+1)+".";
}
w=w+last;
System.out.println(w);
}
}
15. Write a program to input a string and print out the text with the uppercase and
lowercase letters reversed, but all other characters should remain the same as
before. [ICSE 2008]
Example :
INPUT : WelComE TO School
OUTPUT : wELcOMe to sCHOOL
Ans. import java.util.*;
class Question15
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,rs="";
int i;
char x;
System.out.print("Enter a sentence:");
s=sc.nextLine();
s=s.trim();
for(i=0;i<s.length();i++)
{
x=s.charAt(i);
if(x>='A' && x<='Z')
x=(char)(x+32);
else if(x>='a' && x<='z')
x=(char)(x-32);
rs=rs+x;
}
System.out.println("Reversed Sentence:"+rs);
}
}
16. Write a program to input a sentence and change it to its Title case. Title case
means to change the first alphabet of every word to capital and rest to small
letters.
Ans. import java.util.*;
class Question16
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,t="",w="";
int i;
char x,y;
System.out.print("Enter a sentence:");
s=sc.nextLine();
s=s.trim();
s=s.toLowerCase();
s=s+" ";
for(i=0;i<s.length();i++)
{
x=s.charAt(i);
if(x!=' ')
w=w+x;
else
{
y=w.charAt(0);
y=Character.toUpperCase(y);
w=y+w.substring(1);
t=t+w+" ";
w="";
}
}
System.out.println(t);
}
}
17. Write a program to input a sentence and display only those words which begin
with alphabet 'A' or 'a'.
Ans. import java.util.*;
class Question17
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,t="",w="";
int i;
char x,y;
System.out.print("Enter a sentence:");
s=sc.nextLine();
s=s.trim();
s=s.toLowerCase();
s=s+" ";
for(i=0;i<s.length();i++)
{
x=s.charAt(i);
if(x!=' ')
w=w+x;
else
{
y=w.charAt(0);
if(y=='a' || y=='A')
t=t+w+" ";
w="";
}
}
System.out.println(t);
}
}
18. Write a program to input a sentence and display only those words which end
with alphabet 'A' or 'a'.
Ans. import java.util.*;
class Question18
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,t="",w="";
int i;
char x,y;
System.out.print("Enter a sentence:");
s=sc.nextLine();
s=s.trim();
s=s.toLowerCase();
s=s+" ";
for(i=0;i<s.length();i++)
{
x=s.charAt(i);
if(x!=' ')
w=w+x;
else
{
y=w.charAt(w.length()-1);
if(y=='a' || y=='A')
t=t+w+" ";
w="";
}
}
System.out.println(t);
}
}
19. Write a program to input a sentence and display only those words which begin
with a vowel.
Ans. import java.util.*;
class Question19
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,t="",w="";
int i;
char x,y;
System.out.print("Enter a sentence:");
s=sc.nextLine();
s=s.trim();
s=s.toLowerCase();
s=s+" ";
for(i=0;i<s.length();i++)
{
x=s.charAt(i);
if(x!=' ')
w=w+x;
else
{
y=w.charAt(0);
if(y=='a' || y=='A' || y=='e' || y=='E' || y=='i' ||
y=='I' || y=='o' || y=='O' || y=='u' || y=='U')
t=t+w+" ";
w="";
}
}
System.out.println(t);
}
}
20. Write a program to input a sentence and display only those words which begin
and ends with the same alphabet.
Ans. import java.util.*;
class Question20
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,t="",w="";
int i;
char x,y,z;
System.out.print("Enter a sentence:");
s=sc.nextLine();
s=s.trim();
s=s.toLowerCase();
s=s+" ";
for(i=0;i<s.length();i++)
{
x=s.charAt(i);
if(x!=' ')
w=w+x;
else
{
y=w.charAt(0);
z=w.charAt(w.length()-1);
if(y==z)
t=t+w+" ";
w="";
}
}
System.out.println(t);
}
}
21. Write a program to input a sentence and print the longest word, assume that
there is only one longest word in the given sentence.
Ans. import java.util.*;
class Question21
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,lng="",w="";
int i,f=0;
char x;
System.out.print("Enter a sentence:");
s=sc.nextLine();
s=s.trim();
s=s+" ";
for(i=0;i<s.length();i++)
{
x=s.charAt(i);
if(x!=' ')
w=w+x;
else
{
if(f==0)
{
lng=w;
f=1;
}
if(w.length()>lng.length())
lng=w;
w="";
}
}
System.out.println("Longest Word="+lng);
}
}
22. Write a program to print the smallest word in a given sentence, assume that
there is only one smallest word in the given sentence.
Ans. import java.util.*;
class Question22
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,sma="",w="";
int i,f=0;
char x;
System.out.print("Enter a sentence:");
s=sc.nextLine();
s=s.trim();
s=s+" ";
for(i=0;i<s.length();i++)
{
x=s.charAt(i);
if(x!=' ')
w=w+x;
else
{
if(f==0)
{
sma=w;
f=1;
}
if(w.length()<sma.length())
sma=w;
w="";
}
}
System.out.println("Smallest Word="+sma);
}
}
23. Write a program to input a sentence and display only those words which have
even number of vowels.
Ans. import java.util.*;
class Question23
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,t="",w="";
int i,c=0;
char x;
System.out.print("Enter a sentence:");
s=sc.nextLine();
s=s.trim();
s=s+" ";
for(i=0;i<s.length();i++)
{
x=s.charAt(i);
if(x!=' ')
{
w=w+x;
if(x=='a' || x=='A' || x=='e' || x=='E' || x=='i' ||
x=='I' || x=='o' || x=='O' || x=='u' || x=='U')
c++;
}
else
{
if(c%2==0)
t=t+w+" ";
w="";
c=0;
}
}
System.out.println(t);
}
}
24. Write a program to print the word with maximum number of vowels in a given
sentence. Assume that there is only one word having maximum number of
vowels.
Ans. import java.util.*;
class Question24
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,max="",w="";
int i,c=0,f=0,l=0;
char x;
System.out.print("Enter a sentence:");
s=sc.nextLine();
s=s.trim();
s=s+" ";
for(i=0;i<s.length();i++)
{
x=s.charAt(i);
if(x!=' ')
{
w=w+x;
if(x=='a' || x=='A' || x=='e' || x=='E' || x=='i' ||
x=='I' || x=='o' || x=='O' || x=='u' || x=='U')
c++;
}
else
{
if(f==0)
{
max=w;
l=c;
f=1;
}
else
{
if(c>l)
{
l=c;
max=w;
}
}
w="";
c=0;
}
}
System.out.println("Word having maximum number of vowels:"+max);
}
}
25. Write a program to input a sentence and print the word having least number of
vowels. Assume that there is only one word having least number of vowels.
Ans. import java.util.*;
class Question25
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,min="",w="";
int i,c=0,f=0,l=0;
char x;
System.out.print("Enter a sentence:");
s=sc.nextLine();
s=s.trim();
s=s+" ";
for(i=0;i<s.length();i++)
{
x=s.charAt(i);
if(x!=' ')
{
w=w+x;
if(x=='a' || x=='A' || x=='e' || x=='E' || x=='i' ||
x=='I' || x=='o' || x=='O' || x=='u' || x=='U')
c++;
}
else
{
if(f==0)
{
min=w;
l=c;
f=1;
}
else
{
if(c<l)
{
l=c;
min=w;
}
}
w="";
c=0;
}
}
System.out.println("Word having minimum number of vowels:"+min);
}
}
26. Write a method to accept a sentence as parameter and print the longest word
with least number of vowels in it. Assume that there is only one such word in the
given sentence.
Ans. import java.util.*;
class Question26
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,lng="",w="",min="";
int i,f=0,l=0,c=0;
char x;
System.out.print("Enter a sentence:");
s=sc.nextLine();
s=s.trim();
s=s+" ";
for(i=0;i<s.length();i++)
{
x=s.charAt(i);
if(x!=' ')
w=w+x;
else
{
if(f==0)
{
lng=w;
f=1;
}
if(w.length()>lng.length())
lng=w;
w="";
}
}
f=0;
for(i=0;i<s.length();i++)
{
x=s.charAt(i);
if(x!=' ')
{
w=w+x;
if(x=='a' || x=='A' || x=='e' || x=='E' || x=='i' ||
x=='I' || x=='o' || x=='O' || x=='u' || x=='U')
c++;
}
else
{
if(w.length()==lng.length())
{
if(f==0)
{
min=w;
l=c;
f=1;
}
else
{
if(c<l)
{
l=c;
min=w;
}
}
}
w="";
c=0;
}
}
System.out.println("Longest Word having least number of vowels:"+min);
}
}
27. Write a program to input a sentence and print only those words which have all
the five vowels present in it (i.e. A, E, I, O, U), in a given sentence.
For example,
INPUT
Enter a sentence: EDUCATION IS A MUST FOR THE DEVELOPMENT
OF THE COUNTRY
OUTPUT
EDUCATION
Ans. import java.util.*;
class Question27
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,t="",w="";
int i,fa=0,fe=0,fi=0,fo=0,fu=0;
char x;
System.out.print("Enter a sentence:");
s=sc.nextLine();
s=s.trim();
s=s+" ";
for(i=0;i<s.length();i++)
{
x=s.charAt(i);
if(x!=' ')
{
w=w+x;
if(x=='a' || x=='A' ) fa=1;
if(x=='e' || x=='E') fe=1;
if(x=='i'|| x=='I') fi=1;
if(x=='o' || x=='O') fo=1;
if(x=='u' || x=='U') fu=1;
}
else
{
if(fa==1 && fe==1 && fi==1 && fo==1 && fu==1)
t=t+w+" ";
w="";
fa=fe=fi=fo=fu=0;
}
}
System.out.println("Word having all the five vowels:"+t);
}
}
28. Write a method to accept a word as parameter and print the alphabets in
alphabetical order.
e.g. Input: world
Output: DLORW
e.g. Input: HUMAN
Output: AHMNU
Ans. class Question28
{
static void alphabetical(String w)
{
w=w.toUpperCase();
int i;
char x;
String t="";
for(x='A';x<='Z';x++)
{
for(i=0;i<w.length();i++)
{
if(x==w.charAt(i))
t=t+x;
}
}
System.out.println(t);
}
}
29. Write a method to accept a word as parameter and return true if all its
alphabets are unique otherwise return false. Now use it in another function to
print those words whose all alphabets are unique in a given sentence.
Ans. import java.util.*;
class Question29
{
static boolean isUnique(String w)
{
int i,j;
boolean f=true;
for(i=0;i<w.length();i++)
{
for(j=i+1;j<w.length();j++)
{
if (w.charAt(i)==w.charAt(j))
f=false;
}
}
return f;
}