Board Exam Questions On Strings

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

/*Write a program to input a sentence and convert it into uppercase and count

and display the total number of words starting with a letter ‘A’.*/
import java.util.*;
public class CountA
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String s,w="";
int i,count=0;
char ch;
System.out.println("Enter the string");
s=sc.nextLine();
s=s+" ";
s=s.toUpperCase();
int l=s.length();
for(i=0;i<l;i++)
{
ch=s.charAt(i);
if(ch!=' ')
w=w+ch;
else
{
ch=w.charAt(0);
if(ch=='A')
count++;
w="";
}
}
System.out.println("Number of words starting with A="+count);
}
}

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

import java.util.*;
public class FirstLetter
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String s,w="";
char ch;
System.out.println("Enter the string");
s=sc.nextLine();
s=s+" ";
int l=s.length();
int i;
for(i=0;i<l;i++)
{
ch=s.charAt(i);
if(ch!=' ')
w=w+ch;
else
{
ch=w.charAt(0);
w=Character.toUpperCase(ch)+w.substring(1);
System.out.print(w+" ");
w="";
}
}
}
}

Write a program to input a string and find and print the frequency of a character
in a string.
import java.util.*;
public class CharacterFrequency
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String s;
int count,i,j;
char ch=' ';
System.out.println("Enter the string");
s=sc.nextLine();
int l=s.length();
s=s.toUpperCase();
for(i=65;i<=90;i++)
{
count=0;
for(j=0;j<l;j++)
{
ch=s.charAt(j);
if((char)i==ch)
count++;
}
if(count!=0)
System.out.println((char)i+"="+count);
}
}
}
Enter the string
an apple a day keeps the doctor away
A=6
C=1
D=2
E=4
H=1
K=1
L=1
N=1
O=2
P=3
R=1
S=1
T=2
W=1
Y=2

Write a program to input a string and display only vowels from string si, after
converting it to lower case.
import java.util.*;
public class Vowel
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String si;
int i;
char ch;
System.out.println("Enter the string");
si=sc.nextLine();
int l=si.length();
si=si.toLowerCase();
for(i=0;i<l;i++)
{
ch=si.charAt(i);
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':System.out.println(ch);
}
}
}
}

Special words are those words which starts and ends with the same letter.
Examples: EXISTENCE COMIC WINDOW
Palindrome words are those words which read the same from left to right and
vice versa Examples: MALAYALAM MADAM LEVEL ROTATOR CIVIC
All palindromes 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.
import java.util.*;
public class PalindromeSpecial
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String s,rev="";
int i,l;
System.out.println("Enter the string");
s=sc.nextLine();
l=s.length();
s=s.toUpperCase();
for(i=l-1;i>=0;i--)
rev=rev+s.charAt(i);
if(s.compareTo(rev)==0)
System.out.println(s+" is palindrome");
else if(s.charAt(0)==s.charAt(l-1))
System.out.println(s+" is special");
else
System.out.println(s+" is neither palindrome nor special");

}
}

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
import java.util.*;
public class DoubleLetters
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String s;
int i,count=0;
char ch;
System.out.println("Enter the string");
s=sc.nextLine();
int l=s.length();
s=s.toUpperCase();
for(i=0;i<l-1;i++)
{
if(s.charAt(i)==s.charAt(i+1))
count++;
}
System.out.println("Number of double letter words="+count);
}
}

Write a program to accept a word and convert it into lowercase if it is in


uppercase, and display the new word by replacing only the vowels with the
character following it : Example : Sample Input : computer
Sample Output : cpmpvtfr
import java.util.*;
public class VowelReplace
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String s,w="";
int i;
char ch;
System.out.println("Enter the string");
s=sc.nextLine();
int l=s.length();
s=s.toLowerCase();
for(i=0;i<l;i++)
{
ch=s.charAt(i);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')

w=w+(char)(ch+1);

else
w=w+ch;
}
System.out.println(“The new string is “+w);
}
}

You might also like