Chapter -8 String Manipulation

Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

Chapter 8: String Handling

A. Answer the following questions:


1. What is a String? Explain its use.
Ans.The String data type is a class defined in the java.lang package and every variable
that you create of String data type is actually an object or object reference of the String
class.
Using String variable in Java it is possible to manipulate strings i.e. a group of characters,
by extracting characters and manipulate it.
2. Why is String considered to be immutable?
Ans. String is considered to be immutable as it is not possible to insert or remove
characters from it. Moreover every time a new value is assigned to a String variable it
occupies a different memory location as compared to the previous allocation.
3. What are the common methods of initializing String objects?
Ans. A string may be initialised either using a String reference. For example,
String s="COMPUTER";
Or initialised using a constructor while creating a String object. For example,
String s=new String("COMPUTER");
4. What is concatenation? Give an example to show how '+' operator is used to
perform concatenation.
Ans. Joining of two or more strings to form a single string is termed as concatenation.
For example,
String a="AB", b="CD",c;
c=a+b;
Thus the value of the variable c is "ABCD".
5. Explain, with the help of an example, the functionality of toString( ).
Ans. The toString( ) function in Java is used to convert a value of any data type to
String. For example, the following statements convert a value (say 45) stored in an
integer variable to a String.
int a=45;//45 is stored in int type variable
String s=Integer.toString(a); // Content of variable a is converted to String.
6. Give the code to extract the first and the last character of a string in Java.
Ans. In case the String is stored in a variable named ‘s’.
To extract the first character use: s.charAt(0)
To extract the last character use: s.charAt(s.length( )-1)
7. State the use of the following String member functions, along with examples:
i) substring( )
ii) replace( )
iii) trim( )
Ans. i)substring( ) : It is used a part of a string from a given string . It has two
syntaxes:
a) substring(s,e): Extracts characters from s to e-1(s and e
are indexes and should be of int type). For example,
“INDIA”.substring(1,4) will give “NDI”.
b) substring(s): Extracts characters from s till the end of the
string (s is an index and should be of int type). For example,
“Laserjet”.substring(5) will yield ”jet”.

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);

Ans. He is going to the market


2. Give the output of the following program fragment:
String s="String";
int a=12,b=45;
System.out.println(s+a+b);
System.out.println(a+s+b);
System.out.println(a+b+s);
Ans. String1245
12String45
57String

3. Give the output of the following program fragment:


String s="india",s1="IndIA",s2=s;
System.out.println(s.equals(s1));
System.out.println(s.equalsIgnoreCase(s1));
System.out.println(s2==s);
System.out.println(s.toUpperCase( )==s1.toUpperCase( ));
System.out.println(s.startsWith("IN".toLowerCase( )));
System.out.println(s1.endsWith("iA".toUpperCase( )));
Ans. false
true
true
false
true
true
4. What do the following functions return for: [ICSE 2005]
String x ="hello";
String y = "world"
System.out.println(x + y);
System.out.println(x.length( );
System.out.println(x.charAt(3));
System.out.println(x.equals(y));
Ans. helloworld
5
l
false
5. What is the output of the following: [ICSE 2007]
i) System.out.println ("four :" + 4 + 2);
System.out.println (" four : "+(2+2));
ii) String S1 = "Hi";
String S2 = "Hi";
String S3 = "there";
String S4 = "HI";
System.out.println(S1 + "equals" + S2 + "" + S1.equals(S2));
System.out.println(S1 + "equals" + S3 + "" + S1 .equals(S3));
System.out.println(S1 + "equals" + S4 + "" + S1 .equals(S4));
System.out.println(S1 + "equalsIgnoreCase" +S4 + "" +
S1.equalsIgnoreCase(S4));
Ans. i) four :42
four : 4

ii) HiequalsHi→true
Hiequalsthere→false
HiequalsHI→false
HiequalsIgnoreCaseHI→true

6. If, String x = "Computer"; [ICSE 2008]


String y = "Applications";
What do the following functions return for?
i) System.out.println (x.substring(1,5));
ii) System out.println (x.indexOf(x.charAt(4)));
iii) System.out.println (y+x.substring(5));
iv) System.out.println (x.equals(y));
Ans. ompu
4
Applicationster
false
7. What will be the output for the following program segment? [ICSE 2006]
String s = new String("abc");
System.out.println(s.toUpperCase( ) );
Ans. ABC
8. What will be the output of the following code? [ICSE 2010]
char x = 'A' ; int m;
m=(x= ='a') ? 'A' : 'a';
System.out.println("m="+m);
Ans. m=97
9. Write a program to count the number of non-blank characters in a given sentence.
Ans. import java.util.*;
class Question9
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s;
char x;
int i,c=0;
System.out.print("Enter a sentence:");
s=sc.nextLine();
for(i=0;i<s.length();i++)
{
x=s.charAt(i);
if(x!=' ')
c++;
}
System.out.print("No. of non-blank characters:"+c);
}
}
10. Write a method to accept a word and print it in the following way:
Parameter: TRIAL
Output: L
AL
IAL
RIAL
TRIAL
Ans. class Question10
{
static void pattern(String s)
{
int i,j;
char x;
for(i=s.length()-1;i>=0;i--)
{
for(j=i;j<=s.length()-1;j++)
{
x=s.charAt(j);
System.out.print(x);
}
System.out.println();
}
}
}
11. Write a program to count the number of words in a given sentence. Assume
that there may be any number of blank spaces between the words.
Ans. import java.util.*;
class Question11
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s;
int i,c=1;
System.out.print("Enter a sentence:");
s=sc.nextLine();
s=s.trim();
s=s+" ";
for(i=0;i<s.length()-1;i++)
{
if(s.charAt(i)==' ' && s.charAt(i+1)!=' ')
c++;
}
System.out.print("No. of words:"+c);
}
}
12. Write a method to accept a name as parameter and print its initials.
Example,
Parameter-> AJAY PRATAP SINGH
Output-> A.P.S.
Ans. import java.util.*;
class Question12
{
static void main(String s)
{
s=s.trim();
s=" "+s;
String w="";
int i;
for(i=0;i<s.length()-1;i++)
{
if(s.charAt(i)==' ' && s.charAt(i+1)!=' ')
w=w+s.charAt(i+1)+".";
}
System.out.print(w);
}
}
13. Write a program to print the first and last word in a given sentence.
Ans. import java.util.*;
class Question13
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,first,last;
int i;
System.out.print("Enter a sentence:");
s=sc.nextLine();
s=s.trim();
i=s.indexOf(" ");
first=s.substring(0,i);
i=s.lastIndexOf(" ");
last=s.substring(i+1);
System.out.println("First word:"+first);
System.out.println("Last word:"+last);

}
}
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;
}

static void main()


{
Scanner sc=new Scanner(System.in);
int i;
char x;
String s,w="",t="";
System.out.println("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(isUnique(w))
t=t+w+" ";
w="";
}
}
System.out.println(t);
}
}
30. Write a method to accept a sentence as a parameter and print the word which
occurs the most. Assume that there is only one word occurring maximum number
of times.
Ans. import java.util.*;
class Question30
{
static void maxOccuring(String s)
{
s=s.trim();
s=s+" ";
int i,j,c=0,l=0;
char x,y;
String w="",m="",max="";
for(i=0;i<s.length();i++)
{
x=s.charAt(i);
if(x!=' ')
w=w+x;
else
{
c=0;
for(j=0;j<s.length();j++)
{
y=s.charAt(j);
if(y!=' ')
m=m+y;
else
{
if(m.equalsIgnoreCase(w))
c++;
m="";
}
}
if(c>l)
{
l=c;
max=w;
}
w="";
}
}
System.out.println("Word occuring maximum number of time:"+max);
}
}
31. Write a method to accept a sentence as parameter and print the longest word
at prime locations in the sentence. That is the 2nd word, 3rd word, 5th word, 7th
word, etc.
Ans. import java.util.*;
class Question31
{
static boolean isPrime(int n)
{
int i,c=0;
for(i=1;i<=n;i++)
{
if (n%i==0)
c++;
}
if(c==2)
return true;
else
return false;
}
static void main()
{
Scanner sc=new Scanner(System.in);
String s,lng="",w="";
int i,f=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
{
c++;
if(isPrime(c))
{
if(f==0)
{
lng=w;
f=1;
}
if(w.length()>lng.length())
lng=w;
}
w="";
}
}
System.out.println("Longest Word="+lng);
}
}
32. Write a program using a method Palin( ), to check whether a string is a
palindrome or not. A palindrome is a string that reads the same from left to right
and vice versa. [ICSE 2007]
E.g. MADAM, ARORA, ABBA, etc.
Ans. class Question32
{
static void Palin(String w)
{
int i;
String r="";
for(i=0;i<w.length();i++)
{
r=w.charAt(i)+r;
}
if(w.equalsIgnoreCase(r))
System.out.println("Palindrome");
else
System.out.println("Not Palindrome");
}
}
33. Write a program to input a sentence and print only the polindrome words.
Ans. import java.util.*;
class Question33
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,t="",w="",r="";
int i;
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;
r=x+r;
}
else
{
if(w.equalsIgnoreCase(r))
t=t+w+" ";
r=w="";
}
}
System.out.println(t);
}
}
34. Write a program to print the longest palindrome word in a given sentence, if any.
Ans. import java.util.*;
class Question34
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,lng="",w="",r="";
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;
r=x+r;
}
else
{
if(w.equalsIgnoreCase(r))
{
if(f==0)
{
lng=w;
f=1;
}
if(w.length()>lng.length())
lng=w;
}
r=w="";
}
}
if(f==1)
System.out.println("Longest Palindrome Word="+lng);
else
System.out.println("No Palindrome Words found");
}
}
35. Write a program to input any given string to calculate the total number of
characters and vowels present in the string and also reverse the string:
Example: INPUT
Enter String: SNOWY
OUTPUT
Total number of characters: 05
Number of Vowels : 01
Reverse string : YWONS [ICSE 2005]
Ans. import java.util.*;
class Question35
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,r="";
int i,c=0;
char x;
System.out.print("Enter String:");
s=sc.nextLine();
s=s.trim();
for(i=0;i<s.length();i++)
{
x=s.charAt(i);
if(x=='a' || x=='A' || x=='e' || x=='E' || x=='i' ||
x=='I' || x=='o' || x=='O' || x=='u' || x=='U')
c++;
r=x+r;
}
System.out.println("Total number of characters:"+s.length());
System.out.println("Number of Vowels:"+c);
System.out.println("Reverse string:"+r);
}
}
36. Consider the following statement: "January 26 is celebrated as the Republic
Day of India". Write a program to change "26" to "15", "January" to "August",
"Republic" to "Independence" and finally print "August 15 is celebrated as the
Independence Day of India". [ICSE 2006]
Ans. class Question36
{
static void main()
{
String s="January 26 is celebrated as the Republic Day of India";
String w="",t="";
int i,c=0;
char x;
s=s.trim();
s=s+" ";
for(i=0;i<s.length();i++)
{
x=s.charAt(i);
if(x!=' ')
w=w+x;
else
{
if(w.equalsIgnoreCase("January"))
w="August";
else if(w.equalsIgnoreCase("26"))
w="15";
else if(w.equalsIgnoreCase("Republic"))
w="Independence";
t=t+w+" ";
w="";
}
}
System.out.println(t);
}
}
37. Write a program to enter a sentence from the keyboard and count the number
of times a particular word occurs in it. Display the frequency of the search word.
Example:
INPUT:
Enter a sentence : the quick brown fox jumps over the lazy dog.
Enter a word to be searched : the
OUTPUT:
Searched word occurs : 2 times. [ICSE 2007]
Ans. import java.util.*;
class Question37
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,ws="",w="";
int i,c=0;
char x;
System.out.print("Enter a sentence:");
s=sc.nextLine();
System.out.print("Enter a word to be searched:");
ws=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(w.equals(ws))
c++;
w="";
}
}
System.out.println("Searched word occurs :"+c+" times");
}
}
38. Write a program to input a sentence and print the number of characters found
in the longest word of the given sentence. For example if S= "India is my country"
then the output should be 7. [ICSE 2009]
Ans. import java.util.*;
class Question38
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,w="";
int i,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;
else
{
if(w.length()>l)
l=w.length();
w="";
}
}
System.out.println("Length of longest word="+l);
}
}
39. Design a class to overload a function num_calc( ) as follows:
i) void num_calc(int num, char ch) with one integer argument and one character
argument, computes the square of integer argument if choice ch is 's' otherwise
finds its cube.
ii) void num_calc(int a, int b, char ch) with two integer arguments and one
character argument. It computes the product of integer arguments if ch is 'p' else
adds the integer.
iii) void num_calc(String s1, String s2) with two string arguments, which prints
whether the strings are equal or not.
Ans. class Question39
{
static void num_calc(int num, char ch)
{
if(ch=='s')
System.out.println(num*num);
else
System.out.println(num*num*num);
}

static void num_calc(int a,int b, char ch)


{
if(ch=='p')
System.out.println(a*b);
else
System.out.println(a+b);
}

static void num_calc(String s1,String s2)


{
if(s1.equalsIgnoreCase(s2))
System.out.println("Equal");
else
System.out.println("Not Equal");
}
}
40. In a set of 50 names, it is intended to find the total number of names which
contain at least one pair of consecutive letters, e.g. SURABHI. In this 'A' and 'B'
are consecutive letters and 'H' and 'I' are consecutive letters. Write a program for
the above situation.
Ans. import java.util.*;
class Question40
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,w="";
int i,j,f,c=0;
char x,y;
System.out.println("Enter 50 names:");
for(i=0;i<50;i++)
{
s=sc.nextLine();
s=s.toLowerCase();
f=0;
for(j=0;j<s.length()-1;j++)
{
x=s.charAt(j);
y=s.charAt(j+1);
if((char)(x+1)==y)
f=1;
}
if(f==1)
c++;
}
System.out.println("Number of consecutive letter words="+c);
}
}
41. Write a program to input a line of text consisting of sentences terminated by
either "." or "!" or "?".
The words in the sentences may be separated by multiple spaces. The program
should output:
i) The total number of words and
ii) The number of alphabets in the given text.
Ans. import java.util.*;
class Question41
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,w="";
int i,c=0,p=0;
char x,y;
System.out.println("Enter a string:");
s=sc.nextLine();
for(i=0;i<s.length();i++)
{
x=s.charAt(i);
if(x!=' ' && x!='.' && x !='!' && x!='?')
{
w=w+x;
if(x>='A' && x<='Z' || x>='a' && x<='z')
p++;
}
else
{
if(w!="")
c++;
w="";
}
}
System.out.println("Total number of words="+c);
System.out.println("Total number of alphabets="+p);
}
}
42. Write a program to input a simple mathematical expression consisting of a
binary operator and two integer operands and evaluate it. For example, if
INPUT: 55+11
OUTPUT: 66
INPUT: 5*12
OUTPUT: 60
Ans. import java.util.*;
class Question42
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,w="";
int i,f,l;
float eval=0;
char x=' ';
System.out.println("Enter an expression:");
s=sc.nextLine();
for(i=0;i<s.length();i++)
{
x=s.charAt(i);
if(x=='+' || x=='-' || x=='/' || x=='%' || x=='*')
break;
}
f=Integer.parseInt(s.substring(0,i).trim());
l=Integer.parseInt(s.substring(i+1).trim());
switch(x)
{
case '*':
eval=f*l;
break;
case '%':
eval=f%l;
break;
case '/':
eval=(float)f/l;
break;
case '+':
eval=f+l;
break;
case '-':
eval=f-l;
}
System.out.println(eval);
}
}
43. Write a program to accept any string: Count and print the number of pairs of
consecutive letters present in words in the forward direction only. Assume that all
the letters in the string are in the same case, consecutive letters in two different
words are not counted and 'za' or 'ZA' in any word is not a consecutive pair.
For example:
INPUT:
Enter String: ABSTRACT STRING IS BEING COUNTED EDUCATIONALLY.
OUTPUT:
Pairs of consecutive letters: 3
Ans. import java.util.*;
class Question43
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,w="";
int i,j,c=0;
float eval=0;
char x;
System.out.print("Enter String:");
s=sc.nextLine();
s=s.trim();
s=s+" ";
s=s.toUpperCase();
for(i=0;i<s.length();i++)
{
x=s.charAt(i);
if(x!=' ')
w=w+x;
else
{
for(j=0;j<w.length()-1;j++)
{
if((char)(w.charAt(j)+1)==w.charAt(j+1))
c++;
}
w="";
}
}
System.out.println("Pair of consecutive letters:"+c);
}
}
44. Pig Latin is a language game of alterations played in English. To form the Pig
Latin form of an English word the initial consonant sound is transposed to the end
of the word and an "ay" is affixed (for example, trash yields ashtray and plunder
yields underplay). Write a program to input a word and change it to Pig Latin.
Ans. import java.util.*;
class Question44
{
static void main()
{
Scanner sc=new Scanner(System.in);
String w,pigLatin;
int i;
char x;
System.out.print("Enter a word:");
w=sc.nextLine();
w=w.trim();
w=w.toUpperCase();
for(i=0;i<w.length();i++)
{
x=w.charAt(i);
if(x=='A' || x=='E' || x=='I' || x=='O' || x=='U')
break;
}
pigLatin=w.substring(i)+w.substring(0,i)+"AY";
System.out.println("Pig Latin:"+pigLatin);
}
}
45. An anagram is a word or a phrase made by transposing the letters of another
word or phrase; for example, "parliament" is an anagram of "partial men," and
"software" is an anagram of "swear oft". Write a program that figures out whether
one string is an anagram of another string. The program should ignore white
space and punctuation.
Ans. import java.util.*;
class Question45
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s1,s2;
int i,p,j,c,f=0;
char x,y;
System.out.print("Enter a string:");
s1=sc.nextLine();
System.out.print("Enter another string:");
s2=sc.nextLine();
s1=s1.trim();
s2=s2.trim();
s1=s1.toUpperCase();
s2=s2.toUpperCase();
for(i=0;i<s1.length();i++)
{
x=s1.charAt(i);
if(x>='A' && x<='Z')
{
c=0;
//Check the number of times it is present in the same string
for(j=0;j<s1.length();j++)
{
y=s1.charAt(j);
if(x==y)
c++;
}
p=0;
//Check the number of times it is present in another string
for(j=0;j<s2.length();j++)
{
y=s2.charAt(j);
if(x==y)
p++;
}
if(c!=p)
f=1;
}
}
if(f==0)
System.out.println("ANAGRAM");
else
System.out.println("NOT ANAGRAM");
}
}
46. Write a program to input a string in upper case and print the frequency of each
character. [ICSE 2010]
INPUT: COMPUTER HARDWARE
OUTPUT:
CHARACTERS FREQUENCY
A 2
C 1
D 1
E 2
H 1
M 1
P 1
R 3
T 1
U 1
W 1
Ans. import java.util.*;
class Question46
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s;
int i,c;
char x,y;
System.out.print("Enter a string:");
s=sc.nextLine();
s=s.toUpperCase();
System.out.println("CHARACTERS\t\tHARDWARE");
for(x='A';x<='Z';x++)
{
c=0;
for(i=0;i<s.length();i++)
{
y=s.charAt(i);
if(x==y)
c++;
}
if(c>0)
System.out.println(x+"\t\t\t"+c);
}
}
}
47. Write a program to input two words and find out the set that will be formed by
the intersection of the characters found in both the strings.
INPUT:
Enter the first word: TOPPLE
Enter the second word: CRIPPLE
OUTPUT:
Intersecting Set: PLE
Ans. import java.util.*;
class Question47
{
static void main()
{
Scanner sc=new Scanner(System.in);
String w1,w2,t="";
int i,j,f;
char x,y;
System.out.print("Enter the first word:");
w1=sc.nextLine();
w1=w1.toUpperCase();
System.out.print("Enter the second word:");
w2=sc.nextLine();
w2=w2.toUpperCase();
for(i=0;i<w1.length();i++)
{
f=0;
for(j=0;j<i;j++)
{
if(w1.charAt(i)==w1.charAt(j))
f=1;
}
if(f==0)
{
for(j=0;j<w2.length();j++)
{
if(w1.charAt(i)==w2.charAt(j))
f=1;
}
if(f==1)
t=t+w1.charAt(i);
}
}
System.out.println("Intersecting Set:"+t);
}
}
48. Write a program to input two words and find out the set that will be formed by
the union of the characters found in both the strings.
INPUT:
Enter the first word: TOPPLE
Enter the second word: CRIPPLE
OUTPUT:
Union Set: TOPLECRI
Ans. import java.util.*;
class Question48
{
static void main()
{
Scanner sc=new Scanner(System.in);
String w1,w2,t="",con="";
int i,j,f;
char x,y;
System.out.print("Enter the first word:");
w1=sc.nextLine();
w1=w1.toUpperCase();
System.out.print("Enter the second word:");
w2=sc.nextLine();
w2=w2.toUpperCase();
con=w1+w2;
for(i=0;i<con.length();i++)
{
f=0;
for(j=0;j<i;j++)
{
if(con.charAt(i)==con.charAt(j))
f=1;
}
if(f==0)
t=t+con.charAt(i);
}
System.out.println("Union Set:"+t);
}
}
49. Write a program to input an integer of the Decimal Number System and
convert it to corresponding Hexadecimal.
INPUT:
Enter an integer of the Decimal Number System: 2578
OUTPUT:
Equivalent Hexadecimal Number: A12
Ans. import java.util.*;
class Question49
{
static void main()
{
Scanner sc=new Scanner(System.in);
String hex="";
int n,d;
System.out.print("Enter an integer of the Decimal Number System:");
n=sc.nextInt();
while(n!=0)
{
d=n%16;
if(d<=9)
hex=d+hex;
else
hex=(char)(d+55)+hex;
n=n/16;
}
System.out.println("Union Set:"+hex);
}
}
50. Write a program to input an integer of the Hexadecimal Number System and
convert it to a number of Decimal Number System.
INPUT:
Enter a hexadecimal number: 12BC
OUTPUT:
Decimal Number: 4796
Ans. import java.util.*;
class Question50
{
static void main()
{
Scanner sc=new Scanner(System.in);
String hex="";
int i,d=0,dec=0,c=0;
char x;
System.out.print("Enter a Hexadecimal Number:");
hex=sc.nextLine();
for(i=hex.length()-1;i>=0;i--)
{
x=hex.charAt(i);
if(x>='0' && x<='9')
d=x-48;
else if(x>='A' && x<='Z')
d=x-55;
dec=dec+d*(int)Math.pow(16,c++);
}
System.out.println("Decimal Number:"+dec);
}
}

You might also like