You are on page 1of 10

String Handling Programs

Question 1
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

Answer:

import java.util.*;
class Reversed
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter a string: ”);
String s = sc.nextLine( );
for(int i=0 ; i<s.length( ) ; i++)
{
char ch = s.charAt(i);
if(Character.isUpperCase(ch))
System.out.print(Character.toLowerCase(ch));
else
System.out.print(Character.toUpperCase(ch));
}
}
}

Question 2
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

Answer:

import java.util.*;
class FirstLetter

-1-
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter a string: ”);
String s = sc.nextLine( );
char ch = s.charAt(0);
String ns = “”+Character.toUpperCase(ch);
for(int i=1 ; i<s.length( ) ; i++)
{
ch = s.charAt(i);
if(Character.isWhitespace(ch))
{
char c = s.charAt(++i);
c = Character.toUpperCase(c);
ns = ns + “ ” + c;
}
else
ns = ns + ch;
}
System.out.print(“The new string: ”+ns);
}
}

Question 3
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

Answer:

import java.util.*;
class ReplaceVowels
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

-2-
System.out.print(“Enter a word: ”);
String w = sc.next( );
w = w.toLowerCase( );
String nw = “”;
for(int i=0 ; i<w.length( ) ; i++)
{
char ch = w.charAt(i);
if(ch==‘a’ || ch==‘e’ || ch==‘i’ || ch==‘o’ || ch==‘u’)
nw = nw + (char)(ch+1);
else
nw = nw + ch;
}
System.out.println(“New Word = ”+nw);
}
}

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

Answer:

import java.util.*;
class DoubleLetterSequence
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter a sentence: ”);
String s = sc.nextLine( ).toUpperCase( );
int c = 0;
for(int i=0 ; i<s.length( )–1 ; i++)
{
char ch1 = s.charAt(i);
char ch2 = s.charAt(i+1);
if(ch1 == ch2)
-3-
c++;
}
System.out.println(“Number of double letter sequences =”+c);
}
}

Question 5
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 Á’.
Example:
Sample Input : ADVANCEMENT AND APPLICATIONS OF
INFORMATION TECHNOLOGY ARE EVER CHANGING
Sample Output : Total number of words starting with letter A = 4

Answer:

import java.util.*;
class WordsStartingA
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter a sentence: ”);
String s = sc.nextLine( );
s = “ ” + s.toUpperCase( );
int c = 0;
for(int i=0 ; i<s.length( ) – 1 ; i++)
{
char ch1 = s.charAt(i);
char ch2 = s.charAt(i+1);
if(ch1 == ‘ ’ && ch2 == ‘A’)
c++;
}
System.out.println(“Total number of words starting with letter A = ”+c);
}
}

Question 6
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.
-4-
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

Answer:

import java.util.*;
class Frequency
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter a sentence : ”);
String s = sc.nextLine( );
System.out.print(“Enter a word to be searched : ”);
String w = sc.next( );
s = s + “ ”;
String ew = “”;
int c = 0, len = s.length( );
for(int i=0 ; i<len ; i++)
{
char ch = s.charAt(i);
ew = ew+ch;
if(ch == ‘ ’)
{
ew = ew.trim( );
if(w.equalsIgnoreCase(ew))
c++;
ew = “”;
}
}
System.out.println(“Search word occurs : ”+c+“ times”);
}
}

Question 7
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.

-5-
Answer:

import java.util.*;
class LongestWordLength
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter a string: ”);
String s = sc.nextLine( );
s = s + “ ”;
String w = “”;
int wl=0, lwl=0;
for(int i=0 ; i<s.length( ) ; i++)
{
char ch = s.charAt(i);
if(ch != ‘ ’)
w = w + ch;
else
{
wl = w.length( );
if(wl > lwl)
lwl = wl;
w = “”;
}
}
System.out.print(“No of characters in longest word = ”+lwl);
}
}

Question 8
Write a program to input any given string to calculate the total number of characters
and vowels present in the string and also reverse the string.
Example:
INPUT
Enter string : SNOWY
OUTPUT
Total number of characters : 05
Number of vowels : 01
Reverse string : YWONS

-6-
Answer:

import java.util.*;
class Word
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter a string: ”);
String s = sc.nextLine( );
int len = s.length( ), vow = 0;
String rev = “”;
for(int i=len–1 ; i>=0 ; i––)
{
char ch = s.charAt(i);
rev = rev + ch;
ch = Character.toUpperCase(ch);
if(ch==‘A’ || ch==‘E’ || ch==‘I’ || ch==‘O’ | |ch==‘U’)
vow++;
}
System.out.println(“Total number of characters : ”+len);
System.out.println(“Number of vowels : ”+vow);
System.out.println(“Reverse string : ”+rev);
}
}

Question 9
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.

Answer:

import java.util.*;
class SpecialPalindrome
{
-7-
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter a word: ”);
String s=sc.next( );
s.toUpperCase( );
String sr= “”;
for(int i=s.length( )–1 ; i>=0 ; i––)
{
char ch = s.charAt(i);
sr = sr+ch;
}
if(s.equals(sr))
System.out.println(“It is a palindrome word.”);
else if(s.charAt(0) == s.charAt(s.length( )-1))
System.out.println(“It is a special word.”);
}
}

Question 10
Write a program that encodes a word into Piglatin. To translate word into a Piglatin
word, 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

Answer:

import java.util.*;
class PiglatinWord
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter a word : ”);
String w = sc.next( );
w = w.toUpperCase( );
int i;
for(i=0 ; i<w.length( ) ; i++)
-8-
{
char ch = w.charAt(i);
if(ch==‘A’ || ch==‘E’ || ch==‘I’ || ch==‘O’ || ch==‘U’)
break;
}
String nw = w.substring(i).concat(w.substring(0,i)) + "AY";
System.out.print(“Piglatin Word : ”+nw);
}
}

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

Answer:

class FileDetails
{
public static void main(String args[ ])
{
String path = “C:\\Users\\admin\\Pictures\\flower.jpj”;
String fp = path.substring(0, path.lastIndexOf(“\\”)+1);
String fn = path.substring(path.lastIndexOf(“\\”)+1,path.lastIndexOf(“.”));
String ext = path.substring(path.lastIndexOf(“.”) + 1);
System.out.println(“File Path: ”+fp);
System.out.println(“File Name: ”+fn);
System.out.println(“File Extension: ”+ext);
}
}

Question 12
Write a program to input a string in uppercase and print the frequency of each character.
Example:
INPUT : COMPUTER HARDWARE
OUTPUT:

-9-
CHARACTER FREQUENCY
A 2
C 1
D 1
E 2
H 1
M 1
O 1
P 1
R 3
T 1
U 1
W 1

Answer:

import java.util.*;
class CharacterFrequency
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter a sentence : ”);
String s = sc.nextLine( );
s = s.toUpperCase( );
int c = 0;
for(int i=65 ; i<=90 ; i++)
{
for(int j=0 ; j<s.length( ) ; j++)
{
char ch = s.charAt(j);
if(ch == i)
c++;
}
if(c > 0)
System.out.println((char)i+“\t”+c);
c = 0;
}
}
}
=================================
- 10 -

You might also like