String Function Worksheet 2023 24
String Function Worksheet 2023 24
String Function Worksheet 2023 24
_________________________________________________________________________________
1 String Methods BlueJ
System.out.println("Starts With U is " + str1.startsWith("U"));
System.out.println("Equals is " + str1.equals("My String
Methods"));
System.out.println("Equals is " + str1.equals("my string
methods"));
System.out.println("Equals Ignore Case is " +
str1.equalsIgnoreCase("my string methods"));
System.out.println("6th letter is " + str1.charAt(5));
System.out.println("Concat : " + str1.concat(str2));
System.out.println("Equals is " + str1.compareTo(str2));
System.out.println("Index of M is " + str1.indexOf("M"));
System.out.println("Last Index of M is " +
str1.lastIndexOf("M"));
System.out.println("Length of str1 is " + str1.length());
System.out.println("SubString of str1 from index no 2 is " +
str1.substring(2));
System.out.println("SubString of str1 from index no 2 to index 7 is " + str1.substring(2,7));
System.out.println("Value of integer x :" + str1.valueOf(x));
System.out.println("Trim of str2 is " + str2.trim());
System.out.println("Str 2 after replacing L with 8" + str2.replace('L','8'));
System.out.println("Returns the string itself is toString() : " + str1.toString());
}}
***********************************
Worksheet – Library Classes (String)
1. Write a java class ‘space’ to accept string from the user and display the number of times a space has come in
the string.
2. WAP to accept string it should print total number of characters and vowels present in the string and also
reverse the string.
3. Write a program that checks a string to be palindrome without using function reverse. A palindrome is a string
that reads the same from left to right and from right to left.(eg. MADAM, NITIN, ARORA, MALAYALAM).
4. Write a program in Java to accept a word and store it in lowercase and replace ‘e’ with ‘*’ present in the word.
5. WAP in java to accept a string and find:
i) number of blank spaces in the string
ii) number of words in the string
iii) number characters present in the string
6. WAP in java to accept a word and display the same in Pig Latin form.
Sample Input : trouble Output : oubletray
7. Consider the following statement “ January 26 is celebrated as the Republic Day of India” WAP to change the
number, from 26 to 15, the text ‘January’ to ‘August’ & the text ‘Republic’ to ‘Independence’ are finally print
“August 15 is celebrated as the Independence day of India.”
8. Write a programe in java to accept a String and convert in Uppercase and replace all the vowels with
Asterisk ( * ) present in the String.
Sample : Tata Steel is in Jamshedpur.
Output : TATA STEEL IS IN JAMSHEDPUR.
T*T* ST**L *S *N J*MSH*DP*R
_________________________________________________________________________________
2 String Methods BlueJ
9. WAP to input a string as a sentence through the keyboard and replace every occurance of the vowel with a
consonant that follows that vowel. For example Consonant following ‘a’ is ‘b’. Reconstruct the string and
display the same.
10. Write a program to accept a string and display the frequency of a character accepted by user.
11. WAP in java to accept a string and display the new String after removing all the vowels from the string.
Input : Computer Application with BlueJ
Output : Cmptr pplctn wth BlJ
12. Write a program in Java to accept a word / a String and display all the vewels present in it.
Input : COMPUTER APPLICATION
OUTPUT : OUEAIAI O
13. Write a program to accept a string and display each word in new line.
Example : Input : This is my country.
Output : This
is
my
country.
14. Write a program in Java to accept a name and Display only the initials (i.e. First alphabet of each word).
Input : LAL KRISHNA ADVANI Output : L K A
15. WAP in java to accept a name (containing three words) and display the initials along with the surname.
Input : Subhash Chandra Bose Output : S.C. Bose
16. Write a method to input a string using parameter and print as follows.
E.g. INPUT We are in the school OUTPUT school the in are We
17. WAP to display each word of the string in reverse order.
e.g : - if s = India is my country”. Output = aidnI si ym yrtnuoc”.
18. Write a program to input your name and print it in bibliography format.
For example : if input is : “Lok Manya Bal Gangadhar Tilak”
The output should be : Tilak, L.M.B.G.
19. WAP in Java to accept a sentence and word. Find the frequency of a word present in a string.
20. WAP to accept string and count number of palindrome words in a string.
21. Write a program in Java to accept a string and count the words (containing at least two alphabets) which
begins with a vowel (upper or lower case).
EXAMPLE : Input : BSNL launches One India Plan to connect people.
Output : No. of words begin with a vowel : 2
22. Write a program to input a word from the user and remove the consecutive repeated characters by replacing
the sequence of repeated characters by its single occurrence.
Example: INPUT : Jaaavvvvvaaaaaaaa
OUTPUT : Java
23. Write a program to accept a string and print all the characters of the string in the even position as one word
and all the characters in odd position as another word. Also print the number of characters in the new words.
For Example : If input is “sensor”, then the output should be:
_________________________________________________________________________________
3 String Methods BlueJ
Even position is: esr Total number of characters is 3
Odd position is: sno Total number of characters is 3
24. Write a program in Java to accept a sentence and display the longest word and the length of the longest word
present in the String.
Input : “We are Indian.
Output : The longest word : Indian & The Length of the word :6
25. Wirte a program to accept a word from user and according to the user choice it should print the following
pattern:
Input : Enter Word : COMPUTER
Enter your choice : to print patter 1 or 2
Enter choice : 1 2
COMPUTER C
COMPUTE CO
COMPUT COM
COMPU COMP
COMP COMPU
COM COMPUT
CO COMPUTE
C COMPUTER
26. 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
27. Write a program in Java to input two strings of same length and form a new word in such a way that, the first
character of the first word is followed by the first character of the second word and so on. If the strings are
not of same length, then print the message “Invalid Input”.
Worksheet – Library Classes(Character)
Character class has many static methods to manipulate or inspect single-character data.
Most used accessor methods of Character class(lang package)
Functions Purpose
boolean Character.isDigit(ch) Returns true if ch is digit
boolean Character.isLetter(ch) Returns true if ch is letter
boolean Character.isLetterOrDigit(ch) Returns true if ch is digit or letter.
boolean Character.isLowerCase(ch) Returns true if ch is lowercase
boolean Character.isUpperCase(ch) Returns true if ch is uppercase
boolean Character.isWhitespace(ch) Returns true if ch is space
char toLowerCase() Returns lowercase of ch
char toUpperCase() Returns uppercase of ch
_________________________________________________________________________________
4 String Methods BlueJ
public class strchar
{
public static void main()
{
char ch1 = 'A'; char ch2 = 'a'; char ch3 = '5'; char ch4 = ' ';
System.out.println("ch1 is " + ch1);
System.out.println("ch2 is " + ch2);
System.out.println("ch3 is " + ch3);
System.out.println("ch4 is " + ch4);
System.out.println("ch1 is Digit " + Character.isDigit(ch1));
System.out.println("ch3 is Digit " + Character.isDigit(ch3));
System.out.println("ch1 is Letter " + Character.isLetter(ch1));
System.out.println("ch3 is Letter " + Character.isLetter(ch3));
System.out.println("ch1 is Letter or Digit " + Character.isLetterOrDigit(ch1));
System.out.println("ch3 is Letter or Digit " + Character.isLetterOrDigit(ch3));
System.out.println("ch4 is Letter or Digit " + Character.isLetterOrDigit(ch4));
System.out.println("ch1 is LC " + Character.isLowerCase(ch1));
System.out.println("ch1 is UC " + Character.isUpperCase(ch1));
System.out.println("ch4 is sp " + Character.isSpaceChar(ch4));
System.out.println("ch3 is sp " + Character.isSpaceChar(ch3));
System.out.println("ch1 to LC " + Character.toLowerCase(ch1));
System.out.println("ch2 to UC " + Character.toUpperCase(ch2));
}}
Assignment
1. Write a program in java to input a string and print capital letters and small letters sepeartely.
Example: Input: Computer Application
Output: capital letters : CA
small latters : omputerpplication
2. 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
3. Write a program in Java to accept a string and display the number upper case, number of
lower case, number of special characters and number of digits present in the string.
Sample Input : Jamshedpur-831005
Output : No. of upper case characters = 1
No. of lower case characters = 9
No. of special characters = 1
No. of digits = 6
_________________________________________________________________________________
5 String Methods BlueJ
1. Give the output for the following statements:
String x = "hello"; String y = "world"; String z = “Hello”;
a System.out.prinln(x+y);
b System.out.println(x.length());
c System.out.println(x.charAt(3));
d System.out.println(x.equals(y));
e System.out.println(x + “ “ + y);
f System.out.println(y.length());
g System.out.println(y.endsWith(“o”));
h System.out.println(x.lastIndexOf(‘l’));
i System.out.println(x.equals(z));
j System.out.println(x.equalsIgnoreCase(z));
2. What will be the output for the following program segment?
String S1 = "Hi"; String S2 = "Hi"; String S3 = "there"; String S4 = "HI";
a System.out.println(S1 + "equals" + S2 + "→" + S1.equals(S2));
b System.out.println(S.substring(pos, pos1));
c System.out.println(S.charAt(8));
d System.out.println(S.indexOf("G"));
_________________________________________________________________________________
6 String Methods BlueJ
4. What do the following functions return for :
String x = “Guten”; String y = “Morgan”; String z= x + “ “ +y;
a System.out.println(z);
b System.out.println(z.length( ));
c System.out.println(z.substring(4,8));
d System.out.println(z.charAt(8));
e System.out.println(z.indexOf(‘g’));
f System.out.println(x.toUpperCase( ));
5. Write java statements if str=“Object Oriented Programming is at the core of Java.” to a String.
a Write a code, which replaces the letters O with X.
_________________________________________________________________________________
7 String Methods BlueJ
7. 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)); ______________________
8. State the data type and values of a and b after the following segment is executed:
String s1= “Computer”, s2=“Applications”;
a = (s1.compareTo(s2)); _____________
b = (s1.equals(s2)); ______________
12. 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); _______________
_________________________________________________________________________________
8 String Methods BlueJ
13. What will be the output when the following code segments are executed?
i. String s = “1001”;
int x = Integer.valueOf(s);
double y = Double.valueOf(s);
System.out.println(“x=” +x); _______________________
System.out.println(“y=” +y); _______________________
ii. System.out.println(“The king said \”Begin at the beginning!\” to me);
_____________________________________________________
_________________________________________________________________________________
9 String Methods BlueJ
(viii)String s= "Today is Test";
System.out.println(s.indexOf('T')); ___________________
System.out.println(s.substring(0,7)+ " "+ "Holiday"); ____________________
(ix) char ch=’F’;
int m=ch; m=m+5; System.out.println(m+ “ ”+ch); _____________
18. State the data type and value of res after the following is executed:
char ch = '9';
res = Character.isDigit(ch); __________________
19. Give the output of the following code:
String A= "26", B= "100" ;
String D=A+B+ "200";
int x= Integer.parseInt(A);
int y=Integer.parseInt(B);
int d=x+y;
System.out.println("Result 1=" +D); _____________________
System.out.println("Result 2="+d); ______________________
g Compare city with “Ghaziabad” and store the Boolean result in variable bool.
h Print the first position of first occurrence of the letter ‘B’ in string City.
i Print the position of last occurrence of the substring “ DEL”in the string str1.
j Compare name with “KARAM ARORA” ignoring the case matching. Store the
Boolean result in variable bln.
L Remove all leading and trailing white spaces from string stored in variable stng.
*********************************************
_________________________________________________________________________________
11 String Methods BlueJ