Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
33 views

programs of String handling

The document contains a series of Java programming exercises focused on string handling. Each question presents a specific task, such as checking for palindromes, counting letter frequencies, and identifying the longest word in a sentence. The provided code snippets demonstrate how to implement these tasks using Java's string manipulation capabilities.

Uploaded by

akarshhagarwal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
33 views

programs of String handling

The document contains a series of Java programming exercises focused on string handling. Each question presents a specific task, such as checking for palindromes, counting letter frequencies, and identifying the longest word in a sentence. The provided code snippets demonstrate how to implement these tasks using Java's string manipulation capabilities.

Uploaded by

akarshhagarwal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Class 10: String Handling program

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;

Scanner sc = new Scanner(System.in);


System.out.println("Enter 2 strings");
s1= sc.nextLine();
s2=sc.nextLine();
su1 = s1.toUpperCase();
su2 = s2.toUpperCase();
System.out.println(su1+ " " + su2);
int res = su1.compareTo(su2);
if(res>0)
System.out.println(su1+ " is greater string");
else if(res<0)
System.out.println(su2+ " is greater string");
else
System.out.println("Both strings are equal");

}}
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

// program for printing initials and surname


import java.util.*;
public class surname
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the full name");
String n = sc.nextLine();
n = " "+n;
int pos = n.lastIndexOf(' ');
for( int i =0; i<pos; i++)
{
if(n.charAt(i) == ' ')
System.out.print(n.charAt(i+1)+ ".");
}
System.out.print(n.substring(pos));
}}

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.*;

public class lengthofeachword


{
public static void main()
{
char ch;
int len=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string");
String s = sc.nextLine();
s= s+" ";
int l = s.length();
for(int i = 0; i<l ; i++)
{
ch = s.charAt(i);
if (ch != ' ')
{ System.out.print(ch);
len = len+1;
}
if(ch == ' '){
System.out.print( " == The size is " +len+ " characters");
len=0;
System.out.print("\n");}
}
}}

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

You might also like