STRING CODES WORKSHEET
Find the outputs for the following code segments
(A) If String str = “Red house is the Super house of 2024”
(i) System.out.println( str.charAt(4) + “ ” + str.charAt(9) );
(ii) System.out.println(str.substring(0,10 ) . equalsIgnoreCase (“RED HOUSE”) );
(iii) System.out.println ( (int) “excellent mind blowing”.charAt(3) );
(B) if String str = “excellent mind blowing you all are”
(i) boolean ans = str.startsWith(“Excell”); what is the o/p of ans?
(ii) System.out.println(str.lastIndexOf(‘e’));
(iii) System.out.println(str.indexOf(‘e’));
(iv) System.out.prinltn(str.indexOf(‘U’));
(C) if String str = “all is fair in love and computers”
(i) System.out.println(str.charAt(str.lastIndexOf(‘o’)));
(ii) System.out.println(str.substring(str.indexOf(‘e’));
(D) if String str = “the best house wins”;
(i) String ans = str.replace(“best” , “red”);
Sopln(ans);
(ii) int ch = (int) (str.charAt(2)); int( ?)
(iii) System.out.println( Character.toUpperCase( str.charAt( str.indexOf( (char) 98 ) ) ) );
(E) String str = “Attitude determines your altitude”
[ Write type of error also for 2nd and 3rd bits ]
(i) System.out.println(str.lastIndexOf( ‘z’));
(ii) System.out.println(str.substring(-30)); /StringIndexOutOfBoundsException
(iii) System.out.println(str.substring(7,6));
(iv) String s1 = str.substring(7,7);
String s2 = str.substring(7);
String s3 = s1.concat(s2);
Sopln ( s3);
ASCII CODES
(i) int m; char ch ;
m = 805/10 + 45%8 + 29%13;
Sopln( (char) m);
(ii) char ch = ‘7’;
System.out.println( ( int ) ch );
(iii) char ch1 = ‘a’ , ch2 = ‘A’;
int ans = ch1 + ch2; 65 + 97
System.out.println(ans); OUTPUT : 162
(iv) char ch1 = ‘a’ , ch2 = ‘ ’;
System.out.println ( ch1 + ch2 );
(v) int c1 = 97 , c2 = 32;
char ch = (char)(c1+c2);
Sopln (ch );
Sopn ( (char ) (c1 – c2 ) ); OUTPUT : 97-32 = 65 A
WRITE JAVA STATEMENTS FOR THE FOLLOWING
1) If String str = “2023”; int b = 99;
Write the statement to convert the String value to integer value.
Write the statement to convert the integer value to String value.
WRITE JAVA STATEMENTS FOR THE FOLLOWING STATEMENTS.
EXAMPLE :
To check if the character value ch is in uppercase or not.
boolean ans = Character.isUpperCase(ch);
1) To display the position of the last occurrence of space in a String str.
2) To check if 2 String values are equal or not, not taking cases into consideration
3) To print the first character in a String
4) To check the last character in a String [ HINT : Need to calculate length of the String ]
5) To check if the String value ends with the String value “nd”.
6) To remove all the trailing spaces from a String value str.
7) To replace all occurrence of letter ‘t’ with ‘&’ in String str.
8) To replace all the occurrence of word “end” with “start” in a String str.
9) To convert the entire String value to lowercase.
10) To covert the last character of the String in to uppercase alphabet.
11) To extract a part of the String from positions 4 to 8
12) To extract String from the index/position 3.
13) To convert a Character value into upper case.
14) To check if a Character value is an digit or not.
15) To check if a Character value is a space or not.
16) To concatenate String s1 and S2
17) To print the character from the index/position 5 from String str.
18) To extract String from the index 4 till 9 .
19) To convert the String value str to upper case.
20) To initialize a character variable to null literal
21) To extract String from the a String value starting from 1st position
22) To join two String values.