0% found this document useful (0 votes)
2K views17 pages

Java String Handling Programs for ICSE

Uploaded by

deepa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views17 pages

Java String Handling Programs for ICSE

Uploaded by

deepa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

STRING CLASS PROGRAMS

/** Write a program to accept a two different characters & display the sum
& difference of their ASCII values.
SampLe i/p: A
D
Sample o/p: The sum of ASCII values = 165
The difference of ASCII values =35 * */
import [Link].*;
public class sumAscii
{
public static void main(String args[])
{
[Link]("\f");
Scanner sc=new Scanner([Link]);
[Link]("Enter a character");
char ch1=[Link]().charAt(0);
char ch2=[Link]().charAt(0);
int a=(int)ch1;
int b=(int)ch2;
int sum=a+b;
int diff=a-b;
[Link]("The sum of ASCII values ="+sum);
[Link]("The difference of ASCII values
="+[Link](diff));
}
}
}
}
/** * Write a program to accept a character if it is a letter then display the
case i.e lower or upper, otherwise check whether it is digit or special
character
Sample i/p: p
Sample o/p: p is a letter
p is in lower case.
* */
import [Link].*;
public class charCheck
{
public static void main(String args[])
{
[Link]("\f");
Scanner sc=new Scanner([Link]);
[Link]("Enter a character");
char ch = [Link]().charAt(0);
if([Link](ch))
{
[Link](ch+"is a letter");
if([Link](ch))
[Link](ch +" is in upper Case");
else
[Link](ch +" is in lower Case");
}
else if([Link](ch))
[Link](ch +" is a digit");
else
[Link](ch +" is a Special Character");
}
}
/** Write a program in java to accept a string/word and display the new
string after removing all the vowels present in it.
SampLe i/p: COMPUTER APPLICATIONS
Sample o/p: CMPTR PPLCTNS **/
import [Link].*;
public class removeVowel
{
public static void main(String args[])
{
[Link]("\f");
String w="";
Scanner sc=new Scanner([Link]);
[Link]("Enter a String");
String st=[Link]();
for(int i=0;i<[Link]();i++)
{
char x=[Link](i);
if(x!='A' && x!='E' && x!='I' && x!='O' && x!='U' && x!='a' && x!
='e' && x!='i' &&x!='o' && x!='u')
w=w+x;
}
[Link](w);
}
}
/** Write a program to input a word/string, count all the alphabets
excluding vowels present in the word/string and display the result.
* Sample i/p: Happy New Year
* Sample o/p: no of alphabets excluding vowels = 8 **/
import [Link].*;
public class countNonvowel
{
public static void main(String args[])
{
[Link]("\f");
int c=0;
Scanner sc=new Scanner([Link]);
[Link]("enter a string");
String st=[Link]();
for(int i=0;i<[Link]();i++)
{
char x=[Link](i);
if(x!=' '&& x!='A' && x!='E' && x!='I' && x!='O' && x!='U' &&
x!='a' && x!='e' &&x!='i' && x!='o' && x!='u')
c++;
}
[Link]("no of alphabets excluding vowels = "+c);
}
}
/** Write a program in java to accept a name(containing three word)and
display only the initials(i.e first alphabet of each word). display the longest
word and the length of the longest word present in the string.
Sample i/p: LAL KRISHNA ADVANI
Sample o/p: L K A ` * */

import [Link].*;
public class nameInitLength
{
public static void main(String args[])
{
[Link]("\f");
Scanner sc=new Scanner([Link]);
[Link]("Enter a String");
String st=[Link]();
st=[Link]();
st=' '+st;
for(int i=0;i<[Link]();i++)
{
char x=[Link](i);
if(x==' ')
[Link]([Link](i+1)+" ");
}
}
}
/** Write a program in java to accept a name containing three words and
display the surname first followed by the first and middle names.
Sampke i/p: MOHANDAS KARAMCHAND GANDHI
Sample o/p: GANDHI MOHANDAS KARAMCHAND * */
import [Link].*;
public class surnameFirstmiddle
{
public static void main(String args[])
{
[Link]("\f");
Scanner sc=new Scanner([Link]);
[Link]("Enter a String");
String st=[Link]();
st=[Link]();
int n= [Link]();
int first=[Link](' ');
int last=[Link](' ');
st=[Link](last,n)+' '+[Link](0,last);
[Link](st);
}
}
/** Write a program in java to accept a string/sentence and find the
frequency of given alphabets. display the longest word and the length of
the longest word present in the string.
Sample i/p: WE ARE LIVING IN COMPUTER WORLD
Enter an alphabets whose frequency is to be checked :E
Sample o/p: The frequency of 'E' is 3 * */
import [Link].*;
public class charFreq
{
public static void main(String args[])
{
[Link]("\f");
int c=0;
Scanner sc=new Scanner([Link]);
[Link]("Enter a String");
String st=[Link]();
[Link]("Enter an alphabets whose frequency is to be
checked: ");
char ch=[Link]().charAt(0);
for(int i=0;i<[Link]();i++)
{
char x=[Link](i);
if(x==ch)
c++;
}
[Link]("The frequency of "+ch+" is "+c);
}
}
/** A man has written a statement as "My name is Alok Kumar Gupta and
my age is 45 years". Later on he realized that he had declared his name as
Alok instead of Ashok and age 45 instead of 35. Write a Program in java to
correct his age in the predicted statement. Display the o/p as
Sample o/p: My name is Ashok Kumar Gupta and my age is 35 years. ` * */
import [Link].*;
public class wordCorrect
{
public static void main(String args[])
{
[Link]("\f");
String st="My name is Alok Kumar Gupta and my age is 45
years";
st=[Link]("Alok","Ashok");
st= [Link]("45","35");
[Link](st);
}
}
/**
Write a program to accept a string/sentence in upper case and Display the
longest word and the length of the longest word present in the string.
Sample i/p: "TATA FOOTBALL ACADEMY WILL PLAY AGAINST MOHAN
BAGAN"
Sample o/p: The longest word : FOOTBALL : The length of word :8 ` * */
import [Link].*;
public class longestWord
{
public static void main(String args[])
{
[Link]("\f");
int max_len=0,len=0;
String w="",max_word="";
Scanner sc=new Scanner([Link]);
[Link]("Enter a String");
String st=[Link]();
st=[Link]();
st=st+' ';
for(int i=0;i<[Link]();i++)
{
char x=[Link](i);
if(x!=' ')
w=w+x;
else
{
len=[Link]();
if(len>max_len)
{
max_word=w;
max_len=len;
}
w="";
}
}
[Link]("The longest word : "+max_word+" The length
of word: "+ max_len);
}
}
/** Write a program to accept a string in upper case and find the frequency
of each vowel present in string:
Sample i/p: "RAIN WATER HARVESTING ORGANIZED BY JUSCO"
Sample o/p: Frequency of 'A' = 4
Frequency of 'E' = 3
Frequency of 'I' = 3
Frequency of 'O' = 2
Frequency of 'U' = 1 * */
import [Link].*;
public class vowelFreq
{
public static void main(String args[])
{
[Link]("\f");
int ACOU=0,ECOU=0,ICOU=0,OCOU=0,UCOU=0;
Scanner sc=new Scanner([Link]);
[Link]("Enter a String");
String st=[Link]();
for(int i=0;i<[Link]();i++)
{
char x=[Link](i);
if(x=='A')
ACOU++;
else if(x=='E')
ECOU++;
else if(x=='I')
ICOU++;
else if(x=='O')
OCOU++;
else if(x=='U')
UCOU++;
}
[Link]("FREQUENCY OF 'A' ="+ACOU);
[Link]("FREQUENCY OF 'E' ="+ECOU);
[Link]("FREQUENCY OF 'I' ="+ICOU);
[Link]("FREQUENCY OF 'O' ="+OCOU);
[Link]("FREQUENCY OF 'U' ="+UCOU);
}
}
/**
* Write a program to accept a string as: IF IT RAINS, YOU WILL NOT GO TO
PLAY" Convert all characters of the word in lower case other than the first
characters so as to obtain the following
output:
Sample o/p: If It Rains, You Will Not Go To Play. * */
import [Link].*;
public class wordUpper
{
public static void main(String args[])
{
[Link]("\f");
int f=0;
String w="";
Scanner sc=new Scanner([Link]);
[Link]("Enter a String");
String st=[Link]();
st= [Link]();
st=' '+st;
st= [Link]();
for(int i=0;i<[Link]();i++)
{
char x=[Link](i);
if(x==' ')
{
w=w+x;
f=1;
continue;
}
if(f==1)
{
x=[Link](x);
f=0;
}
w=w+x;
}
[Link](w);
}
}
/* OR
for(int i=0;i<[Link]();i++)
{
char x=[Link](i);
if(x==' ')
{
int n=[Link]();
char y=
[Link]([Link](i+1));
st=[Link](0,i+1)+y+[Link](i+2,n);
}
}
[Link](w);
**/
/** Write a program to accept a word and display the ASCII of each
character.
Sample i/p: BLUEJ
Sample o/p: ASCII of B = 66
ASCII of L = 75
ASCII OF U = 84
ASCII OF E = 69
ASCII of L = 73 * */
import [Link].*;
public class charAscii
{
public static void main(String args[])
{
[Link]("\f");
Scanner sc=new Scanner([Link]);
[Link]("enter a word");
String st=[Link]();
for(int i=0;i<=[Link]()-1;i++)
{
char x=[Link](i);
[Link]("ASCII of "+x+" = "+(int)x);
}
}
}
/**Write a program to input a string in upper case and replace all the vowels
with Asterisk(*) present in the string.
Sample i/p: TATA STEEL IS IN JAMSHEDPUR
Sample o/p: T*T* ST**L I* J*MSH*DP*R * */
import [Link].*;
public class vowelReplace
{
public static void main(String args[])
{
int c=0;
String w="";
[Link]("\f");
Scanner sc=new Scanner([Link]);
[Link]("enter a string");
String st=[Link]();
st=[Link]();
for(int i=0;i<=[Link]()-1;i++)
{
char x=[Link](i);
if(x=='A'||x=='E'||x=='I'||x=='O'||x=='U')
{
x='*';
}
w=w+x;
}
[Link](w);
}
}
/** Write a program in java to enter a string and frame a word by joining all
the first characters of each [Link] the new word.
Sample i/p: Vital Information Resource Under Seize
Sample o/p: VIRUS */
import [Link].*;
public class wordFirstchar
{
public static void main(String args[])
{
String w="";
[Link]("\f");
Scanner sc=new Scanner([Link]);
[Link]("enter a string");
String st=[Link]();
st=[Link]();
st=" "+st;
for(int i=0;i<[Link]();i++)
{
char x= [Link](i);
if(x==' ')
w=w+[Link](i+1);
}
[Link](w);
}
}
/** Write a program in java to enter a string and display all the palindrome
words present in the String.
Sample i/p: MOM AND DAD ARE NOT AT HOME
Sample o/p: MOM
DAD */
import [Link].*;
public class palindromeWord
{
public static void main(String args[])
{
char x;
String s1="",s2="",rev="";
[Link]("\f");
Scanner sc=new Scanner([Link]);
[Link]("enter a string");
String st=[Link]();
st= [Link]();
st=[Link]();
st=st+" ";
for(int i=0;i<[Link]();i++)
{
x=[Link](i);
if(x!=' ')
{
s1=s1+x;
s2=x+s2;
}
else
{
if([Link](s2))
[Link](s1);
s1="";
s2="";
}
}}}

/** Write a program to accept a string and display the string in a reverse
order.
Sample i/p: COMPUTER IS FUN
Sample o/p: FUN IS COMPUTER */
import [Link].*;
public class stringRev
{
public static void main(String args[])
{
[Link]("\f");
String w="",s1="";
char x=' ';
Scanner sc= new Scanner([Link]);
[Link]("enter a String:");
String st= [Link]();
st=[Link]();
st=" "+st;
for(int i= [Link]()-1;i>=0;i--)
{
x=[Link](i);
if(x!=' ')
w=x+w;
else
{
s1=s1+w+" ";
w="";
}
}
[Link](s1);
}
}
/**Write a Program to input a string and print the character which occur
maximum no of times
within a string.
Sample i/p: James Gosling developed java
Sample o/p: The character with maximum no of times: e */
import [Link].*;
public class Q18
{
public static void main(String args[])
{
[Link]("\f");
char x=' ',ch=' ';
int c,l=0;
Scanner sc= new Scanner([Link]);
[Link]("enter a string:");
String st= [Link]();
st=[Link]();
for(int i=0;i<[Link]();i++)
{
x=[Link](i);
c=0;
if([Link](x)!=[Link](x))
{
for(int j=i+1;j<[Link]();j++)
{
if(x==[Link](j))
c++;
}
if(c>l)
{
ch=x;
l=c;
}
}
}
[Link]("the character with no of times:"+ ch);
}
}
/**Write a program to input a string and print the word containing maximum
no of vowels.
SAMPLE i/p: HAPPY NEW YEAR
SAMPLE O/P: THE WORD WITH MAXIMUM NO OF VOWELS :
YEAR */
import [Link].*;
public class Q19
{
public static void main(String args[])
{
[Link]("\f");
char x;
int c=0,f=0,l=0;
String w="",max="";
Scanner sc =new Scanner([Link]);
[Link]("enter a string");
String st=[Link]();
st=[Link]();
st=st+" ";
for(int i=0;i<[Link]();i++)
{
x=[Link](i);
if(x!=' ')
{
w=w+x;
if(x=='a'||x=='A'||x=='e'||x=='E'||x=='i'||
x=='I'||x=='o'|| x=='O'||

x=='u'||x=='U')
c++;
}
else
{
if(f==0)
{
max=w;
l=c;
f=1;
}
else if(c>l)
{
max=w;
l=c;
}
w="";
c=0;
}
[Link]("max="+max+" l= "+l);
}
[Link]("The word with maximum no of vowels :"+
max);
}
}
/** Consider the string as “Blue bottle is in Blue bag lying on the Blue
carpet”. Write a program to accept a sample string and replace the word
'Blue' with 'red'. The new string is displayed as
“Red bottle is in Red bag lying on the Red carpet”
*/
import [Link].*;
public class Q20
{
public static void main(String args[])
{
[Link]("\f");
Scanner sc=new Scanner([Link]);
[Link]("Enter a string:");
String st=[Link]();
st=[Link]("Blue","Red");
[Link](st);
}
}
/**A computer typist has the habit of deleting the middle name 'kumar'
while entering the names containing three words,write a program to enter
a name contaning three words and display the new name after deleting
middle name 'kumar'.
sample input : Ashish Kumar Nehra
Sample o/p: Ashish Nehra */
import [Link].*;
public class Q21
{
public static void main(String args[])
{
[Link]("\f");
Scanner sc=new Scanner([Link]);
[Link]("enter a 3 word name:");
String st=[Link]();
st=[Link]();
int first=[Link](' ');
int last=[Link](' ');
String s1=[Link](0,first)+"
"+[Link](last+1,[Link]());
[Link](s1);
}
}
/** Write a program to accept a word & convert it into lower case, if it is in
upper case. Display a
new word by replacing only the vowels with the character following it.
Sample i/p: Computer
Sample o/p: cpmpvtfr */
import [Link].*;
public class Q22
{
public static void main(String args[])
{
[Link]("\f");
char x;
Scanner sc= new Scanner([Link]);
[Link]("enter a string");
String st=[Link]();
st=[Link]();
for(int i=0;i<[Link]();i++)
{
x=[Link](i);
if(x=='a'||x=='e'||x=='i'||x=='o'||x=='u')
st=[Link](x,++x);
}
[Link](st);
}
}
/* Write a program to input a word & print the new word after removing all
the repeated alphabets
* sample i/p: applications
* sample o/p: aplictons */
import [Link].*;
public class Q23
{
public static void main(String args[])
{
String s1;
char x=0;
[Link]("\f");
Scanner sc = new Scanner([Link]);
[Link]("enter a string:");
String st= [Link]();
for(int i=0;i<[Link]()-1;i++)
{
x=[Link](i);
if([Link](x)!=[Link](x))
{
for(int j=i+1;j<[Link]();j++)
{
char y=[Link](j);
if(x==y)
st=[Link](0,j)
+[Link](j+1,[Link]());
}
[Link](st);
}
}
}
}
/**A string is said to be 'unique' if none of the alphabets present in the
string are repeated. Write
a program to accept a string and check whether the string is unique or not.
The program displays a message accordingly.
Sample i/p: COMPUTER
SAMPLE O/P: UNIQUE STRING */
import [Link].*;
public class Q24
{
public static void main(String args[])
{
[Link]("\f");
int f=0;
char x;
Scanner sc= new Scanner([Link]);
[Link]("enter a string:");
String st=[Link]();
for(int i=0;i<[Link]();i++)
{
x=[Link](i);
if([Link](x)!=[Link](x))
{
[Link]("NOT A UNIQUE STRING");
f=1;
break;
}
}
if(f==0)
[Link]("UNIQUE STRING");
}
}
/** Write a program to input a string and print the frequency of vowels of
each word present in the string.
SAMPLE I/P: MASTERING INFORMATION TECHNOLOGY
SAMPLE O/P: NO OF VOWELS PRESENT IN MASTERING : 3
NO OF VOWELS PRESENT IN INFORMATION : 5
NO OF VOWELS PRESENT IN
TECHNOLOGY :3 */
import [Link].*;
public class Q25
{
public static void main(String args[])
{
[Link]("\f");
char x;
int c=0;
String w="";
Scanner sc =new Scanner([Link]);
[Link]("enter a string");
String st=[Link]();
st=[Link]();
st=st+" ";
for(int i=0;i<[Link]();i++)
{
x=[Link](i);
if(x!=' ')
{
w=w+x;
if(x=='a'||x=='A'||x=='e'||x=='E'||x=='i'||x=='I'||
x=='o'||x=='O'
||
x=='u'||x=='U')
c++;
}
else
{
[Link](" NO OF VOWELS PRESENT IN
"+ w +" = "+c);
w="";
c=0;
}
}
}
}
/**
A non Palindromte word can be a palindrome word just by adding reverse of
the word with the
original word. Write a program to accept a non -palidrome word and display
the new word after
making it a palindrome.
Sample i/p: ICSE
Sample o/p: The new word aftter making it palindrome as:
ICSEESCI */
import [Link].*;
public class Q26
{
public static void main(String args[])
{
[Link]("\f");
Scanner sc=new Scanner([Link]);
[Link]("enter a string");
String st=[Link]();
st=[Link]();
for(int i=[Link]()-1;i>=0;i--)
{
char x=[Link](i);
st=st+x;
}
[Link](st);
}
}
/**Write a program to input a string. Count and dispaly the frequency of
each alphabet in an
order, Which is present in the string.
Sample i/p: COMPUTER APPLICATIONS
Sample o/p: A-2,C-2,I-1,L-2,M-1,N-1,O-2,P-3,R-1,S-1,T-2,U-1 * */
import [Link].*;
public class Q27
{
public static void main(String args[])
{
int c=0,f=0;
char x;
[Link]("\f");
Scanner sc=new Scanner([Link]);
[Link]("enter a string");
String st=[Link]();
st=[Link]();
st=[Link]();
[Link]("Character\tFrequency");
for(char i='A';i<'Z';i++)
{
f=0;c=0;
for(int j=0;j<[Link]();j++)
{
x=[Link](j);
if(x==i)
{
f=1;
c++;
}
}
if(f==1)
[Link](" "+i+"\t \t"+c+"\n");
}
}
}

Common questions

Powered by AI

The logic involves iterating through the string and checking each character's first and last index. If they differ, indicating repetition, subsequent occurrences of the character are removed by reconstructing the string without those instances. This approach reduces character diversity and ensures a minimally occurring character set for simplified parsing needs, though it does not preserve the original string's reading flow .

The program ensures consistent case handling by converting the entire input string to upper case before evaluating each character for vowel frequency. This eliminates any discrepancies that could arise from mixed-case input, allowing accurate frequency counts of each vowel .

The program initializes variables to track the current word and its length, comparing each word's length as it iterates through a string with each word separation marked by a space. When a longer word is found, its length and content are stored. After processing the entire string, the stored longest word and its length are displayed .

The algorithm assumes uniform case (all upper case) and seamless space separation for word identification. It concatenates characters into a forward and backward string for comparison. Challenges include handling punctuation, case inconsistencies in input, and efficiently managing computation for large text without optimization or additional checks for word boundaries beyond spaces .

The program classifies the input character as a special character if it is neither a letter nor a digit. It uses the `Character.isLetter()` and `Character.isDigit()` methods to differentiate between letters, digits, and special characters. If both conditions return false, the input is classified as a special character .

The method implemented in reversing a sentence involves adding a space at the start of the string, then iterating backward to accumulate words in reverse order. This technique, while effective for sentence reversal, inherently assumes spaced-separated words and does not handle punctuation or preserve original formatting beyond basic word order reversal .

The program effectively converts a non-palindrome word into a palindrome by appending the reverse of the word to itself. This operation ensures symmetry around the center of the new string, thus, meeting the basic definition of a palindrome. However, it might not always produce a natural or meaningful palindrome .

The program leverages ASCII values by casting characters to integers, a process integral in numeric comparisons and arithmetic operations like sum and differences of characters. This is particularly useful for comparison without relying on direct character operations, thus providing a versatile and language-agnostic method for character-based computation .

The program implements a nested loop structure: the outer loop iterates through all possible characters ('A' to 'Z'), while the inner loop counts occurrences by comparing each character in the string to the current loop character. This method provides an orderly, thorough frequency count with explicit enumeration, benefiting applications requiring detailed character distribution analysis in text data .

The program employs a systematic approach where each character is evaluated against criteria, such as being a vowel, to decide its transformation, typically using conditional replacements or string operations. The strategy involves iterating through characters, checking qualitative conditions, and updating the string in place, demonstrating the power and flexibility of string manipulation in Java .

You might also like