String Function Worksheet 2023 24

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

Academic year 2023-2024

Std. XI Subject : Computer Application


Worksheet – Library Classes (String)
Library classes
Predefined classes of Java which get included in an application program itself are called the Library classes. They are
built-in java classes; they have member variables, constructor and methods. These classes are contained in Packages.
Some of the commonly used packages are : java.lang, java.util, java.io, java.awt, javax.swing, java.applet,
java.net, java.beans, java.rmi, java.text, javax.servlet.http.
Note : Lang package is evoked automatically, that is even if you do not import the package.

Most used accessor methods of String Class(lang package)


Functions Purpose
int length() Returns length of the string.
String toUpperCase() Converts string to capital letters
String toLowerCase() Converts string to small letters
String replace(oldChar, newChar) Replaces old character with new character
String trim() Trims the white spaces both the ends of the string
boolean equals(str) Returns true if both the string are equal
boolean equalsIgnoreCase(str) Returns true if both the string are equal
char charAt(indexno) Returns the character at specified index
int compareTo(str1, str2) Compares two strings in alphabetical order.
int compareToIgnoreCase (str1,
Compares two strings in alphabetical order with ignoring the case.
str2)
String concat(str) Adds one string to the other.
Extract a part of the string from the specified index no. to the last
String substring(beginindexno)
character.
String substring(beginindexno, Extract a part of the string from the specified beginindex no. to the
lastindexno) lastindexno.
String valueOf(number) Return string representation of the passed argument (int, float)
boolean endsWith(str) Returns true if ends with the specified letter
boolean startsWith(str) Returns true if starts with the specified letter
String toString() Returns the string itself.
int lastIndexOf(char) Returns index value of the last occurrence of the given letter.
int indexOf(char) Returns index value of the first occurrence of the given letter.
EXAMPLE :
public class sample
{
public static void main( )
{
String str1 = "My String Methods";
String str2 = " Using Library Classes ";
int x = 5;
System.out.println("Str1 is " + str1);
System.out.println("Str2 is " + str2);
System.out.println("Ends With s is " + str1.endsWith("s"));
System.out.println("Ends With . is " + str1.endsWith("."));
System.out.println("Starts With M is " + str1.startsWith("M"));

_________________________________________________________________________________
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(S1 + "equals" + S3 + "→" + S1 .equals(S3));

c System.out.println(S1 + "equals" + S4 + "→" + S1 .equals(S4));

d System.out.println(S1 + "EqualIgnoreCase" +S4 + "→" +


S1.equalsIgnoreCase(S4));

3. Give the output for the following statements :


String S = "STRING HANDLING"; int pos = 2; int pos1 = 5;
a System.out.println(S.length( ));

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.

b Change the string to uppercase.

c Change the string to lowercase.

d Extract the first 10 characters from the string.

e Display the length of the string.

6. What will be the output of following java expression:-


a char ch=”DEMO”.charAt(2);

b int b="PROGRAMMING IS FUN".length();

c String s1= “Good”; String s2=s1; s1 += “Morning”; System.out.println(s1 + “\n”+s2);

d char c; String s= “SYSTEM”; int post=2; c=s.charAt(post); System.out.println(c);

e String s = new String("abc"); System.out.println(s.toUpperCase());

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

9. What will the following code output:


String s= “malayalam”;
System.out.println(s.indexOf(‘m’)); ______________
System.out.println(s.lastIndexOf(‘m’)); _______________

10. State the values stored in variables str1 and str2


String s1 = "good"; String s2 = "world matters";
String str1 = s2.substring(5).replace('t','n'); _____________
String str2 = s1.concat(str1); __________________

11. State the output of the following program segment:


String str1 = “great”; String str2 = “minds”;
System.out.println(str1.substring(0,2).concat(str2.substring(1))); _______________
System.out.println((“WH”+(str1.substring(2).toUpperCase( )))); ________________

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

_____________________________________________________

14. What is the value stored in variable res given below :


double res = Math.pow("345".indexOf('5'),3); ________________
15. State the data type and the value of y after the following is executed :
char x = '7'; y = Character.isLetter(x); __________________
16. State the output of the following program segment is 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)); ___________________
17. Give the output of the following string functions:
(i) "ACHIEVEMENT".replace('E', 'A') _____________________
(ii) "DEDICATE".compareTo("DEVOTE") _____________________
(iii) String s1 = "phoenix";
String s2 = "island";
System.out.println(s1.substring(0).concat(s2.substring(2))); _________________________
System.out.println(s2.toUpperCase()); _________________________
(iv) “MISSISSIPPI”.indexOf(‘S’) + “MISSISSIPPI”.lastIndexOf(‘I’) ___________________
(v) “CABLE”.compareTo(“CADET”) ___________________
(vi) System.out.println("Incredible"+"\n"+"world"); ___________________
(vii) String p = "20", q = "19";
int a = Integer.parseInt(p);
int b = Integer.valueOf(q);
System.out.println(a + "" + b); ___________________

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

20. 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”)); ……………………………..

19. Write the output of the following:


(i) System.out.println (Character.isUpperCase(‘R’)); _____________________
(ii) System.out.println(Character.toUpperCase(‘j’)); _____________________

22. 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))); __________________


_________________________________________________________________________________
10 String Methods BlueJ
23. Write the java statements to perform the following tasks :

a Initialize a variable city to store “DELHI”.

b Accept the address from the user.

c Print the number of character in the above entered address.

d Concatenate City with Address and store as string PostalAddress.

e Extract 10 characters from 1st character to 10th of PostalAddress.

f Extract the last character of PostalAddress.

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.

K Replace all occurances of character ch1 by ch2 in the string str.

L Remove all leading and trailing white spaces from string stored in variable stng.

*********************************************

_________________________________________________________________________________
11 String Methods BlueJ

You might also like