Get ICSE String Manipulation Solutions for class 10. It will help you to make good preparation before attending the ICSE Board exam. We have provided you with String Manipulation Solutions class 10 to score good marks in your exam.
In ICSE CLass 10, String Manipulation Solutions is compulsory to score good marks in computer applications. Students Who are planning to score higher marks in class 10 should practice ICSE String Manipulation Solutions class 10.
ICSE String Manipulation Solutions for Class 10 Computer Applications
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.
Output:
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.
Output:
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.
Output:
false
true
true
false
true
true
4. What do the following functions return for:
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.
Output:
helloworld
5
l
false
5. What is the output of the following:
(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) Hi equals Hi→true
Hi equals there→false
Hi equals HI→false
Hi equalsIgnoreCase HI→true
6. If, String x = “Computer”;
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.
(i) ompu (ii) 4
(iii) Applicationster (iv) false
7. What will be the output for the following program segment?
String s = new String(“abc”);
System.out.println(s.toUpperCase());
Ans.
ABC
8. What will be the output of the following code?
char x = ‘A’; int m;
m=(x= =’a’) ? ‘A’ : ‘a’;
System.out.println(“m=”+m);
Ans.
m=97
9. Write statements to show how finding the length of a character array and char[] differs from finding the length of a String object str.
Ans.
chars.length
str.length()
10. Write a statement each to perform the following task on a string:
(i) Find and display the position of the last space in a string s.
(ii) Convert a number stored in a string variable x to double data type
Ans.
(i) System.out.println(s.lastIndexOf(“ ”));
(ii) double d=Double.parseDouble(x);
11. Write a statement each to perform the following task on a string:
(i) Extract the second last character of a word stored in the variable wd.
(ii) Check if the second character of a string str is in uppercase.
Ans.
(i) char sl=wd.charAt(wd.length()-2);
(ii) if(Character.isUpperCase(str.charAt(1)))
12. Give the output of the following string functions:
(i) “ACHIEVEMENT”.replace(‘E’,‘A’)
(ii) “DEDICATE”.compareTo(“DEVOTE”)
Ans.
(i) ACHIAVAMANT
(ii) -18
13. Consider the following String array and give the output:
String arr[]={“DELHI”, “CHENNAI”, “MUMBAI”, “LUCKNOW”, “JAIPUR”};
System.out.println(arr[0].length()>arr[3].length());
System.out.print(arr[4].substring(0,3));
Ans.
false
JAI
14. String x[] = {“SAMSUMG”, “NOKIA”, “SONY”, “MICROMAX”, “BLACKBERRY”};
Give the output of the following statements:
(i) System.out.println(x[1]);
(ii) System.out.println(x[3].length());
Ans.
(i) NOKIA
(ii) 8
15. Write the output for the following:
String s=“Today is Test”;
System.out.println(s.indexOf(‘T’));
System.out.println(s.substring(0,7)+“ ”+“Holiday”);
Ans.
0 Today i
Holiday
16. Give the ouput of the following string functions:
(i) “MISSISSIPPI”.indexOf(‘S’)+“MISSISSIPPI”.lastIndexOf(‘I’)
(ii) “CABLE”.compareTo(“CADET”)
Ans.
(i) 12
(ii) -2
17. State the output of the following program segment when executed:
String a = “Smartphone”, b = “Graphic Art”;
String h = a.substring(2, 5);
String k = b.substring(8).toUpperCase();
System.out.println(h);
System.out.println(k.equalsIgnoreCase(h));
Ans.
art
True
18. State the output of the following program segment:
String str1 = “great”; String str2 = “minds”;
System.out.println(strl.substring(0,2).concat(str2.substring(1)));
System.out.println((“WH” + (strl.substring(2).toUpperCase())));
Ans.
grinds
WHEAT
19. State the value of characteristic and mantissa when the following code is executed.
String s = “4.3756”;
int n = s.indexOf(‘.’);
int characteristic = Integer.parseInt(s.substring(0,n));
int mantissa = Integer.valueOf(s.substring(n+1));
Ans.
characteristic=4
mantissa=3756
20. State the output of the following program segment.
String s = “Examination”;
int n = s.length();
System.out.println(s.startsWith(s.substring(5, n)));
System.out.println(s.charAt(2) == s.charAt(6));
Ans.
false
true
21. What will the following code output?
String s = “malayalam”;
System.out.println(s.indexOf(‘m’));
System.out.println(s.lastIndexOf(‘m’));
Ans.
0
8
22. Give the output of the following:
String n = “Computer Knowledge”;
String m = “Computer Applications”;
System.out.println(n.substring (0,8).concat(m.substring(9)));
System.out.println(n.endsWith(“e”));
Ans.
ComputerApplications
true
SECTION B
Write programs for the following:
1. Write a program to count the number of non-blank characters in a given sentence.
Ans.
import java.util.*;
class Sol1
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s;
int i,c=0;
System.out.println(“Enter a sentence:”);
s=sc.nextLine();
for(i=0;i<s.length();i++)
{
char x=s.charAt(i);
if (x!=’ ‘)
c++;
}
System.out.println(“No. of non-blank character:”+c);
}
}
2. Write a program to input a sentence and count the number of ‘A’ or ‘a’ in it.
Ans.
import java.util.*;
class Sol2
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s;
int i,c=0;
System.out.println(“Enter a sentence:”);
s=sc.nextLine();
for(i=0;i<s.length();i++)
{
char x=s.charAt(i);
if (x==’A’ || x==’a’)
c++;
}
System.out.println(“No. of A or a:”+c);
}
}
3. Write a program to input a sentence and count the number of vowels in it.
Ans.
import java.util.*;
class Sol3
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s;
int i,c=0;
System.out.println(“Enter a sentence:”);
s=sc.nextLine();
s=s.toUpperCase();
for(i=0;i<s.length();i++)
{
char x=s.charAt(i);
if (x==‘A’ || x==‘E’ || x==‘I’ || x==‘O’ || x==‘U’)
c++;
}
System.out.println(“No. of vowels:”+c);
}
}
4. Write a method to accept a word and print it in the following way:
Parameter-> TRIAL
Output-> L
AL
IAL
RIAL
TRIAL
Ans.
class Sol4
{
static void main(String s)
{
int i,j;
for(i=s.length()-1;i>=0;i–)
{
for(j=i;j<s.length();j++)
{
char x=s.charAt(j);
System.out.print(x);
}
System.out.println();
}
}
}
5. Write a method to accept a name as parameter and print its initials.
Example,
Parameter-> AJAY PRATAP SINGH
Output-> A.P.S.
Ans.
class Sol5
{
static void main(String n)
{
int i;
n=“ ”+n.trim();
n=n.toUpperCase();
for(i=0;i<n.length()-1;i++)
{
if (n.charAt(i)==‘ ’ && n.charAt(i+1)!=‘ ’)
System.out.print(n.charAt(i+1)+“.”);
}
}
}
6. 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.
class Sol6
{
static void main(String n)
{
int i,lp;
n=“ ”+n.trim();
n=n.toUpperCase();
lp=n.lastIndexOf(‘ ’);
for(i=0;i<lp;i++)
{
if (n.charAt(i)==‘ ’&& n.charAt(i+1)!=‘ ’)
System.out.print(n.charAt(i+1)+“.”);
}
System.out.print(n.substring(lp+1));
}
}
7. 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.
Example :
INPUT : WelComE TO School
OUTPUT : wELcOMe to sCHOOL
Ans.
import java.util.*;
class Sol7
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,n=“ ”;
int i,c=0;
System.out.println(“Enter a sentence:”);
s=sc.nextLine();
for(i=0;i<s.length();i++)
{
char x=s.charAt(i);
if(x>=‘A’ && x<=‘Z’)
x=(char)(x+32);
else if(x>=‘a’ && x<=‘z’)
x=(char)(x-32);
n=n+x;
}
System.out.println(“New sentence:”+n);
}
}
8. Write a program to input a sentence and display only those words which begins with a vowel.
Ans.
import java.util.*;
class Sol8
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,w=“ ”;
int i,c=0;
System.out.println(“Enter a sentence:”);
s=sc.nextLine().toUpperCase();
s=s.trim()+ “ ”;
for(i=0;i<s.length();i++)
{
char x=s.charAt(i);
if(x!=‘ ’)
w+=x;
else
{
char y=w.charAt(0);
if(y==‘A’ || y==‘E’ || y==‘I’ || y==‘O’ || y==‘U’)
System.out.print(w+“ ”);
w=“ ”;
}
}
}
}
9. Write a program to input a sentence and display only those words which begin and end with the same alphabet.
Ans.
import java.util.*;
class Sol9
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,w=“ ”;
int i;
System.out.println(“Enter a sentence:”);
s=sc.nextLine().toUpperCase();
s=s.trim()+“ ”;
for(i=0;i<s.length();i++)
{
char x=s.charAt(i);
if(x!=‘ ’)
w+=x;
else
{
char f=w.charAt(0);
char l=w.charAt(w.length()-1);
if(f==l)
System.out.print(w+“ ”);
w=“ ”;
}
}
}
}
10. Write a program to input a sentence and print only those words which as 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 Sol10
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,w=“ ”;
int i,fa=0,fe=0,fi=0,fo=0,fu=0;
System.out.println(“Enter a sentence:”);
s=sc.nextLine().toUpperCase();
s=s.trim()+“ ”;
for(i=0;i<s.length();i++)
{
char x=s.charAt(i);
if(x!=‘ ’)
{
w+=x;
if(x==‘A’) fa=1;
if(x==‘E’) fe=1;
if(x==‘I’) fi=1;
if(x==‘O’) fo=1;
if(x==‘U’) fu=1;
}
else
{
if(fa+fe+fi+fo+fu==5)
System.out.print(w+“ ”);
w=“ ”;
}
}
}
}
11. 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.
E.g. MADAM, ARORA, ABBA, etc.
Ans.
class Sol11
{
static void Palin(String s)
{
int i;
s=s.trim();
String r=“ ”;
for(i=0;i<s.length();i++)
{
r=s.charAt(i)+r;
}
if(s.equalsIgnoreCase(r))
System.out.print(“Palindrome”);
else
System.out.print(“Not Palindrome”);
}
}
12. 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
Ans.
class Sol12
{
static void main(String s)
{
int i,c=0;
String r= “ ”;
s=s.toUpperCase();
for(i=0;i<s.length();i++)
{
char x=s.charAt(i);
if (x== ‘A’ || x==‘E’ || x==‘I’ || x==‘O’ || x==‘U’)
c++;
r=x+r;
}
System.out.println(“Total number of characters:”+c);
System.out.println(“Number of Vowels :”+c);
System.out.println(“Reverse string :”+r);
}
}
13. 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.
Ans.
import java.util.*;
class Sol13
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,w=“ ”,n;
int i,c=0;
System.out.println(“Enter a sentence:”);
s=sc.nextLine().toUpperCase();
s=s.trim()+“ ”;
System.out.println(“Enter a word to be searched:”);
n=sc.next();
for(i=0;i<s.length();i++)
{
char x=s.charAt(i);
if(x!=‘ ’)
w+=x;
else
{
if(w.equals(n))
c++;
w= “ ”;
}
}
System.out.println(“Searched word occurs:”+c+“times”);
}
}
14. 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.
Ans.
import java.util.*;
class Sol14
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,w=“ ”,l=“ ”;
int i;
System.out.println(“Enter a sentence:”);
s=sc.nextLine().toUpperCase();
s=s.trim()+ “ ”;
for(i=0;i<s.length();i++)
{
char x=s.charAt(i);
if(x!= ‘ ’)
w+=x;
else
{
if(w.length()>l.length())
l=w;
w= “ ”;
}
}
System.out.println(“Length of the longest word:”+l.length());
}
}
15. Design a class to overload a function num_calc( ) as follows:
a. 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.
b. 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.
c. void num_calc(String s1, String s2) with two string arguments, which prints whether the strings are equal or not.
Ans.
import java.util.*;
class Overlaod
{
static void num_calc(int num, char ch)
{
int c;
if (ch==‘s’)
c=num*num;
else
c=num*num*num;
System.out.println(“Answer:”+c);
}
static void num_calc(int a, int b, char ch)
{
int c;
if (ch==‘p’)
c=a*b;
else
c=a+b;
System.out.println(“Answer:”+c);
}
static void num_calc(String s1, String s2)
{
if (s1.equalsIgnoreCase(s2))
System.out.println(“Same”);
else
System.out.println(“Not Same”);
}
}
16. In a set of 50 names, it is intended to find the total number of names which contains 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 program.
Ans.
import java.util.*;
class Sol16
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s[]=new String[50];
int i,c=0,j;
System.out.println(“Enter 50 names:”);
for(i=0;i<50;i++)
s[i]=sc.nextLine();
for(i=0;i<50;i++)
{
for(j=0;j<s[i].length()-1;j++)
{
if((char)(s[i].charAt(j)+1)==s[i].charAt(j))
{
c++;
break;
}
}
}
System.out.println(“No. of words :”+c);
}
}
17. 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:
a. The total number of words and
b. The number of alphabets in the given text.
Ans.
import java.util.*;
class Sol17
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,w= “ ”;
int i,c=0,p=0;
System.out.println(“Enter a string:”);
s=sc.nextLine();
s=s.trim()+“ ”;
for(i=0;i<s.length();i++)
{
char x=s.charAt(i);
if(x!=’ ‘ && x!=’.’ && x!=’!’ && x!=’?’)
{
w+=x;
if(Character.isLetter(x))
c++;
}
else
{
if(w!=“ ”)
p++;
w=“ ”;
}
}
System.out.println(“The total number of words :”+p);
System.out.println(“The number of alphabets in the given text. :”+c);
}
}
18. 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 letter: 3
Ans.
import java.util.*;
class Sol18
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s;
int i,c=0;
System.out.println(“Enter a string:”);
s=sc.nextLine().toUpperCase();
for(i=0;i<s.length()-1;i++)
{
if((char)(s.charAt(i)+1)== s.charAt(i+1))
c++;
}
System.out.println(“Pairs of consecutive letter :”+c);
}
}
19. 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 ash-tray and plunder yields under-play). Write a program to input a word and change it to Pig Latin.
Ans.
import java.util.*;
class Sol19
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,n;
int i;
System.out.println(“Enter a word:”);
s=sc.nextLine().toUpperCase();
for(i=0;i<s.length();i++)
{
char x=s.charAt(i);
if(x==‘A’ || x==‘E’ || x==‘I’ || x==‘O’ || x==‘U’)
break;
}
n=s.substring(i)+s.substring(0,i)+“AY”;
System.out.println(“Pig Latin:”+n);
}
}
20. 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 Sol20
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s1,s2,n;
int i,c,p,f=0;
char x;
System.out.println(“Enter 2 words:”);
s1=sc.nextLine().toUpperCase();
s2=sc.nextLine().toUpperCase();
for(x=’A’;x<=’Z’;x++)
{
c=p=0;
for(i=0;i<s1.length();i++)
{
if(s1.charAt(i)==x)
c++;
}
for(i=0;i<s2.length();i++)
{
if(s2.charAt(i)==x)
p++;
}
if(c!=p)
f=1;
}
if(f==0)
System.out.println(“Anagram”);
else
System.out.println(“Not Anagram”);
}
}
21. Write a program to input a string in uppercase and print the frequency of each character.
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 Sol21
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s;
int i,c;
char x;
System.out.println(“Enter a string:”);
s=sc.nextLine().toUpperCase();
System.out.println(“CHARACTERS\t\tFREQUENCY”);
for(x=‘A’;x<=‘Z’;x++)
{
c=0;
for(i=0;i<s.length();i++)
{
if(s.charAt(i)==x)
c++;
}
if(c>0)
System.out.println(x+“\t\t”+c);
}
}
}
22. 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 Sol22
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s1,s2,n=“ ”;
int i;
char x;
System.out.println(“Enter 2 words:”);
s1=sc.nextLine().toUpperCase();
s2=sc.nextLine().toUpperCase();
for(i=0;i<s1.length();i++)
{
x=s1.charAt(i);
if(s2.indexOf(x)!=-1 && n.indexOf(x)==-1)
n+=x;
}
System.out.println(“Intersecting Set:”+n);
}
}
23. Write a program to enter a sentence and a word 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.
Ans.
import java.util.*;
class Sol23
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,w=“ ”,n;
int i,c=0;
System.out.println(“Enter a sentence:”);
s=sc.nextLine().toUpperCase();
s=s.trim()+ “ ”;
System.out.println(“Enter a word to be searched:”);
n=sc.next();
for(i=0;i<s.length();i++)
{
char x=s.charAt(i);
if(x!= ‘ ’)
w+=x;
else
{
if(w.equals(n))
c++;
w=“ ”;
}
}
System.out.println(“Searched word occurs :”+c+“times”);
}
}
24. Write a program in Java to accept a string in lower case and change the first letter of every word to upper case. Display the new string.
Sample input: we are in cyber world
Sample output: We Are In Cyber World
Ans.
import java.util.*;
class Sol24
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,n=“ ”;
int i,c=0;
System.out.println(“Enter a sentence:”);
s=sc.nextLine();
for(i=0;i<s.length();i++)
{
char x=s.charAt(i);
if(x>=‘A’ && x<=‘Z’)
x=(char)(x+32);
else if(x>=‘a’ && x<=‘z’)
x=(char)(x-32);
n=n+x;
}
System.out.println(“New sentence:”+n);
}
}
25. Design a class to overload a function check() as follows:
(i) void check(String str, char ch) – to find and print the frequency of a character in a string.
Example:
Input: Output:
str=“success” number of s present is =3
ch=‘s’
(ii) void check(String s1) – to display only vowels from string s1. after converting it to lower case.
Example:
Input: Output:
s1=“computer” o u e
Ans.
import java.util.*;
class Sol25
{
static void check(String str, char ch)
{
int i,c=0;
for(i=0;i<str.length();i++)
{
char x=str.charAt(i);
if(x==ch)
c++;
}
System.out.println(“number of”+ch+ “present is =”+c );
}
static void check(String s1)
{
s1=s1.toLowerCase();
int i;
for(i=0;i<s1.length();i++)
{
char x=s1.charAt(i);
if(x==‘a’ || x==‘e’ || x==‘i’ || x==‘o’ || x==‘u’)
System.out.print(x+ “ ”);
}
}
}
26. Write a program to input forty words in an array. Arrange these words in descending order of alphabets, using selection sort technique. Print the sorted array.
Ans.
import java.util.*;
class Sol26
{
static void main()
{
Scanner sc=new Scanner(System.in);
String a[]=new String[40],s;
int i,p,j;
System.out.println(“Enter 40 words:”);
for(i=0;i<40;i++)
a[i]=sc.nextLine();
for(i=0;i<39;i++)
{
s=a[i];p=i;
for(j=i+1;j<40;j++)
{
if(a[j].compareToIgnoreCase(s)<0)
{
s=a[j];
p=j;
}
}
a[p]=a[i];
a[i]=s;
}
for(i=0;i<40;i++)
System.out.println(a[i]);
}
}
27. Special words are those words which starts and ends with the same letter.
Examples:
EXISTENCE
COMIC
WINDOW
Palindrome words are those words which read the same from left to right and vice-versa.
Example:
MALAYALAM
MADAM
LEVEL
ROTATOR
CIVIC
All palindromes are special words, but all special words are not palindromes.
Write a program to accept a word check and print whether the word is a palindrome or only special word.
Ans.
import java.util.*;
class Sol27
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,r=“ ”;
int i;
System.out.println(“Enter a word:”);
s=sc.nextLine().toUpperCase();
for(i=0;i<s.length();i++)
{
char x=s.charAt(i);
r=x+r;
}
if(r.equals(s))
System.out.println(“Palindrome”);
else if(s.charAt(0)==r.charAt(0))
System.out.println(“Special”);
else
System.out.println(“Neither Palindrome nor special”);
}
}
28. Write a program to initialize the seven Wonders of the World along with their locations in two different arrays. Search for a name of the country input by the user. If found, display the name of the country along with its Wonder, otherwise display “Sorry Not Found!”
Seven wonders – CHICHEN ITZA, CHRIST THE RDEEEMER, TAJMAHAL, GREAT WALL OF CHINA,
MACHU PICCHU, PETRA, COLOSSEUM
Locations – MEXICO, BRAZIL, INDIA, CHINA, PERU, JORDAN, ITALY
Example – Country Name: INDIA Output: INDIA – TAJMAHAL
Country Name: USA Output: Sorry Not Found!
Ans.
import java.util.*;
class Sol28
{
static void main()
{
Scanner sc=new Scanner(System.in);
String wonders[]={“CHICHEN ITZA”,“CHRIST THE RDEEEMER”,“TAJMAHAL”,
“GREAT WALL OF CHINA”,“MACHU PICCHU”,“PETRA”,“COLOSSEUM”};
String locations[]={“MEXICO”,“BRAZIL”,“INDIA”,“CHINA”,“PERU”,“JORDAN”,“ITALY”};
String c=“ ”;
int i,f=0;
System.out.println(“Enter a country:”);
c=sc.nextLine().toUpperCase();
for(i=0;i<locations.length;i++)
{
if(locations[i].equals(c))
{
System.out.println(c+“-”+wonders[i]);
f=1;
}
}
if(f==0)
System.out.println(“Sorry Not Found!”);
}
}
29. Design a class to overload a function Joystring() as follows:
(i) void Joystring(String s, char ch1, char ch2) with one string and two character arguments that replaces the character argument ch1 with the character argument ch2 in the given string s and prints the new string
Example:
Input value of s = “TECHNALAGY”
ch1 = ‘A’
ch2 = ‘O’
Output : “TECHNOLOGY”
(ii) void Joystring(String s) with one string argument that prints the position of the first space
and the last space of the given String s.
Example:
Input value of = “Cloud computing means Internet based computing”
First Index : 5
Last Index : 36
(iii) void Joystring(String s1, String s2) with two string arguments that combines the two strings with a space between them and prints the resultant string
Example :
Input value of s1 = “COMMON WEALTH”
s2 = “GAMES”
Output : “COMMON WEALTH GAMES”
(use library functions)
Ans.
class Sol29
{
void Joystring(String s, char ch1, char ch2)
{
s=s.replace(ch1,ch2);
System.out.println(“ Sorry Not Found!”);
}
void Joystring(String s)
{
int f,l;
f=s.indexOf(‘ ’);
l=s.lastIndexOf(‘ ’);
System.out.println(“First Index :”+f);
System.out.println(“Last Index :”+l);
}
void Joystring(String s1, String s2)
{
String s3;
s3=s1+“ ”+s2;
System.out.println(s3);
}
}
30. Write a program to input twenty names in an array. Arrange these names in descending order of alphabets, using the bubble sort technique.
Ans.
import java.util.*;
class Sol30
{
static void main()
{
Scanner sc=new Scanner(System.in);
String names[]=new String[20];
String t=“ ”;
int i,j;
System.out.println(“Enter 20 names:”);
for(i=0;i<20;i++)
names[i]=sc.nextLine();
for(i=19;i>0;i–)
{
for(j=0;j<i;j++)
{
if(names[j].compareToIgnoreCase(names[j+1])>0)
{
t=names[j];
names[j]=names[j+1];
names[j+1]=t;
}
}
}
System.out.println(“Alphabetical order:”);
for(i=0;i<20;i++)
System.out.println(names[i]);
}
}
31. Write a program to assign a full path and file name as given below. Using library functions, extract and output the file path, file name and file extension separately as shown.
Input C:\Users\admin\Pictures\flower.jpg
Output Path: C:\Users\admin\Pictures\
File name: flower
Extension: jpg
Ans.
import java.util.*;
class Sol31
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,path,fname,ext;
int l,p;
System.out.println(“Entera full path name and file name:”);
s=sc.nextLine();
l=s.lastIndexOf(‘\\’);
path=s.substring(0,l+1);
p=s.lastIndexOf(“.”);
fname=s.substring(l+1,p);
ext=s.substring(p+1);
System.out.println(“Path:”+path);
System.out.println(“File name:”+fname);
System.out.println(“Extension:”+ext);
}
}
32. Write a program to accept a string. Convert the string to uppercase. Count and output the number of double letter sequences that exist in the string.
Sample Input: “SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE
Sample Output: 4
Ans.
import java.util.*;
class Sol32
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s;
int i,c=0;
System.out.println(“Enter a string:”);
s=sc.nextLine();
for(i=0;i<s.length()-1;i++)
{
if(s.charAt(i)==s.charAt(i+1))
c++;
}
System.out.println(“Answer:”+c);
}
}
33. Write a program to accept the names of 10 cities in a single dimension string array and their STD (Subscribers Trunk Dialing) codes in another single dimension integer array. Search for a name of a city input by the user in the list. If found, display “Search Successful” and print the name of the city along with its STD code, or else display the message “Search Unsuccessful, No such city in the list’.
Ans.
import java.util.*;
class Sol33
{
static void main()
{
Scanner sc=new Scanner(System.in);
String cities[]=new String[10];
int STD[]=new int[10];
String c=“ ”;
int i,j,f=0;
System.out.println(“Enter 20 cities and STD codes:”);
for(i=0;i<10;i++)
{
cities[i]=sc.nextLine();
STD[i]=sc.nextInt();
}
System.out.println(“Enter the name of a city to search:”);
c=sc.nextLine();
for(i=0;i<10;i++)
{
if(c.equalsIgnoreCase(cities[i]))
{
System.out.println(“ ”Search Successful”);
System.out.println(c+“-”+STD[i]);
f=1;
}
}
if(f==0)
System.out.println(“Search Unsuccessful, No such city in the list”);
}
}
34. Write a program to input a sentence and print the sentence in reverse order without reversing each word.
For example, If input is
He went to the market
Output should be
market the to went He
Ans.
import java.util.*;
class Sol34
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,w=“ ”,r=“ ”;
char x;
int i;
System.out.println(“Enter a sentence:”);
s=sc.nextLine();
s=s.trim()+“ ”;
for(i=0;i<s.length();i++)
{
x=s.charAt(i);
if(x!= ‘ ’)
w=w+x;
else
{
r=w+“ ”+r;
w=“ ”;
}
}
System.out.println(“New Sentence:”+r);
}
}
We believe that every student can get good marks by solving String Manipulation Solutions.So,lets start to learn String Manipulation Solutions ICSE class 10 for your exam.
You can also learn revision note for class 10 computer application prepared by our expert teachers at icseboards.com