ay
SOLVED PROGRAMS ON STRING MANIPULATION z
Prog. 1 Write a program to display each letter of a word entered by the we
4 .
Sample Input : BLUEJ
Sample Output : B
L
U
Z
a
/! To display the given pattern
import java.util.*;
public class Pattern
(
public static void main(String args[ ])
{
Scanner in=new Scanner(System.in);
int a,b;
Suring st;
System.out.printin("Enter the word in uppercase:");
in.next( );
-length( );
System.out.printin("The Pattern:");
for(a=0;a
=0)
vev+1;
}
System.out.printin(
}
"The number of vowels in the string:"+v);Enter a string:
computer science
|The output:
lcomput*r scisnes
Can only enter input wi
te your program is running
Write a program to accept
the string, Display ae wae and change the case of each letter of
Example:
Sample Input: WelComE ‘To School
Sample Output: WELcOMe t
to
Example2: ec
Sample Input: WelCome_202
Sample Output: WELcOmE_ 2029
{ICSE 2008, 2022]
1) To convert the case of the string
import java.util.*;
public class Convert_Case
(
String st,st!
char chr,chr1;
System.out.printIn("Enter a string:");
st=in.nextLine( );
p=stlength( );
for(a=0;a='a' && chr<='z')
{
chr1=Character.toUpperCase(chr);
stl=stl+chr1;
}
else if(chr>='A' && chr<='Z')
{
\hirl =Character.toLowerCase(chr);
oe
String Handling | 27; .printin("The new string after converting the case of ea
System.out <
system outprintn(st] ):
}
}
Output:
Bi bives Terminal Window -Compt0
inter a string:
To School ;
NetCont Teeang after converting the case of each letter:
IwELcOMe to sCHOOL
Nea new string after converting the case of each letter:
|wELcomE_2022
enter input while your program is running
| can only
Prog. 5 /Write a program to accept a String and display the following.
(i) Number of digits
(ii) Number of uppercase and lowercase letters
(iii) Number of special characters
Sample Input: KAPILDEV @83
Sample Output:
Number of digits:
Number of uppercase and lowercase letters: 8
Number of special characters: | [ICSE 23
// To count and display types of characters present in a string
import java.util.*
public class Special
{
Public static void main(String args[ ])
{
Scanner in=new Scanner(System.in);
int a,p,uple=0,d=0,spl=0;
String st;
char chr;
System.out.printIn("Enter a string:
st=in.nextLine( );
278 | Understanding Computer Applications with BlueJ—xtdength( );
fortadia='a" && chre="2!) i Cha’ ga chr<='Z'))
uple=uple+1;
else if(chr>="0' && chr<='9")
ded+l;
else
spl=spl+1;
}
System.out.printin("Number of di;
System.out_printIn("Number of wy
System.out printIn("Number of special characters:"+spl);
}
}
Output:
gits present in the strin;
'PPercase and lowercase letters:"+uplc);
B Bluet: Terminal Window - Compo
Options
Enter a string:
KAPILDEVes3
Number of digits
Number of upperca
Number of special,
Present in the string:2
ise and lowercase letters:8
characters:1
Cen enly enter input while your progran ie running
Prog. 6/ Write a program to accept a name and dis;
play the initials along with the
surname.
Sample Input: Mohandas Karamchand Gandhi
Sample Output: M. K. Gandhi
To enter a full name and to print initials with surname
import java.util.*;
Public class Surname
{
Public static void main(String args{ ])
il
String st,sn,stl="",st2="
int ip;
char chr;
Scanner in=new Scanner(System.in);
System.out.printIn(“Enter the full nami
St=in.nextLine( ); //Accept full namecuiemee——
if(ch==ch2)
tel; .
}
if(t==0)
s2=s2+chl;
“The new word after removing duplicate letters.
System.out.printin( 499),
}
Output:
GB Blued: Terminal Window - Comp10
Options
enter a word in lowercase:
lapplications
he new word after removing duplicate letters:aplictons
" Write a program in Java to accept a word and check whether the
is Palindrome or not.
Sample Input: Enter a word: madam
Sample output: Palindrome
(A word is said to be Palindrome, if it reads same after reversing the ete
[ICSE 20
// To check whether a word is Palindrome or not
import java.util.*;
public class Palindrome
{
public static void main(String args ])
{
Scanner in = new Scanner(System.in);
int i,p;
284 | Understanding Computer Applications with BlueJ—Xgystem-outprintin("Enter a word: ");
vord=in.next( );
=word.length( );
a p-lii>=0;i-—)
: chr=word.charAt(i);
word|=word1+chr;
; :
jf(word.equals(word1))
Systeri.out-println("Palindrome"),
else
System.out.printIn("Not a Palindrome");
l
Output:
PB bluek Terminal Window = Compo
Options
Enter a word:
Inadan
IPatindrone
lEnter a word:
Jcomputer
Not a Palindrome
only enter input whdls 9
Write a program in Java to accept a sentence in lowercase. Convert
first letter of each word of the sentence in uppercase. Display the new
sentence so formed.
Sample Input: we are in cyber world
Sample Output: We Are In Cyber World (ICSE 2018]
4 To convert all the first letter of each word the sentence in uppercase
import java.util.*;
Public class Convert
{
public static void main(String args[ ])
{
String str,strl="";
int ip;
char chr,chr1;
Scanner in = new Scanner(System.in);System.out.printIn("Enter a string: "); 4
str=in.nextLine( ); 5
str=' '+str;
p=str.length( );
for(i=0;iRTU [ °
ey enor
S| camng
|Prog. 14 rite a program in Java to accept a word and display the same in Pig
i Latin form. A word is framed in Pig Latin form by using the following
steps:
i. Enter a word.
ii, Shift all the letters which are available before the first vowel, towards
the end of the word.
iii, Finally, add 'AY' at the end of word sequence.
Sample Input: TROUBLE
Sample Output: OUBLETRAY [ICSE 2013]
To display a Pig Latin word
public class Piglatin
{ *
public static void main(String str)// Enter a String
{
int m,p;
String strl,str2;
char ch;ch=0;
str=str.toUpperCase( );
String Handling | 287p=str.length( );
for(m=0;m
=0;a-—)
(
for(p=1;p<=k;p++)
{
ew Scanner(System.in);
System.out.print(" ");
}
System.out.printin(st.substring(0,a));
kets
}
}
)
Beat
lt
he Pattern:
JaLues
LUE JB
Jucvat
IEJBLU
JJBLUE
Can only enter input while your progr
‘an is running
Write a menu driven
program i
ic gram to display the pattern as the per user's
Pattern |
ABCDE dare 2
ABCD 7;
' ABC U é U
p AB
a EEEE
i 2 : i 71
i ‘or an incorrect option, an appropriate error message should be
| displayed.
| {ICSE 2018]
1) To display the pattern of a String as per the user's choice
import java.util. *;
public class Display_Pattern
{
public static void main(String args{ })
{
Scanner in=new Scanner(System.in);
int a,b,p,k,igj. m=4;
char ch='A'‘, chr;
String str;
System.out.printIn("Ent
System.out.printIn("Enter your choice:");
ksin.nextInt( );
switch(k)
{
case 1:
System.out.printin("The Pattern 1:");
for(a=1; a<=5; a++)
{
for(b=ch; b<=ch+m; b++)
{
ter 1 to display ‘Pattern 1’ & 2 to display ‘Pattern 2' ");g haracter array of size ten, A
E | Define a class to declare a cl : .
Prog. 20 ‘characters into the array and display the characters with the hignet the
lowest ASCII (American Standard Code for Information Inept 4
Be)
values. va __
Sample Input: (‘R’, '2', ‘a p,m’, 'U', 'Q, 'F}
Sample Output:
Character with the highest ASCII value = z
_ Character with the lowest ASCII value = A licse 200
11 To display characters with the highest and lowest ASCII values
import java.util.*;
class High_Low_ASCII
{
Public static void main(String args{ })
{
Scanner in = new Scanner(System.in);
char chr{ ] = new char[10];
int ij;
System.out.printIn("Enter 10 characters in the array one by one:");
for(i=0; i<10; i++)
chrfiJ=in.next( ).charAt(0);
char high=chr[0};
char low=chr{0];
for(j=0; j
{
if(highchr[j])
low=chr{jJ;
}
System out printin(*Character with highest ASCII value: "+high);
System.out printin("Character with lowest ASCII value: "+low):
}
| Prog. 2: Define a class to accept 10 characters (lowercase/uppercase) from the
a in an array. Using bubble sort technique, arrange them in ascending
order,
Display the sorted array and the original array,
Sample Input: (©, pa, Wp ree,
Sample Output: [a, ¢, 4, f, 2, hy k, m, p, 2] (ICSE 2023]
294 | Understanding Computer Applications with Blue—xBx
f i ‘
to as0}8Y ae or an array in ascending order using bubble sort technique
sava.util.*; ,
“anport Javad
we characte
d
(vie state void main(String args })
scanner in = new Scanner(System.in);
char chrl ] = new char{10};
int ids
char ts
system.out.printIn(
for(i=0; i<10; i++)
in.next( ).charAt(0);
i<9; i++)
"Enter 10 characters in the array one by on
for(j=
(
if(chr[j]> chr[j+1])
{
t= chrij);
chrfjl= chrfj+1];
chr{j+1] =t;
}
}
}
System.out.printin("The characters arranged in ascending order are:");
for(i=0; i<10; i++)
System.out.printin("“chr["+i+"]; "+chr[i]);
J<(9-1); j+4)
}
}
| Prog. 22 Define a class to declare an array to accept and store ten names/words.
f Display only those names/words which begin with the letter 'A' or 'a'
| and end with the letter ‘A’ or ‘a’.
Sample Input: Hari, Anita, Akash, Amrita, Alina, Devi, Rishabh, John,
Farha, AMITHA
f Sample Output: Anita
Amrita
Alina
AMITHA {ICSE 2022]
String Handling | 2).
To display words which begin or end with the letter 'A' or ‘a’
import java.util.*;
class Words
{
Public static void main(String args[ ])
{
Scanner in = new Scanner(System.in);
String word[ ] = new String[10];
String wd;
char chl,ch2;
int ij.p;
System.out.printIn("Enter 10 words in the array one by one:
for(i=0; i<10; i++)
word[i]=in.next( );
System.out.println("The words which begin or end with the letter 'A' or a");
for(G=0; j<10; j++)
{
wd= word[j];
p=wd.length( );
chl=wd.charAt(0);
=wd.charAt(p—1);
if((ch1=="A' Il chl=="a') && (ch2=='A' [I ch
System.out.printIn(wd);
}
}
}
Prog. 23. Write a program to store names of 20 Students and their telephot
numbers correspondingly in two single dimensional arrays. Enter a name
separately and search it in the given list of names. If it is present thet
display the name with its telephone number otherwise, display a mes
) | "Name not found”. Use linear search technique.”
// To search name and the correspondin; ‘© number by using linear search
import java.util.*; :
public class Name_Telephgpge,eal
j.equals(name))
jac
{ yack b=m{[i];
kel
}
)
ith?
if ystem-out-printin( ‘The name is "4 4" and the telephone number is "
" +nfa});
se . me
tem out.printin(name 1s not found at any location");
}
)
cee,
ee ny
Write a program to accept 10 different city names ina Single Dimensional
Array (SDA). Arrange the names in ascending order by using the Bubble
Sort technique and display them.
Sample Input: Delhi, Bengaluru,
Sample Output: Agra,
[ICSE 2008, 2015)
, Agra, Mumbai, Kolkata
Bengaluru, Delhi, Kolkata, Mumbai
() To arrange the names in ascending order by using Bubble Sort technique
import javacutil.*;
class Bubble_Sort
(
public static void main(String args[ ])
(
Scanner in = new Scanner(System.in);
String ct[ ]-= new String[10];
String t='
int ij;
System.out.printIn("Enter array elements:");
forli=0;i<10;i++)
String Handling | 297Ss - out.printin("The city names arranged in ascending order:");
system.
for(i=0;i<10;i++)
System.out.printIn(ct[i]);
i : Write a program to input forty words in an array. Arrange these
in descending order of letters by using Selection Sort technique
the sorted array. ese an
Sample Input: New Delhi, Bengaluru, Agra, Mumbai, Kolkata
Sample Output: New Delhi, Mumbai, Kolkata, Bengaluru, Aga
/ To arrange the words in descending order by using Selection Sort technique
import java.util.*;
class Descending
{
Public static void main(String args[ ])
{
Scanner in = new Scanner(System.in);
int ij,min;
String wd[ ]=new String[40];
String t=";
System.out.printIn ("Enter 40 different words in the array:");
for(i=0;i<40;i++)
f
wd[i] =in.next( );
}
98 | Understanding Computer Applications with Bluel—xa .
fori
<40;j++)
{ .
jt(wali]-compareTo(wd{min})>0)
min=j;
}
tewdli];
wliJewd[min],
wa{min]=t;
} .
system.out.printIn ("The words arranged in descending order are: ");
forliaQsi<40;i++) a
System.out.printin (wd[i]);
}