Those students are absent for classes and incomplete notes, Complete the notes on
String Manipulation and submit on Tuesday 15/07/2025 without fail.
String Manipulation in Java
What is String ?
A String is group of characters which are enclosed in a pair of double Quotes.
( Characters can be alphabets, digits or special characters )
For example
“Hello Friends “, “BETHANY SCHOOL ”, “Anil”, “Vizag” “Rose “, “533”, “ak32bn6”, “a
%@5wQ”, etc
Function are two types
1) String Functions
2) Character Functions
String Functions
1. toUpperCase()
This function is used to convert lower case letters to Upper case letters.
The return value is string.
Syntax
string.toUpperCase()
Examples
1. “hello”.toUpperCase() = HELLO
2. “I am Fine”.toUpperCase() = I AM FINE
3. “30 th Dec 2020 “ = 30 TH DEC 2020
2. toLowerCase()
This function is used to convert Upper case letters to Lower case letters.
The return value is string.
Syntax
string.toLowerCase()
Examples
1. “HELLO”.toLowerCase() = hello
2. “I Am Fine”.toLowerCase() = i am fine
3. trim()
This function is used to remove spaces or blanks at beginning and end of the string. The
return value is string.
Syntax
string.trim()
Examples
1. “ Today “.trim() = Today
2. “ I am Fine ”.trim() = I am Fine
3. String st1= “ Today “;
String st2 = st1.trim();
Sopln(st1);
Sopln(st2);
Today
Today
4. length()
This function is used to count number of characters in a given string and returns value in
integer.
or
The function returns number of characters in a given string and the return value in Integer.
string.length()
Examples
1. “School”.length() = 6
2. “I am Fine”.length() = 9
3. “23, (/Ac *+”.length() = 10
Note : In strings space/blank is a character. ( length function will count space as 1
character )
“1235.78”.length() + “Covid 19”.length() - “virus”.length() =
7+8–5
10
5. charAt()
This function return a single character at a given position or index of the input string. The
return value is character.
Syntax
string.charAt(n)
Where n is the position or index of the character. Here count of characters will be from 0.
(zero)
Examples
1. “COMPUTER”.charAt(3) = P
2. “COMPUTER”.charAt(6) = E
3. “I am Fine”.charAt(7) = n
4. “I am Fine”.charAt(0) = I
5. “Hello”.charAt(7)
This example will display error message String index out of range 7
PROGRAMS
Write program to display word COMPUTER in the following format.
import java.util.*;
public class Pattern
{
public static void main ( )
{
Scanner in = new Scanner(System.in);
String st;
int i , l ;
char ch;
st = “HELLO”;
l = st.length();
for(i =0 ; i<l ; i++)
{
ch= st.charAt(i);
System.out.println(ch);
} // for loop
} // public
} // class
2. Write a program to display HELLO in the following pattern.
H
HE
HEL
HELL
HELLO
import java.util.*;
public class Pattern
{
public static void main ( )
{
Scanner in = new Scanner(System.in);
String st;
int i , j, l ;
char ch;
st = “HELLO”;
l = st.length();
for(i =0 ; i<l ; i++)
{
for(j =0 ; j<=i ; j++)
{
ch= st.charAt( j );
System.out.print(ch);
} // j loop
System.out.println();
} // I loop
} // public
} // class
3. Write a program to display HELLO in the following pattern.
H
EE
LLL
LLLL
OOOOO
import java.util.*;
public class Pattern
{
public static void main ( )
{
Scanner in = new Scanner(System.in);
String st;
int i , j, l ;
char ch;
st = “HELLO”;
l = st.length();
for(i =0 ; i<l ; i++)
{
for(j =0 ; j<=i ; j++)
{
ch= st.charAt( i );
System.out.print(ch);
} // j loop
System.out.println();
} // I loop
} // public
} // class
4. Write a program to display HELLO in the following pattern.
HELLO
HELL
HEL
HE
H
import java.util.*;
public class Pattern
{
public static void main ( )
{
Scanner in = new Scanner(System.in);
String st;
int i , j, l ;
char ch;
st = “HELLO”;
l = st.length() -1;
for(i =l ; i>=0 ; i -- )
{
for(j =0 ; j<= i ; j++)
{
ch= st.charAt( j );
System.out.print(ch);
} // j loop
System.out.println();
} // i loop
} // public
} // class
5. Write a program to display HELLO in the following pattern.
HHHHH 6) OLLEH
EEEE OLLE
LLL OLL
LL OL
O O
import java.util.*;
public class Pattern
{
public static void main ( )
{
Scanner in = new Scanner(System.in);
String st;
int i , j, l ;
char ch;
st = “HELLO”;
l = st.length() -1;
for(i =0 ; i<=l ; i ++ )
{
for(j =i ; j<= l ; j++)
{
ch= st.charAt( i );
System.out.print(ch);
} // j loop
System.out.println();
} // i loop
} // public
} // class
import java.util.*;
public class Pattern
{
public static void main ( )
{
Scanner in = new Scanner(System.in);
String st;
int i , j, l ;
char ch;
st = “HELLO”;
l = st.length() -1;
for(i =0 ; i<=l ; i ++ )
{
for(j =l ; j>= i ; j--)
{
ch= st.charAt( j );
System.out.print(ch);
} // j loop
System.out.println();
} // i loop
} // public
} // class
5. Write a program to input a sentence and find number of vowels.
6. Write a program to input a sentence and find number of Consonants.
import java.util.*;
public class Vowels
{
public static void main ( )
{
Scanner in = new Scanner(System.in);
String st;
int i , l , v = 0;
char ch;
System.out.println(“ Enter Any Sentence “);
st=sc.nextLine();
l = st.length();
for(i = 0 ; i<l ;i++ )
{
ch=st.charAt(i);
if ( ch == ‘A’ || ch == ‘E’ || ch == ‘I’ || ch ==’O’ || ch == ‘U’ || ch == ‘a’ ||
ch == ‘e’ || ch == ‘i’ || ch ==’o’ || ch == ‘u’ )
v= v + 1;
} // for
System.out.println(“ Number of Vowels “ + v);
} // public
} // class
7. Write a program to input a sentence and find number of Consonants.
import java.util.*;
public class Consonants
{
public static void main ( )
{
Scanner in = new Scanner(System.in);
String st;
int i , l , c = 0;
char ch;
System.out.println(“ Enter Any Sentence “);
st=sc.nextLine();
l = st.length();
for(i = 0 ; i<l ;i++ )
{
ch=st.charAt(i);
if ( ch != ‘A’ || ch != ‘E’ || ch != ‘I’ || ch !=’O’ || ch != ‘U’ || ch != ‘a’ ||
ch != ‘e’ || ch != ‘i’ || ch !=’o’ || ch != ‘u’ )
c= c + 1;
} // for
System.out.println(“ Number of Consonants “ + c);
} // public
} // class
8. Write a program to input a sentence and find number of vowels, consonants, words, spaces
and number of characters.
import java.util.*;
public class Count
{
public static void main ( )
{
Scanner in = new Scanner(System.in);
String st;
int i , l , v = 0, c = 0, w = 0, s = 0 ;
char ch;
System.out.println(“ Enter Any Sentence “);
st=sc.nextLine();
l = st.length();
for(i = 0 ; i<l ;i++ )
{
ch=st.charAt(i);
if ( ch == ‘A’ || ch == ‘E’ || ch == ‘I’ || ch ==’O’ || ch == ‘U’ || ch == ‘a’ ||
ch == ‘e’ || ch == ‘i’ || ch ==’o’ || ch == ‘u’ )
v= v + 1;
if ( ch==’ ‘)
s= s + 1;
} // for
c= l – v – s ;
w= s+1;
System.out.println(“ Number of Vowels “+v);
System.out.println(“ Number of Consonants “ + c) ;
System.out.println(“ Number of Spaces “+s);
System.out.println(“ Number of Words “ + w) ;
System.out.println(“ Number of Characters “ + l );
} // public
} // class
9 a. Write a program to input a sentence and find the frequency of each vowels.
import java.util.*;
public class Vowels
{
public static void main ( )
{
Scanner in = new Scanner(System.in);
String st;
int i , l , va = 0, ve = 0, vi = 0, vo = 0, vu = 0;
char ch;
System.out.println(“ Enter Any Sentence “);
st=sc.nextLine();
l = st.length();
for(i = 0 ; i<l ;i++ )
{
ch=st.charAt(i);
if ( ch == ‘A’ || ch == ‘a’ )
va++ ;
else if ( ch == ‘E’ || ch == ‘e’ )
ve++;
else if ( ch == ‘I’ || ch == ‘i’ )
vi++;
else if ( ch == ‘O’ || ch == ‘o’ )
vo++;
else if ( ch == ‘U’ || ch == ‘u’ )
vu++;
} // for
System.out.println(“ Frequency of A Vowels “+ va);
System.out.println(“ Frequency of E Vowels “+ ve);
System.out.println(“ Frequency of I Vowels “+ vi);
System.out.println(“ Frequency of O Vowels “+ vo);
System.out.println(“ Frequency of U Vowels “+ vu);
} // public
} // class
9b. Write a program to input a sentence and find the frequency of each
vowels using switch statement .
import java.util.*;
public class Vowels
{
public static void main ( )
{
Scanner in = new Scanner(System.in);
String st;
int i , l , va = 0, ve = 0, vi = 0, vo = 0, vu = 0;
char ch;
System.out.println(“ Enter Any Sentence “);
st=sc.nextLine();
l = st.length();
for(i = 0 ; i<l ;i++ )
{
ch=st.charAt(i);
switch( ch)
{
case : ‘A’ :
case : ‘a’ : va++ ;
break;
case : ‘E’ :
case : ‘e’ : ve++ ;
break;
case : ‘I’ :
case : ‘i’ : vi++ ;
break;
case : ‘O’ :
case : ‘o’ : vo++ ;
break;
case : ‘U’ :
case : ‘u’ : vu++ ;
break;
} // switch
} // for
System.out.println(“ Frequency of A Vowels “+ va);
System.out.println(“ Frequency of E Vowels “+ ve);
System.out.println(“ Frequency of I Vowels “+ vi);
System.out.println(“ Frequency of O Vowels “+ vo);
System.out.println(“ Frequency of U Vowels “+ vu);
} // public
} // class
10. Write a program to input a sentence and display sentence after
removing vowels.
Input : Today is Monday
Output : Tdy s Mndy
import java.util.*;
public class Vowel
{
public static void main ( )
{
Scanner in = new Scanner(System.in);
String st;
int i , l ;
char ch
System.out.println(“ Enter Any Sentence “);
st=sc.next();
l = st.length();
for(i = 0 ; i<l ;i++ )
{
ch=st.charAt(i);
if ( ch != ‘A’ || ch != ‘E’ || ch != ‘I’ || ch !=’O’ || ch != ‘U’ || ch != ‘a’ ||
ch != ‘e’ || ch != ‘i’ || ch !=’o’ || ch != ‘u’ )
System.out.print(ch);
} // for
} // public
} // class
9b. Write a program to input a sentence and display sentence by replacing vowels by * .
Today is thur
T*d*y *s th*r
String st;
int i , l ;
char ch;
System.out.println(“ Enter Any Sentence “);
st=sc.next();
for(i = 0 ; i<l ;i++ )
{
ch=st.charAt(i);
if ( ch == ‘A’ || ch == ‘E’ || ch == ‘I’ || ch ==’O’ || ch == ‘U’ || ch == ‘a’ ||
ch == ‘e’ || ch == ‘i’ || ch ==’o’ || ch == ‘u’ )
System.out.print(‘*’);
else
System.out.print(ch);
} // for
10) Input any string. WP to display characters in alphabetic order.
Example : input BAT output ABT
import java.util.*;
public class Ascending
{
public static void main ( )
{
Scanner in = new Scanner(System.in);
String st ;
int i , j, l ;
char ch;
System.out.println(“ Enter Any String /Word “);
st=sc.next();
l = st.length();
for(i = 65 ; i<=90 ;i++ )
{
for(j =0 ; j<l; j++)
{
ch=st.charAt(j);
if( i == (int)ch || i == (i +32)ch)
System.out.print(ch);
} // j loop
} // I loop
} // public
} // class
11) WP to accept string.Convert the string to upper case.Count and display number of
double letters sequences that exist in the string.
Example
HE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE
import java.util.*;
public class Sequence
{
public static void main( )
{
Scanner sc=new Scanner(System.in);
String st;
char ch,ch1;
int i,l,c=0;
System.out.println("ENTER A SENTENCE");
st=sc.nextLine();
st=st.toUpperCase();
l=st.length()-1;
for(i=0;i<l;i++)
{
ch = st.charAt(i);
ch1 = st.charAt(i+1);
if(ch==ch1)
c++;
} // FOR
System.out.println("NUNBER OF PAIRS="+c);
}// public
} // class
12) WP to accept string.Convert the string to upper case.Check the string is UNIQUE word
or not .
BAT, TABLE, COMPUTE , …… are unique words
RABBIT, HELLO, APPLE , ……. Are not unique words
import java.util.*;
public class Unique
{
public static void main(String args[ ])
{
Scanner sc=new Scanner(System.in);
String st;
int i, l, j, c=0;
char ch1,ch2;
System.out.println("ENTER A WORD");
st=sc.next();
st =st.toUpperCase();
l=st.length()-1;
for(i=0 ;i <l ; i++)
{
ch1=st.charAt(i);
for(j=i+1;j<l;j++)
{
ch2=st.charAt(j);
if(ch1==ch2)
{
c++;
break;
} // if
} // j loop
} // i loop
if(c==0)
System.out.println(st + "IS A UNIQUE WORD");
else
System.out.println(st+ "IS NOT A UNIQUE WORD");
} //public
} // class
13) WP to accept sentence and accept a character. Find the frequency of given alphabet.
Input st = WE ARE LIVING IN COMPUTER WORLD
Input character ch=R
Output = 3
import java.util.*;
public class Count
{
public static void main( )
{
Scanner sc=new Scanner(System.in);
String st;
char ch,ch1;
int i,l,c=0;
System.out.println("ENTER A SENTENCE");
st=sc.nextLine();
st=st.toUpperCase();
System.out.println("ENTER A CHARACTER");
ch =sc.next().charAt(0);
l=st.length()-1;
for(i=0;i<l;i++)
{
ch1 = st.charAt(i);
if(ch==ch1)
c++;
} // FOR
System.out.println("THE FREQUENCY OF LETTER “ + ch + “=" + c);
}// public
} // class
6. equals function
This function is used to compare two strings. If two strings are equal, it returns true else
returns false. The return value of equals function is Boolean value.
Syntax
st.equals(st1)
Examples
“HELLO”.equals(“HELLO”) true
“HELLO”.equals(“Hello”) false
“hi”.equals(“hi”) true
“HI”.equals(“hi”) false
“COMPUTER”.equals(“C OMPUTER”) false
7. equalsIgnoreCase function
This function is used to compare two strings. If two strings are equal, it returns true after
ignoring case else returns false. The return value of equalsIgnoreCase function is Boolean
value.
Syntax
st.equalsIgnoreCase(st1)
Examples
“HELLO”.equalsIgnoreCase(“HELLO”) true
“HELLO”.equalsIgnoreCase (“Hello”) true
“hi”.equalsIgnoreCase (“hi”) true
“HI”.equalsIgnoreCase (“hi”) true
“COMPUTER”.equalsIgnoreCase (“ C OMPUTER”) false
“School”.equalsIgnoreCase(“Schol”) false
11. Input two strings. Check two strings are equal or Not.
import java.util.*; st1 st2
public class Check_Equal HELLO HELLO tru
{ FRIEND friend tru
public static void main ( ) Dog dOG true
Keyboard keychain false
{ Hammer hameer fals
Scanner sc = new Scanner(System.in);
String st1 , st2;
System.out.println(“ Enter Two Strings “);
st1 = sc.next();
st2 = sc.next();
if (st1.equalsIgnoreCase(st2))
System.out.println( “ TWO STRINGS ARE EQUAL “ );
else
System.out.println( “ TWO STRINGS ARE NOT EQUAL “ );
} // public
} // class
12. Write a program to input a word/string and check the word is palindrome or not.
A palindrome is a word, number which reads the same backward as forward are
known as Palindrome.
Example madam, racecar, mom , dad, radar, LIRIL, etc
import java.util.*;
public class Palindrome
{
public static void main ( )
{
Scanner in = new Scanner(System.in);
String st , rev =””;
int i , l ;
char ch;
System.out.println(“ Enter Any Word/ String “);
st=sc.next();
l = st.length();
for(i = 0 ; i<l ;i++ )
{
ch=st.charAt(i);
rev = ch + rev ;
} // I loop end
if (st.equalsIgnoreCase(rev))
System.out.println( st + “ IS A PALINDROME STRING “ );
else
System.out.println( st + “ IS NOT A PALINDROME STRING “ );
} // public
} // class
13. Write a program to input 10 words/strings and display the words which are
Palindrome.
Examples SCHOOL, MADAM, TIGER, MOM, HELLO, DOG, RADAR
Output MADAM, MOM, RADAR
import java.util.*;
public class Palindrome
{
public static void main( )
{
Scanner in = new Scanner(System.in);
String st , rev =””;
int i , ,j , l ;
char ch;
for( i=1; i<=10; i++ )
{
System.out.println(“ Enter Any Word/ String “);
st=sc.next();
l = st.length();
for(j = 0 ; j<l ; j++ )
{
ch=st.charAt(j);
rev = ch + rev ;
} // j loop end
if (st.equalsIgnoreCase(rev))
System.out.println( st + “ ,“ );
rev =”” ;
} // I loop
} // public
} // class
Continuation of String Functions
8 a) replace function
This function replaces all the occurrence of the given character (ch1) by another character
(ch2) .
Syntax
st.replace(ch1, ch2)
Example
1. “RUNNING”.replace(‘N’, ‘K’) RUKKIKG
2. “RUNnInG”.replace(‘n’, ‘d’) RUNdIdG
3. “HELLO”.replace(‘L’,’t’) HEttO
8 b) replace function
This function replaces all the occurrence of the given word by another word.
Syntax
4. st.replace(word1, word2)
Example
1. “Black cat and Black dog”.replace(“Black” , “White”)
White cat and White dog
2. “Black cat and BLACK dog”.replace(“Black” , “White”)
White cat and BLACK dog
3. “black cat and Black dog”.replace(“Black” , “White”)
black cat and White dog
9) concat function
This function is used to join two strings.
Syntax
st1.concat(st2)
Example
1. “Hello”.concat(“Bye”) HelloBye
2. “Hello”.concat(“ Friends”) Hello Friends
Note : + operator is also used to join two or more strings
“Today” + “ Is” + “ Monday”
Today Is Monday
10 a ) substring Function
This function returns substring from a given position or index of the input string.
Function counts the characters from 0. The value is string.
Syntax
st.substring(n);
where n is the position of the character
01 2 34 5 67
1. “COMPUTER”.substring(3) PUTER
2. “COMPUTER”.substring(5) TER
3. “understanding”.substring(5) standing
10 b) substring Function
Syntax
st.substring(n1, n2);
This function returns substring from a given position or index, out of n1 characters leaving
the first n2 characters of the input string. Function counts the characters from 0. The return
value is string.
Example
1. “understanding”.substring(5, 10) stand
2. “understanding”.substring(7, 10) and
3. “PRIME NUMBER”.substring(6, 9) NUM
15) Input any string, Make the string as PIGLATIN word.
trouble = ouble + tr + ay = oubletray
apple = appleay
kite = itekay
import java.util.*;
public class Piglatin
{
public static void main ( )
{
Scanner in = new Scanner(System.in);
String st , st1 =””, st2=””,pl=””;
int i , l ;
char ch;
System.out.println(“ Enter Any Word/ String “);
st=sc.next();
l = st.length();
for(i = 0 ; i<l ;i++ )
{
ch=st.charAt(i);
if ( ch == ‘A’ || ch == ‘E’ || ch == ‘I’ || ch ==’O’ || ch == ‘U’ || ch == ‘a’ ||
ch == ‘e’ || ch != ‘i’ || ch ==’o’ || ch == ‘u’ )
break;
} // I loop end
st1 = st.substring(i);
st2 = st.substring(0, i);
pl = st1 + st2 + “ay” ;
System.out.println( “PIGLATIN WORD IS “ + pl );
} // public
} // class
11 a) indexOf function
The function returns the position of the first occurrence of a given character (ch) of
the input string. The function count the character from 0 position. The return value is
integer.
Syntax
st.indexOf(ch)
Syntax
1. “APPLE”.indexOf(‘P’) 1
2. “understand.indexOf(‘d’) 2
3. “prime number”.indexOf(‘e’) 4
12 b) Syntax
st.indexOf(ch, n)
The function returns position of a given character (ch) after nth position of the input
string. The function count the character from 0 position. The return value is integer.
1. “APPLE”.indexOf(‘P’, 1) 2
2. “understand.indexOf(‘d’, 5) 9
3. “prime number”.indexOf(‘e’, 1) 4
4. “prime number”.indexOf(‘m’, 5) 8
c) Syntax
st.lastindexOf(ch)
The function returns last position of a given character (ch) of the input string. The
function count the character from 0 position. The return value is integer.
1. “APPLE”.lastindexOf(‘P’) 2
2. “understand. lastindexOf(‘d’) 9
3. “prime number”. lastindexOf(‘e’) 10
4. “prime number”. lastindexOf(‘m’) 8
13. startsWith()
The function returns true if the string st1 starts with st2 . This function is a boolean function
which returns true or false.
Syntax
st1.startsWith(st2)
Example :
1. “TODAY IS THURSDAY”.startsWith(“TODAY”) true
2. “TODAY IS THURSDAY”.startsWith(“Today”) false
3. “TODAY IS THURSDAY”.startsWith(“TODY”) false
4. “TODAY IS THURSDAY”.startsWith(“TIME”) false
5. “TODAY IS THURSDAY”.startsWith(“TOD”) true
14. endsWith()
The function returns true if the string st1 ends with st2 . This function is a boolean function
which returns true or false.
Syntax
st1.endsWith(st2)
Example :
1. “TODAY IS THURSDAY”.endsWith(“THURSDAY”) true
2. “TODAY IS THURSDAY”.endsWith(“Thursday”) false
3. “TODAY IS THURSDAY”.endsWith(“THURS”) false
4. “TODAY IS THURSDAY”.endsWith(“DAY”) true
5. “TODAY IS THURSDAY”.endsWith(“D AY”) false
Input any word/string. Write program to check word is unique or not.
import java.util.*;
public class unique
{
public static void main ( )
{
Scanner in = new Scanner(System.in);
String st ;
int i, j, l ,c=0;
char ch,ch1;
System.out.println("Enter a string");
st = sc.next();
st=st.toUpperCase();
l=st.length();
for( i=0; i<l; i++)
{
ch=st.charAt(i);
for( j=0; j<l; j++)
{
ch1=st.charAt(j);
if(ch==ch1)
{
c++;
} // if
} // j loop
} // I loop
if(l==c)
System.out.println(st + "It is unique string");
else
System.out.println( st + "It is not a unique string");
} // public
} // class
Input any sentence. Write a program to display first and last word.
Input : Today is Monday dated 10th August
Output : Today August
import java.util.*;
public class Word
{
public static void main(String arg[ ])
{
Scanner in = new Scanner(System.in);
String st ,st1=””, st2=”” ;
int i , p1, p2 ;
System.out.println("Enter a sentence “);
st = sc.nextLine();
st=st.toUpperCase();
l=st.length()-1;
p1 = st.indexOf(‘ ‘);
p2 = s2.lastindexOf(‘ ‘);
st1 = st.substring(0, p1) ;
st2 = st.substring(p2, l );
System.out.println( st1 + "\t” + st2 );
} // public
} // class
compareTo() Function:
This function compares two string and returns difference in ASCII Values of two strings by
taking first character of two string, if both the characters are same of the two strings,
compareTo function takes the second character of two strings and so on.
Syntax :
s1.compareTo(s2)
If s1>s2 the function returns >=1
If s1<s2 the function returns <=1(i.e -1, -2, -3 , -4 , ……….)
If s1=s2 the function returns = 0