String Handling

Section B

2005

Question 5
Write a program to input a string, calculate the total number of characters and vowels present in the string and also reverse it:
Example INPUT:
Enter string : SNOWY
OUTPUT:
Total number of characters : 5
Number of Vowels : 1
Reverse string: YWONS[15]

import java.util.*;
public class Reverse
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a string: ");
String a=sc.nextLine();
a=a.toUpperCase();
int l=a.length(); //To find number of characters of the string
int c=0;
String rev=""; //To store reverse
//loop to count vowels and reverse the string
for(int i=l-1;i>=0;i--)
{
char ch=a.charAt(i);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
c++;
rev=rev+ch;
}
System.out.println("Total number of characters : "+l);
System.out.println("Number of Vowels : "+c);
System.out.println("Reverse string : "+rev);
}
}

Variable Description

VariableTypeDescription
aStringTo store input string
lintTo store length of the string
cintCounter variable
iintLoop variable
revintTo store reverse

2006

Question 6
Consider the following statement:-
"January 26 is celebrated as the Republic Day of India".
Write a program to change 26 to 15, January to August, Republic to Independence and finally print "August 15 is celebrated as the Independence Day of India". [15]

import java.util.*;
public class Replace
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string: ");
String a=sc.nextLine();
String ans;
do
{
System.out.print("Enter the word to find: ");
String find=sc.nextLine();
System.out.print("Enter the word to replace: ");
String repl=sc.nextLine();
a=a.replace(find,repl);
System.out.print("Continue? (y/n): ");
ans=sc.next();
}
while(ans.equalsIgnoreCase("y"));
System.out.println(a);
}
}

2007

Question 7
Write a program to enter a sentence from the keyboard and count the number of times a particular word occurs in it. Display the frequency of the search word.
Example:
INPUT:
Enter a sentence : the quick brown fox jumps over the lazy dog.
Enter a word to be searched : the
OUTPUT: Searched word occurs : 2 times. [15]

import java.util.*;
public class Count
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a sentence : ");
String a=sc.nextLine();
System.out.print("Enter a word to be searched : ");
String s=sc.nextLine();
a=a+" ";
String w=“”;
int c=0;
for(int i=0;i< a.length();i++)
{
char ch=a.charAt(i);
if(ch!=' ')
w+=ch;
else
{
if(w.equalsIgnoreCase(s))
c++;
w= “”;
}
}
System.out.println("Searched word occurs : "+c+" times.");
}
}

Question 9
Write a program using a method Palin(), to check whether a string is a Palindrome or not. A Palindrome is a string that reads the same from left to right and vice versa.
E.g. MADAM, ARORA, ABBA, etc. [15]

import java.util.*;
public class Palindrome
{
public static void Palin(String a)
{
a=a.toUpperCase();
int l=a.length();
int j=l-1;
boolean flag=false;
for(int i=0;i
{
if(a.charAt(i)!=a.charAt(j--))
{
flag=true;
break;
}
if(flag==false)
System.out.println("The string is palindrome.");
else
System.out.println("The string is not palindrome.");
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string : ");
String a=sc.nextLine();
Palin(a);
}
}
}

2008

Question 5
Write a program to input a string and print out the text with the uppercase and lowercase letters reversed, but all other characters should remain the same as before.
Example: INPUT: WelComE TO School
OUTPUT : wELcOMe to sCHOOL [15]

import java.util.*;
public class ReverseCase
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String s=sc.nextLine();
String st="";
for(int i=0;i< s.length();i++)
{
char c=s.charAt(i);
if(c>='A'&&c<='Z') //if(Character.isUpperCase(c))
st=st+(char)(c+32); //st=st+Character.toLowerCase(c);
else if(c>='a'&&c<='z') // else if(Character.isLowerCase(c))
st=st+(char)(c-32); //st=st+Character.toUpperCase(c)
else
st=st+c;
}
System.out.println(st);
}
}

2009

Question 6
Write a program to input a sentence and print the number of characters found in the longest word of the given sentence.
For example if S=“India is my country” then the output should be 7. [15]

import java.util.*;
public class LongestWord
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence");
String s=sc.nextLine();
s=s+" ";
String w="",longest="";
int longlength=0;
for(int i=0;i< s.length();i++)
{
char ch=s.charAt(i);
if(ch!=' ') //to locate word by checking space
w+=ch; //concatenating each character of a word
else
{
if(w.length()>longlength) //to find longest word)
{
longest=w;
longlength=w.length();
}
w=""; //to start the next word
}
}
System.out.println("Longest word: "+longest);
System.out.println("Its length: "+longlength);
}
}

2010

Question 9
Write a program to input a string in uppercase and print the frequency of each character.
Example: Input: COMPUTER HARDWARE [15]
Output:
CHARACTERSFREQUENCY
A2
C1
D1
....
W1

import java.util.*;
public class Frequency
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence in uppercase: ");
String s=sc.nextLine();
int c;
System.out.println("CHARACTERS\tFREQUENCY");
for(char i='A';i<='Z';i++)
{
c=0;
for(int j=0;j< s.length();j++)
{
if(s.charAt(j)==i)
c++;
}
if(c>0)
System.out.println(i+"\t\t"+c);
}
}
}

2011

Question 7
Write a program to accept a word and convert it into lowercase if it is uppercase, and display the new word by replacing only the vowels with the character following.
Example:
Sample input: computer
Output: cpmpvtfr [15]

import java.util.*;
class Convert
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a word");
String a=sc.nextLine();
a=a.toLowerCase(); //To convert into lower case
String st="";
for(int i=0;i< a.length();i++)
{
char ch=a.charAt(i); //extracting each character
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
ch=(char)(ch+1); //replacing vowels with following character
st=st+ch; //concatenates characters to string
}
System.out.println(st);
}
}

2012

Question 6
Write a program to accept a string. Convert the string to uppercase. Count and output the number of double letter sequences that exist in the string.
Sample Input: “SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE”
Sample Output: 4 [15]

import java.util.*;
public class DoubleLetter
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence");
String a=sc.nextLine();
a=a.toUpperCase();
int c=0;
for(int i=0;i< a.length()-1;i++)
{
if(a.charAt(i)==a.charAt(i+1))
c++;
}
System.out.println(c);
}
}

2013

Question 6
Write a program that encodes a word into piglatin. To translate a word into piglatin, convert the word into uppercase and then place the first vowel of the original word as the start of the new word along with the remaining alphabets. The alphabets present before the vowel being shifted towards the end followed by “AY”.
Sample input (1) : London, Sample output(1) : ONDONLAY
Sample input (2) : Olympics, Sample output(2) : OLYMPICSAY [15]

import java.util.*;
public class Piglatin
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a word");
String w=sc.nextLine();
w=w.toUpperCase();
String s1,s2,s3=w;
for(int i=0;i< w.length();i++)
{
char ch=w.charAt(i);
if(ch== 'A'|| ch== 'E'|| ch== 'I'|| ch== 'O'|| ch== 'U')
{
s1=w.substring(0,i);
s2= w.substring(i);
s3=s2+s1+"AY";
break;
}
}
System.out.println(s3);
}
}

2014

Question 6
Write a program to accept a full path and file name as given below. Using library functions, extract and output the file path, file name and file extension seperatly as shown.
Input: C:\Users\admin\Pictures\flower.jpg
Output:
Path: C: \Users\admin\Pictures\
File name: flower
Extension: jpg [15]

import java.util.*;
public class Strings
{
public static void main( )
{
Scanner sc=new Scanner(System.in);
String a=sc.nextLine();
String p,s,fn,e;
int n=a.lastIndexOf('\\');
p=a.substring(0,n+1);
s=a.substring(n+1);
n=s.indexOf('.');
fn=s.substring(0,n);
e=s.substring(n+1);
System.out.println("Path: "+p);
System.out.println("File name: "+fn);
System.out.println("Extension: "+e);
}
}

2015

Question 7
Design a class to overload a function Joystring() as follows:
(i) void Joystring(String s,char ch1, char ch2) that replaces ch1 with ch2 in the given string and prints the new string.
Example:
Input value of s=”TECHNALAGY”
ch1=’A’
ch2=’O’
Output: “TECHNOLOGY”
(ii) void Joystring(String a) that prints the position of the first space and the last space of the given string.
Example:
Input value of s=”Cloud computing means Internet based computing”
Output: First index : 5
Last index : 36
(iii) void Joystring(String s1, String s2) that combines the two strings with a space between them and prints the resultant string.
Example:
Input value of s1=”COMMON WEALTH”
Input value of s2=”GAMES”
Output: COMMON WEALTH GAMES
(Use library functions) [15]

import java.util.*;
public class OverLoad
{
public static void Joystring(String s, char ch1, char ch2)
{
s=s.replace(ch1,ch2);
System.out.println(s);
}
public static void Joystring(String s)
{
int a=s.indexOf(' ');
int b=s.lastIndexOf(' ');
System.out.println("First Index: "+a);
System.out.println("Last Index: "+b);
}
public static void Joystring(String s1, String s2)
{
String s3=s1.concat(" ".concat(s2));
System.out.println(s3);
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string and two characters for replacement");
String s1= sc.nextLine();
char ch1=sc.next().charAt(0);
char ch2=sc.next().charAt(0);
Joystring(s1,ch1,ch2);
System.out.println("Enter a string to find first and last indices of space");
String s2= sc.nextLine();
Joystring(s2);
System.out.println("Enter two strings to combine");
String s3= sc.nextLine();
String s4= sc.nextLine();
Joystring(s3,s4);
}
}

2016

Question 6
Special words are those words which starts and ends with the same letter.
Examples: EXISTENCE WINDOW
Palindrome words are those words which read the same from left to right and vice versa.
Examples: MALAYALAM LEVEL
All palindrome are special words, but all special words are not palindromes.
Write a program to accept a word, check and print whether the word is a palindrome or only special word. [15]

import java.util.*;
public class Words
{
public static void main( )
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter a word: ”);
String a=sc.nextLine();
a=a.toUpperCase();
int l=a.length();
String rev=“”;
for(int i=l-1;i>=0;i- -)
rev=rev+a.charAt(i);
if(a.equals(rev))
System.out.println(“Palindrome”);
else if(a.charAt(0)==a.charAt(l-1))
System.out.println("Special but not Palindrome");
else
System.out.println(“Neither Special nor Palindrome”);
}
}

2017

Question 8
Design a class to overload a function check() as follows:
(i) void check(String str, char ch) – to find and print the frequency of a character in a string.
Example:Input: str=“success” ch= ‘s’
Output: Number of s present is = 3
(ii) void check(String s1) – to display only vowels from string s1, after converting it to lower case
Example: Input: s1= “computer” Output: o u e [15]

public class Strings
{
public static void check(String str, char ch)
{
str=str.toLowerCase();
int c=0;
for(int i=0;i< str.length()-1;i++)
{
if(str.charAt(i)==ch)
c++;
}
System.out.println("Number of ”+ch+” preseent is = "+c);
}
public static void check(String s1)
{
s1=s1.toLowerCase();
for(int i=0;i< s1.length()-1;i++)
{
char ch= s1.charAt(i);
if(ch==’a’|| ch==’e’|| ch==’i’|| ch==’o’|| ch==’u’)
System.out.println(ch+" ");
}
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string and a character to find frequency");
String str=sc.nextLine();
char ch= sc.next().charAt(0);
check(str,ch);
System.out.println("Enter a string to display vowels");
String s=sc.nextLine();
check(s);
}
}
}

2018

Question 6
Write a program in Java to accept a string in lower case and change the first letter of every word to upper case. Display the new string.
Sample input: we are in cyber world.
Sample output: We Are In Cyber World [15]

import java.util.*;
public class Strings
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String a=sc.nextLine();
a=a.toLowerCase();
a=a+“ ”;
String w=“”,s,ns=“”;
for(int i=0;i< a.length();i++)
{
char ch=a.charAt(i);
if(ch!=‘ ’)
w+=ch;
else
{
s=w.substring(0,1);
s=s.toUpperCase()+w.substring(1);
ns+=s+“ ”;
w=“”;
}
}
a=ns.trim();
System.out.println(a);
}
}

Question 8
Write a menu driven program to display the pattern as per user’s choice.
Pattern 1Pattern 2
ABCDEB
ABCDLL
ABCUUU
ABEEEE
A
For an incorrect option, an appropriate error message should be displayed. [15]

import java.util.*;
public class MenuDriven
{
public static void pattern1()
{
String s=“ABCDE”;
int l= s.length();
for(int i=l-1;i>=0 i--)
{
for(int j=0;j<=i;j++)
System.out.print("s.charAt(i)");
System.out.println("");
}
}
public static void pattern2()
{
String s=“BLUE”;
int l= s.length();
for(int i=0;i< l;i++)
{
for(int j=0;j<=i;j++)
System.out.print("s.charAt(i)");
System.out.println("");
}
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Menu");
System.out.println("1 Pattern1");
System.out.println("2 Pattern2");
System.out.print("Enter your choice: ");
int ch=sc.nextInt();
switch(ch)
{
case 1: pattern1();
break;
case 2: pattern2();
break;
default : System.out.println("Wrong choice. Try again!");
}
}
}

2019

Question 8
Write a program to input a sentence. Convert it into uppercase, count and display the total number of words starting with letter ‘A’.
Sample input: ADVANCEMENT AND APPLICATION OF INFORMATION TECHNOLOGY ARE EVER CHANGING
Sample output: Total number of words starting with letter ‘A’= 4. [15]

import java.util.*;
public class Strings
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence");
String s= sc.nextLine();
s=s.toUpperCase();
s=“ ”+s;
int c=0;
for(int i=0;i< s.lenghth();i++)
{
if(s.charAt(i)==‘ ‘&&s.charAt(i+1)==‘A‘)
c++;
}
System.out.println("Total number of words starting with letter ‘A’= "+c);
}
}

2020

Question 6
Write a program to input a sentence and convert it into uppercase and display each word in a separate line.
Example: Input : India is my country
Output :
INDIA
IS
MY
COUNTRY [15]

import java.util.*;
public class Strings
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence");
String s= sc.nextLine();
s=s.toUpperCase();
s=s+" ";
String w="";
for(int i=0;i
{
char ch=s.charAt(i);
if(ch!=' ')
w+=ch;
else
{
System.out.println(w);
w="";
}
}
}
}