CHANDAN RAI 9831688800
String Handling Programs
The string objects of Java are immutable i.e., once created, they cannot be changed. If any change
occurs in a string object, then original string remains unchanged and a new string is created with
the changed string.
String are classes in Java. Array of characters is one of the data members of String class.
[Link] is the package which contains String and StringBuffer class
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 [Link].*;
class Reversed
public static void main(String args[ ])
Scanner sc = new Scanner([Link]);
[Link](“Enter a string: ”);
String s = [Link]( );
for(int i=0 ; i<[Link]( ) ; i++)
char ch = [Link](i);
if([Link](ch))
[Link]( [Link](ch) );
else
[Link]( [Link](ch) );
GIRISH PARK ¥ HOWRAH
CHANDAN RAI 9831688800
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 [Link].*;
class FirstLetter
public static void main(String args[ ])
Scanner sc = new Scanner([Link]);
[Link](“Enter a string: ”);
String s = [Link]( );
char ch = [Link](0);
String ns = “”+[Link](ch);
for(int i=1 ; i<[Link]( ) ; i++)
ch = [Link](i);
if([Link](ch))
char c = [Link](++i);
c = [Link](c);
ns = ns + “ ” + c;
else
ns = ns + ch;
[Link](“The new string: ”+ns);
GIRISH PARK ¥ HOWRAH
CHANDAN RAI 9831688800
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 [Link].*;
class ReplaceVowels
public static void main(String args[ ])
Scanner sc = new Scanner([Link]);
[Link](“Enter a word: ”);
String w = [Link]( );
w = [Link]( );
String nw = “”;
for(int i=0 ; i<[Link]( ) ; i++)
char ch = [Link](i);
if(ch==‘a’ || ch==‘e’ || ch==‘i’ || ch==‘o’ || ch==‘u’)
nw = nw + (char)(ch+1);
else
nw = nw + ch;
[Link](“New Word = ”+nw);
GIRISH PARK ¥ HOWRAH
CHANDAN RAI 9831688800
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 [Link].*;
class DoubleLetterSequence
public static void main(String args[ ])
Scanner sc = new Scanner([Link]);
[Link](“Enter a sentence: ”);
String s = [Link]( ).toUpperCase( );
int c = 0;
for(int i=0 ; i<[Link]( )–1 ; i++)
char ch1 = [Link](i);
char ch2 = [Link](i+1);
if(ch1 == ch2)
c++;
[Link](“Number of double letter sequences = ”+c);
GIRISH PARK ¥ HOWRAH
CHANDAN RAI 9831688800
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 [Link].*;
class WordsStartingA
public static void main(String args[ ])
Scanner sc = new Scanner([Link]);
[Link](“Enter a sentence: ”);
String s = [Link]( );
s = “ ” + [Link]( );
int c = 0;
for(int i=0 ; i<[Link]( ) – 1 ; i++)
char ch1 = [Link](i);
char ch2 = [Link](i+1);
if(ch1 == ‘ ’ && ch2 == ‘A’)
c++;
[Link](“Total number of words starting with letter A = ”+c);
GIRISH PARK ¥ HOWRAH
CHANDAN RAI 9831688800
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.
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 [Link].*;
class Frequency
{
public static void main(String args[ ])
{
Scanner sc = new Scanner([Link]);
[Link](“Enter a sentence : ”);
String s = [Link]( );
[Link](“Enter a word to be searched : ”);
String w = [Link]( );
s = s + “ ”;
String ew = “”;
int c = 0, len = [Link]( );
for(int i=0 ; i<len ; i++)
{
char ch = [Link](i);
ew = ew+ch;
if(ch == ‘ ’)
{
ew = [Link]( );
if([Link](ew))
c++;
ew = “”;
}
}
[Link](“Search word occurs : ”+c+“ times”);
}
}
GIRISH PARK ¥ HOWRAH
CHANDAN RAI 9831688800
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.
Answer:
import [Link].*;
class LongestWordLength
public static void main(String args[ ])
Scanner sc = new Scanner([Link]);
[Link](“Enter a string: ”);
String s = [Link]( );
s = s + “ ”;
String w = “”;
int wl=0, lwl=0;
for(int i=0 ; i<[Link]( ) ; i++)
char ch = [Link](i);
if(ch != ‘ ’)
w = w + ch;
else
wl = [Link]( );
if(wl > lwl)
lwl = wl;
w = “”;
[Link](“No of characters in longest word = ”+lwl);
GIRISH PARK ¥ HOWRAH
CHANDAN RAI 9831688800
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
Answer:
import [Link].*;
class Word
public static void main(String args[ ])
Scanner sc = new Scanner([Link]);
[Link](“Enter a string: ”);
String s = [Link]( );
int len = [Link]( ), vow = 0;
String rev = “”;
for(int i=len–1 ; i>=0 ; i––)
char ch = [Link](i);
rev = rev + ch;
ch = [Link](ch);
if(ch==‘A’ || ch==‘E’ || ch==‘I’ || ch==‘O’ | |ch==‘U’)
vow++;
[Link](“Total number of characters : ”+len);
[Link](“Number of vowels : ”+vow);
[Link](“Reverse string : ”+rev);
GIRISH PARK ¥ HOWRAH
CHANDAN RAI 9831688800
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 [Link].*;
class SpecialPalindrome
public static void main(String args[ ])
Scanner sc = new Scanner([Link]);
[Link](“Enter a word: ”);
String s=[Link]( );
s=[Link]( );
String sr= “”;
for(int i=[Link]( )–1 ; i>=0 ; i––)
char ch = [Link](i);
sr = sr+ch;
if([Link](sr))
[Link](“It is a palindrome word.”);
else if([Link](0) == [Link]([Link]( )-1))
[Link](“It is a special word.”);
GIRISH PARK ¥ HOWRAH
CHANDAN RAI 9831688800
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 [Link].*;
class PiglatinWord
public static void main(String args[ ])
Scanner sc = new Scanner([Link]);
[Link](“Enter a word : ”);
String w = [Link]( );
w = [Link]( );
int i;
for(i=0 ; i<[Link]( ) ; i++)
char ch = [Link](i);
if(ch==‘A’ || ch==‘E’ || ch==‘I’ || ch==‘O’ || ch==‘U’)
break;
String nw = [Link](i).concat([Link](0,i)) + "AY";
[Link](“Piglatin Word : ”+nw);
GIRISH PARK ¥ HOWRAH
CHANDAN RAI 9831688800
Question 11
Write a program to input a string in uppercase and print the frequency of each character.
Example:
INPUT : COMPUTER HARDWARE
OUTPUT:
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 [Link].*;
class CharacterFrequency
public static void main(String args[ ])
Scanner sc = new Scanner([Link]);
[Link](“Enter a sentence : ”);
String s = [Link]( );
s = [Link]( );
int c = 0;
for(int i=65 ; i<=90 ; i++)
for(int j=0 ; j<[Link]( ) ; j++)
GIRISH PARK ¥ HOWRAH
CHANDAN RAI 9831688800
{
char ch = [Link](j);
if(ch == i)
c++;
if(c > 0)
[Link]((char)i+“\t”+c);
c = 0;
}
Question 12
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\[Link]
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\\[Link]”;
String fp = [Link](0, [Link](“\\”)+1);
String fn = [Link]([Link](“\\”)+1,[Link](“.”));
String ext = [Link]([Link](“.”) + 1);
[Link](“File Path: ”+fp);
[Link](“File Name: ”+fn);
[Link](“File Extension: ”+ext);
GIRISH PARK ¥ HOWRAH
CHANDAN RAI 9831688800
GIRISH PARK ¥ HOWRAH