String Notes Julyl 2025
String Notes Julyl 2025
For example
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
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
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
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
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
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
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.
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.
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
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 .
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.
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
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
“School”.equalsIgnoreCase(“Schol”) false
11. Input two strings. Check two strings are equal 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
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
8 b) replace function
This function replaces all the occurrence of the given word by another word.
Syntax
4. st.replace(word1, word2)
Example
Syntax
st1.concat(st2)
Example
1. “Hello”.concat(“Bye”) HelloBye
2. “Hello”.concat(“ Friends”) Hello Friends
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
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 :
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 :
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.
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)