0 ratings 0% found this document useful (0 votes) 54 views 14 pages 10.string Programs
The document contains a series of programming tasks in Java, including string manipulation, character counting, and pattern generation. It provides examples and expected outputs for tasks such as converting strings to uppercase, counting vowels, and checking for palindromes. Each task is presented with sample inputs and outputs, along with code snippets to guide implementation.
AI-enhanced title and description
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here .
Available Formats
Download as PDF or read online on Scribd
Go to previous items Go to next items
Save 10.string programs (1) For Later Question 5 et
Define a class to accept a string and convert the same to uppercase, create and display the new
string by replacing each vowel by immediate next character and every consonant by the previous
character. The other characters remain the same.
Example: Input IMA GINATION@2024
Output — : #JLBEJMBSJPM@2024Question 7 Cc
Define a clast
ts
fo accept a string and print the same in reverse and also print the number,
vowels in the string.
Sample Input: Wd = "BEAUTIFUL"
Sample Output: "LUFITUAEB"
Number of vowels = 5Question 5
Write a progkam xt Java to accept a String i : | (15)
in a mixed ca: ‘ludi ligi i
characters). Perform the following tasks as per the user's ey a
1. Count and display the number of vowels —
2. Count and display the digits , -
3. Count and display the number of special characters excluding whitespaces
Sample Input : 15 August is celebrated as: "Independence Day"
Sample Output: Number of vowels 215
Number of digits
‘Number of special ch:Question 5 Gy)
Write a progtnr to accept a sentence and delete all the uppercase characters Present i
‘iting: The program also counts the deleted characters and alepay the ace eet’
Sampie Input: The Capital of India is New Deka
Sample Output: he aptal of nda is ew eh
Number of characters deleted = $
‘Ans. //To delete and count the characters from a Sting
fenport java ut
public ass Delete
{
public static void main(String argt 1)
f
y Scanner in-new Scanner(ystem in;
String wd, wai
char chr;
inti, pc = 0;
‘System.out.print("Enter a String:");
wed = innextLine( );
Pp = wd.length( );
for(i=0; ic p; i++)
(
chr=wd.charat(i);
if(chr>='A &ebechr<='Z')
c=ctl;
else
wdl=wdl-+chr;
J
System.outprintin(’New string after deleting all the uppercase characters:
System out printin(wd);
System.out printin("Number of characters deleted = "+c);Question 8 &)
Write a prograi (15)
the following specifications:
Class name : StringProg
Data members/Instance variables:
String str
Member methods:
StringProg(String)
void count(char ¢) .
void compare(String) : to compare the string str with the string passed as argument and print
whether the given string passed as an argument is larger, smaller or equal
Ane // & cecorers, beeen(15)
Questions (SS°
Write a prograit 16 input a sentence and convert it into uppercase and display each word in a
separate line.
For example,
Sample Input: India is my country
Sample Output : INDIA
1S 2
MY
COUNTRY i
3b = eit —
ae double to store the sum OF
Question 7 Gu) :
Write a progranr9 input a sentence and convert i
wea crof words starting with the letter ‘A’
Sample Input:
‘Advancement and Application of
Sample Output:
ADVANCEMENT AND APPLICATION OF
‘CHANGING
starting with letter ‘A’ in the sentence = 4
Total number of words,
‘Ans. // to display number of words start with letter ‘A’ in the sentence
import java.util.*;
class Sentence
{
public static void main(String args[ ])
¥
Scanner in=new Scanner(System.in);
int ipc=0;
char b;
String strstr1;
System.out-printin("Enter 7
abe rein a sentence:");
to uppercase. Count and disp g
Information Technology are Ever Changing
INFORMATION ‘TECHNOLOGY ARE EVEi
b=strl.charAt(i);
if((b==' ')&é& strl.charAt(i+)c=ctl;
System.out.printin("The sentence after converting to uppercase’);
System.out printin(str1);
System.out-printin("Number of words starting with letter ‘A
J}
in the sentence are: "+c);Question 4 i .
Using the (CV) ns write a menu driven program oe ne following:
(a) To print the Floyd's triangle: (b) To display the following pattern;
1
23 Tee
aie HG) Tas iauS,
Peseta uaa) Ti Cleese e,
1 12 13.48 15
For an incorrect option, an appropriate error message should be displayed.
Ans. // a menu driven program on Patterns
import java.util.*;
class Choice
{
public static void main(String args[ ])
Scanner in = new Scanner(System.in);
int ij,p=1,cha;
String wd;
System.out.printin("Enter 1 to display Floyd's Triangle");
System.out.printin("Enter 2 to display a String pattern");
System.out println("Enter your choice:");
ch=in.nextInt( );
switch(ch)
{
case 1:
System.out printin("Floyd's Triangle");
for(i=1i<-5;i++){
for§=1j<=i;j++4)
{
System.out print(p4" ",
ptt;
}
System.out.printin( )
break;
case 2:
System.out.printin("Enter a word:");
wd=in.next( );
a=wd.length( );
for(i=0;i<=a;i++)
{
System.out.printIn(wd.substring(0,i));
break;
default: ae
System.out.println("It is a wrong choice!”);
}
}
NEHA hes ereint of vege ne Pattern
# t store length of word
question 5
ial words are those words which te .
amples: eee ee WINDOW ind end with the same letter.
palindrome words are those words which read th ; , .
samples: MALYALAM, MADAM, LEVEL, ROTATOR CVG ot 7 Heht and vicewersa
All palindromes are special words but all special words are not palindromes.
Write a program to accept a word. Print and display whether the word is a palindrome or only
special word.
Ans, // a program to check whether a special word or a palindrome word
import java.util.*;
class Spl_Word
{
public static void main(String args| ])
{
Scanner in = new Scanner(System.in);
int ik;
String word,str:
5]char chr;
system.out printin(
word=in.next( );
k=word.length( );
if(word.charAt(0)==word.charAt(k-1))
{
for(i=k-1,i>=0;i--)
{
chr=word.charAt(i);
str=str+chr;
}
if(str.equals(word))
System.out.printIn("It is a palindrome word");
else
System.out.printin("It is only a special word");
“Enter a word");
}
}
}ft ind the facie TO™ the num
a Ne factorial of ‘each ‘
i {© store the sun, to i
SUM of factorial o
. Aigits
quest “Ad. i.
7 Pt a Word and
We word By Tepacng nt ar SME it
pie Input: computer 'wels with the
eS iple Output: cpmpvtir
7a program to replace vowels
te |e OMels With the character fllowing it
public class Replace .
1 a
pete static void main(String args{ }
ts}
character f
if itis in
“Uppercase, Dis
following it ates)
Scanner in = new Scanner
Seanner i © new Scannerysem in
char chr;
int ip;
System.out print("Enter a word: ";
wdein.next( );
wd=wd.toLowerCase( );// to convert the word in lowercase
p=wd.length( );
for(i=Oicpiit+)
{
chr=wd.charAt(i);
if{chr=='a'| | chr== 'e'| | chr== ‘| | chr=='o'|| che
wd l=wall+((char)int)(che+1))
else
wdl=wd1+chr;
}
System.out.printin(
!
New word replacing vowels with the next character +wal)Question 8 its validity.
Define a Oe the Gmail ID and check for its validity.
A Gmail ID is valid only if it has:
3@
> . dot)
> gmail
Beanple is a valid Gmail ID.
mmple: iese2024@gmail.com is a valid Gmail ID.
‘Ans. // a program check the validity of 2 Gmail ID
import java.util;
class Gmail
(
public static void main(String argst J)
f
Scanner in = new Scanner(System.in);
int i
char ch;
String gm, first;
boolean check = true;
System.out print("Enter the Gmail ID: ");
gm = in:nextLine( );
if((gm.endsWith("@gmail.com’))
check = false;
else
(
first = gmsubstring(0, gmJastindexO{(@));
if(frst.length( ==0)
check = false;
forli = 0; icfirstlength( ); i++)
(
ch = first.charAt(i;
if((CharacterisLetterOrDigit(ch))
t
check = false;
break;
}
1
iff(check==true)
System.out printin("Valid Gmail 1D"),
else
System.out printIn("Invalid Gmail 1D);
}
1
ee ie ae