class10 string programs
class10 string 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 java.util.*;
public class sumAscii
{
public static void main(String args[])
{
System.out.println("\f");
Scanner sc=new Scanner(System.in);
System.out.println("Enter a character");
char ch1=sc.next().charAt(0);
char ch2=sc.next().charAt(0);
int a=(int)ch1;
int b=(int)ch2;
int sum=a+b;
int diff=a-b;
System.out.println("The sum of ASCII values ="+sum);
System.out.println("The difference of ASCII values
="+Math.abs(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 java.util.*;
public class charCheck
{
public static void main(String args[])
{
System.out.println("\f");
Scanner sc=new Scanner(System.in);
System.out.println("Enter a character");
char ch = sc.next().charAt(0);
if(Character.isLetter(ch))
{
System.out.println(ch+"is a letter");
if(Character.isUpperCase(ch))
System.out.println(ch +" is in upper Case");
else
System.out.println(ch +" is in lower Case");
}
else if(Character.isDigit(ch))
System.out.println(ch +" is a digit");
else
System.out.println(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 java.util.*;
public class removeVowel
{
public static void main(String args[])
{
System.out.println("\f");
String w="";
Scanner sc=new Scanner(System.in);
System.out.println("Enter a String");
String st=sc.nextLine();
for(int i=0;i<st.length();i++)
{
char x=st.charAt(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;
}
System.out.println(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 java.util.*;
public class countNonvowel
{
public static void main(String args[])
{
System.out.println("\f");
int c=0;
Scanner sc=new Scanner(System.in);
System.out.println("enter a string");
String st=sc.nextLine();
for(int i=0;i<st.length();i++)
{
char x=st.charAt(i);
if(x!=' '&& x!='A' && x!='E' && x!='I' && x!='O' && x!='U' &&
x!='a' && x!='e' &&x!='i' && x!='o' && x!='u')
c++;
}
System.out.println("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 java.util.*;
public class nameInitLength
{
public static void main(String args[])
{
System.out.println("\f");
Scanner sc=new Scanner(System.in);
System.out.println("Enter a String");
String st=sc.nextLine();
st=st.trim();
st=' '+st;
for(int i=0;i<st.length();i++)
{
char x=st.charAt(i);
if(x==' ')
System.out.print(st.charAt(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 java.util.*;
public class surnameFirstmiddle
{
public static void main(String args[])
{
System.out.println("\f");
Scanner sc=new Scanner(System.in);
System.out.println("Enter a String");
String st=sc.nextLine();
st=st.trim();
int n= st.length();
int first=st.indexOf(' ');
int last=st.lastIndexOf(' ');
st=st.substring(last,n)+' '+st.substring(0,last);
System.out.println(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 java.util.*;
public class charFreq
{
public static void main(String args[])
{
System.out.println("\f");
int c=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a String");
String st=sc.nextLine();
System.out.println("Enter an alphabets whose frequency is to be
checked: ");
char ch=sc.next().charAt(0);
for(int i=0;i<st.length();i++)
{
char x=st.charAt(i);
if(x==ch)
c++;
}
System.out.println("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 java.util.*;
public class wordCorrect
{
public static void main(String args[])
{
System.out.println("\f");
String st="My name is Alok Kumar Gupta and my age is 45
years";
st=st.replace("Alok","Ashok");
st= st.replace("45","35");
System.out.println(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 java.util.*;
public class longestWord
{
public static void main(String args[])
{
System.out.println("\f");
int max_len=0,len=0;
String w="",max_word="";
Scanner sc=new Scanner(System.in);
System.out.println("Enter a String");
String st=sc.nextLine();
st=st.trim();
st=st+' ';
for(int i=0;i<st.length();i++)
{
char x=st.charAt(i);
if(x!=' ')
w=w+x;
else
{
len=w.length();
if(len>max_len)
{
max_word=w;
max_len=len;
}
w="";
}
}
System.out.println("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 java.util.*;
public class vowelFreq
{
public static void main(String args[])
{
System.out.println("\f");
int ACOU=0,ECOU=0,ICOU=0,OCOU=0,UCOU=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a String");
String st=sc.nextLine();
for(int i=0;i<st.length();i++)
{
char x=st.charAt(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++;
}
System.out.println("FREQUENCY OF 'A' ="+ACOU);
System.out.println("FREQUENCY OF 'E' ="+ECOU);
System.out.println("FREQUENCY OF 'I' ="+ICOU);
System.out.println("FREQUENCY OF 'O' ="+OCOU);
System.out.println("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 java.util.*;
public class wordUpper
{
public static void main(String args[])
{
System.out.println("\f");
int f=0;
String w="";
Scanner sc=new Scanner(System.in);
System.out.println("Enter a String");
String st=sc.nextLine();
st= st.trim();
st=' '+st;
st= st.toLowerCase();
for(int i=0;i<st.length();i++)
{
char x=st.charAt(i);
if(x==' ')
{
w=w+x;
f=1;
continue;
}
if(f==1)
{
x=Character.toUpperCase(x);
f=0;
}
w=w+x;
}
System.out.println(w);
}
}
/* OR
for(int i=0;i<st.length();i++)
{
char x=st.charAt(i);
if(x==' ')
{
int n=st.length();
char y=
Character.toUpperCase(st.charAt(i+1));
st=st.substring(0,i+1)+y+st.substring(i+2,n);
}
}
System.out.println(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 java.util.*;
public class charAscii
{
public static void main(String args[])
{
System.out.println("\f");
Scanner sc=new Scanner(System.in);
System.out.println("enter a word");
String st=sc.next();
for(int i=0;i<=st.length()-1;i++)
{
char x=st.charAt(i);
System.out.println("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 java.util.*;
public class vowelReplace
{
public static void main(String args[])
{
int c=0;
String w="";
System.out.println("\f");
Scanner sc=new Scanner(System.in);
System.out.println("enter a string");
String st=sc.nextLine();
st=st.toUpperCase();
for(int i=0;i<=st.length()-1;i++)
{
char x=st.charAt(i);
if(x=='A'||x=='E'||x=='I'||x=='O'||x=='U')
{
x='*';
}
w=w+x;
}
System.out.println(w);
}
}
/** Write a program in java to enter a string and frame a word by joining all
the first characters of each word.Display the new word.
Sample i/p: Vital Information Resource Under Seize
Sample o/p: VIRUS */
import java.util.*;
public class wordFirstchar
{
public static void main(String args[])
{
String w="";
System.out.println("\f");
Scanner sc=new Scanner(System.in);
System.out.println("enter a string");
String st=sc.nextLine();
st=st.trim();
st=" "+st;
for(int i=0;i<st.length();i++)
{
char x= st.charAt(i);
if(x==' ')
w=w+st.charAt(i+1);
}
System.out.println(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 java.util.*;
public class palindromeWord
{
public static void main(String args[])
{
char x;
String s1="",s2="",rev="";
System.out.println("\f");
Scanner sc=new Scanner(System.in);
System.out.println("enter a string");
String st=sc.nextLine();
st= st.toUpperCase();
st=st.trim();
st=st+" ";
for(int i=0;i<st.length();i++)
{
x=st.charAt(i);
if(x!=' ')
{
s1=s1+x;
s2=x+s2;
}
else
{
if(s1.equals(s2))
System.out.println(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 java.util.*;
public class stringRev
{
public static void main(String args[])
{
System.out.println("\f");
String w="",s1="";
char x=' ';
Scanner sc= new Scanner(System.in);
System.out.println("enter a String:");
String st= sc.nextLine();
st=st.trim();
st=" "+st;
for(int i= st.length()-1;i>=0;i--)
{
x=st.charAt(i);
if(x!=' ')
w=x+w;
else
{
s1=s1+w+" ";
w="";
}
}
System.out.println(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 java.util.*;
public class Q18
{
public static void main(String args[])
{
System.out.println("\f");
char x=' ',ch=' ';
int c,l=0;
Scanner sc= new Scanner(System.in);
System.out.println("enter a string:");
String st= sc.nextLine();
st=st.trim();
for(int i=0;i<st.length();i++)
{
x=st.charAt(i);
c=0;
if(st.indexOf(x)!=st.lastIndexOf(x))
{
for(int j=i+1;j<st.length();j++)
{
if(x==st.charAt(j))
c++;
}
if(c>l)
{
ch=x;
l=c;
}
}
}
System.out.println("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 java.util.*;
public class Q19
{
public static void main(String args[])
{
System.out.println("\f");
char x;
int c=0,f=0,l=0;
String w="",max="";
Scanner sc =new Scanner(System.in);
System.out.println("enter a string");
String st=sc.nextLine();
st=st.trim();
st=st+" ";
for(int i=0;i<st.length();i++)
{
x=st.charAt(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;
}
System.out.println("max="+max+" l= "+l);
}
System.out.println("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 java.util.*;
public class Q20
{
public static void main(String args[])
{
System.out.println("\f");
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string:");
String st=sc.nextLine();
st=st.replace("Blue","Red");
System.out.println(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 java.util.*;
public class Q21
{
public static void main(String args[])
{
System.out.println("\f");
Scanner sc=new Scanner(System.in);
System.out.println("enter a 3 word name:");
String st=sc.nextLine();
st=st.trim();
int first=st.indexOf(' ');
int last=st.lastIndexOf(' ');
String s1=st.substring(0,first)+"
"+st.substring(last+1,st.length());
System.out.println(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 java.util.*;
public class Q22
{
public static void main(String args[])
{
System.out.println("\f");
char x;
Scanner sc= new Scanner(System.in);
System.out.println("enter a string");
String st=sc.nextLine();
st=st.toLowerCase();
for(int i=0;i<st.length();i++)
{
x=st.charAt(i);
if(x=='a'||x=='e'||x=='i'||x=='o'||x=='u')
st=st.replace(x,++x);
}
System.out.println(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 java.util.*;
public class Q23
{
public static void main(String args[])
{
String s1;
char x=0;
System.out.println("\f");
Scanner sc = new Scanner(System.in);
System.out.println("enter a string:");
String st= sc.nextLine();
for(int i=0;i<st.length()-1;i++)
{
x=st.charAt(i);
if(st.indexOf(x)!=st.lastIndexOf(x))
{
for(int j=i+1;j<st.length();j++)
{
char y=st.charAt(j);
if(x==y)
st=st.substring(0,j)
+st.substring(j+1,st.length());
}
System.out.println(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 java.util.*;
public class Q24
{
public static void main(String args[])
{
System.out.println("\f");
int f=0;
char x;
Scanner sc= new Scanner(System.in);
System.out.println("enter a string:");
String st=sc.nextLine();
for(int i=0;i<st.length();i++)
{
x=st.charAt(i);
if(st.indexOf(x)!=st.lastIndexOf(x))
{
System.out.println("NOT A UNIQUE STRING");
f=1;
break;
}
}
if(f==0)
System.out.println("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 java.util.*;
public class Q25
{
public static void main(String args[])
{
System.out.println("\f");
char x;
int c=0;
String w="";
Scanner sc =new Scanner(System.in);
System.out.println("enter a string");
String st=sc.nextLine();
st=st.trim();
st=st+" ";
for(int i=0;i<st.length();i++)
{
x=st.charAt(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
{
System.out.println(" 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 java.util.*;
public class Q26
{
public static void main(String args[])
{
System.out.println("\f");
Scanner sc=new Scanner(System.in);
System.out.println("enter a string");
String st=sc.nextLine();
st=st.trim();
for(int i=st.length()-1;i>=0;i--)
{
char x=st.charAt(i);
st=st+x;
}
System.out.println(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 java.util.*;
public class Q27
{
public static void main(String args[])
{
int c=0,f=0;
char x;
System.out.println("\f");
Scanner sc=new Scanner(System.in);
System.out.println("enter a string");
String st=sc.nextLine();
st=st.trim();
st=st.toUpperCase();
System.out.println("Character\tFrequency");
for(char i='A';i<'Z';i++)
{
f=0;c=0;
for(int j=0;j<st.length();j++)
{
x=st.charAt(j);
if(x==i)
{
f=1;
c++;
}
}
if(f==1)
System.out.print(" "+i+"\t \t"+c+"\n");
}
}
}