programs of String handling
programs of String handling
Question 1
Define a class to accept two strings, convert them into uppercase, check and display whether
two strings are equal or not, if the two strings are not equal, print the string with the highest
length or print the message both the strings are of equal length.
import java.util.*;
public class checkstr
{
public static void main()
{
String s1,s2,su1, su2;
}}
Question 2:
Write a program in Java to input a word, convert the word in upper case letters. Check and print
whether the word is a Palindrome word or not.
“Palindrome words are those words which read the same from left to right and vice versa.”
Example : CIVIC, LEVEL, MADAM etc.
Program 8:
// program for palindrome
import java.util.*;
public class palindrome
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the word");
String s = sc.next();
int l = s.length();
String ns ="";
char chr;
for(int i = l-1; i>=0 ; i--)
{
chr=s.charAt(i);
ns=ns+chr;
}
if(s.equalsIgnoreCase(ns))
System.out.println(s+ "is a palindrome");
else
System.out.println(s+ "is not a palindrome");
}}
Question 3:
Write a program in Java to input a String and print the frequency of each letter present in the String.
Input: Apple
Output:
A 1
P 2
L 1
E 1
import java.util.*;
public class frequency
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter String");
String s = sc.nextLine();
int cf[] = new int[26];
int i;
s= s.toUpperCase();
System.out.println(s);
int l= s.length();
for( i =0; i<l; i++)
{
char ch = s.charAt(i);
if(Character.isLetter(ch))
cf[ch-'A']++;
}
System.out.println("Frequency of characters");
for(i=0; i<l; i++)
{
if(cf[i] != 0)
System.out.println((char)(i+'A') + "\t\t" + cf[i]);
}
}
}
Question 4:
Write a program in Java to enter a sentence. Find and print how many words are starting
with letter ‘A’.
import java.util.*;
public class WordsWithLetterA
{
public static void main() {
Scanner in = new Scanner(System.in);
System.out.println("Enter a string: ");
String str = in.nextLine();
str = " " + str; //Add space in the begining of str
int c = 0;
int len = str.length();
str = str.toUpperCase();
for (int i = 0; i < len ; i++) {
if (str.charAt(i) == ' ' && str.charAt(i + 1) == 'A')
c++;
}
System.out.println("Total number of words starting with letter 'A' = " + c);
}}
Question 5:
Write a program in Java to input a full name of a person. Display the name with the initials and
Surname.
Input: Subhash Chand Bose
Output: S. C. Bose
Question 6:
Write a program to input a word and print its corresponding Piglatin. To translate a word into a
Piglatin, convert the word into upper case and place the first vowel of the original word as the start of
the new word along with the remaining alphabets. The letters present before the vowel being shifted
towards the end followed by “AY”.
Input: London
Output: ONDONLAY
import java.util.*;
public class piglatin
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("enter word");
String s = sc.next();
String ns= "";
s = s.toUpperCase();
int l = s.length();
char ch;
int i,p=-1;
for(i=0; i<l; i++)
{
ch= s.charAt(i);
if(ch=='A' || ch== 'E' || ch== 'I' || ch== 'O' || ch== 'U')
{p=i;
break;}
}
if(p>=0)
{
String n1= s.substring(p);
String n2 = s.substring(0,p);
ns = ns+n1+n2+"AY";
System.out.println("Piglatin String is"+ns);
}
else
{
System.out.println("no vowel found");
}}}
Question 7:
Define a class in Java to accept two strings, convert them into uppercase. Find and print whether
string2 is a part of string1 or not. If string 2 is a part of string1 then print the index of string1 from
where string2 begins.
Example 1:
string1 = “acknowledge”
string2 = “now”
Output: NOW IS A PART OF ACKNOWLEDGE
It begins at index 3
Example 1:
string1 = “acknowledge”
string2 = “won”
Output: NO, WON IS NOT A PART OF ACKNOWLEDGE
import java.util.*;
class substr
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int p;
String str1, str2;
System.out.println("Enter two strings");
str1= sc.nextLine();
str2 = sc.nextLine();
str1 = str1.toUpperCase();
str2 = str2.toUpperCase();
p= str1.indexOf(str2);
if(p>=0)
{System.out.println(str2+ " IS A PART OF " + str1 );
System.out.println("It begins at index " +p );}
else
System.out.println(str2+ " IS NOT A PART OF " + str1);
}}
Question 8: Define a class in Java to accept a string and convert it into uppercase. Find and print
whether in the string any letter is repeated in sequence(double letter) or not.
Example 1: string = RABBIT
Output: Yes letters are repeated in sequence.
Example 2: string = PORCUPINE
Output: No letters are not repeated in sequence.
import java.util.*;
public class repeat
{
public static void main()
{
Scanner sc= new Scanner(System.in);
int i,c=0;
char ch1, ch2;
String str;
System.out.println("Enter String");
str = sc.nextLine();
str= str.toUpperCase();
int ln = str.length();
for(i=0; i<ln-1; i++)
{
ch1= str.charAt(i);
ch2= str.charAt(i+1);
if(ch1== ch2)
c++;
}
if(c>0)
System.out.println("Yes letters are repeated in sequence");
else
System.out.println("No letters are not repeated in sequence");
}}
Question 9: Define a class in Java to accept a sentence and find out the length of each word in that
sentence.
Example : computer is fun
Output:
computer == The size is 8 characters
is == The size is 2 characters
fun == The size is 3 characters
import java.util.*;
Question 10: Define a class in Java to input a sentence and find out the longest word in that sentence.
Input: Computer is my favourite subject
Output:
computer == The size is 8 characters
is == The size is 2 characters
my == The size is 2 characters
favourite == The size is 9 characters
subject == The size is 7 characters
longest string is favourite having 9 characters
import java.util.*;
public class longest
{
public static void main()
{
char ch;
int len=0, max=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string");
String s = sc.nextLine();
s= s+" ";
String str = "";
int l = s.length();
String ns = "";
for(int i = 0; i<l ; i++)
{
ch = s.charAt(i);
if (ch != ' ')
{ System.out.print(ch);
len = len+1;
ns = ns+ch;
}
if(ch == ' '){
System.out.print( " == The size is " +len+ " characters");
if(len>max){
max=len;
str = ns;
}
len=0;
ns = "";
System.out.print("\n");}
}
System.out.println("longest string is "+str +" having " + max+ " characters ");
}}