String Functions
String Functions
import java.util.*;
public class Q1
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any sentence");
String s=sc.nextLine();
int w=0;
char ch;
int l=s.length();
for(int i=0;i<l;i++)
{
ch=s.charAt(i);
if(ch==' ')
w++;
}
int a=l-w;
System.out.println("Number of words="+(w+1));
System.out.println("Number of Alphabets="+a);
}
Program 2
Write a program in Java to accept a word/a String and display the new string after removing all the
vowels present in it.
Sample Input: COMPUTER APPLICATION
Sample Output: CMPTRPPLCTNS
import java.util.*;
public class Q2
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any sentence");
String s=sc.nextLine();
int l=s.length();
String s1="";
for(int i=0;i<l;i++)
{
char ch=s.charAt(i);
if(ch!='a'&&ch!='e'&&ch!='i'&&ch!='o'&&ch!='u'&&ch!='A'&&ch!='E'&&ch!='I'&&ch!='O'&&
ch!='U')
s1=s1+ch;
}
System.out.println(s1);
}
}
Program 3
Write a program in Java to accept a name(Containing three words) and Display only the initials(i.e.
first letter of each word).
Sample Input: LAL KRISHNA ADVANI
Sample Output: L K A
import java.util.*;
public class Q3
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any name");
String s=sc.nextLine();
s=" "+s;
int l=s.length();
String s1="";
for(int i=0;i<l;i++)
{
char ch=s.charAt(i);
if(ch==' ')
s1=s1+s.charAt(i+1)+" ";
}
System.out.println(s1);
}
}
Program 4
Write a program in Java to accept a name containing three words and display the surname first,
followed by the first and middle names.
Sample Input: MOHANDAS KARAMCHAND GANDHI
Sample Output: GANDHI MOHAMDAS KARAMCHAND
import java.util.*;
public class Q4
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any name");
String s=sc.nextLine();
int id=s.lastIndexOf(' ');
String s1=s.substring(0,id);
String s2=s.substring(id+1);
System.out.println(s2+" "+s1);
}
}
Program 5
Write a program to accept a word in lower case and display the new word after removing all the
repeated letters.
Sample Input: application
Sample Output: aplictons
import java.util.*;
public class Duplicate
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any word");
String s=sc.next();
String s1="";
char ch,ch1;
int l=s.length();
int i,j,k;
for(i=0;i<s.length();i++)
{
ch=s.charAt(i);
s1=s.substring(0,i+1);
for(j=i+1;j<s.length();j++)
{
ch1=s.charAt(j);
if(ch==ch1)
s1=s1+"";
else
s1=s1+ch1;
}
s=s1;
s1="";
}
System.out.println(s);
}
Program 6
Write a program in Java to accept a word and display the ASCII code of each character of the word.
Sample input : BLUEJ
Sample Output:
ASCII of B = 66
ASCII of L = 75
ASCII of U = 84
ASCII of E = 69
ASCII of J = 73
import java.util.*;
public class Q6
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any word");
String s=sc.nextLine();
int l=s.length();
char ch;
for(int i=0;i<l;i++)
{
ch=s.charAt(i);
System.out.println("ASCII of "+ch+" = "+(int)ch);
}
}
}
Program 7
Write a program in Java to accept a String in upper case and replace all the vowels present in the
String with Asterisk(*) sign.
Sample Input: “TATA STEEL IS IN JAMSHEDPUR”
Sample Output: T*T* ST**L *S *N J*MSH*DP*R
import java.util.*;
public class Q7
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any word");
String s=sc.nextLine(),s2="";
int l=s.length();
s=s.toUpperCase();
char ch;
for(int i=0;i<l;i++)
{
ch=s.charAt(i);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
s2=s2+"*";
else
s2=s2+ch;
}
System.out.println(s2);
}
}
Program 8
Write a program in Java to enter a sentence . Frame word by joining all the first characters of each
word of the sentence. Display the word.
Sample Input: Vital Information Resource Under Seize
Sample Output: VIRUS
import java.util.*;
public class Q8
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any word");
String s=sc.nextLine(),s1="";
s=" "+s;
int l=s.length();
for(int i=0;i<l;i++)
{
char ch=s.charAt(i);
if(ch==' ')
s1=s1+s.charAt(i+1);
}
System.out.println(s1);
}
}
Program 9
Write a program in Java to accept a word and check whether the word is palindrome or not.
import java.util.*;
public class Palindrome
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any word");
String w=sc.next();
String w1="";
int l=w.length();
char ch;
for(int i=0;i<l;i++)
{
ch=w.charAt(i);
w1=ch+w1;
}
if(w.equalsIgnoreCase(w1))
System.out.println("It is a Palindrome Word");
else
System.out.println("It is not a Palindrom Word");
}
}
Program 10
Write a program to accept a sentence. Display the sentence in reversing order of its word.
Sample Input: Computer is Fun
Sample Output: Fun is Computer
import java.util.*;
public class Q10
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any word");
String s=sc.nextLine(),s1="",s2="";
s=s+" ";
int l=s.length();
for(int i=0;i<l;i++)
{
char ch=s.charAt(i);
if(ch!=' ')
s1=s1+ch;
else
{
s2=s1+" "+s2;
s1="";
}
}
System.out.println(s2);
}
Program 11
Write a program to input a word. Create a new word by replacing each consonant with the
previous letter. If the previous letter is a vowel than replace it with the next letter. (if the letter is
B then replace with C as the previous letter of B is A)Other characters must remain the same.
Display the new word.
Sample Input : CAPITAL
Sample Output: BAQISAK
import java.util.*;
public class Replace
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any word");
String w=sc.next().toUpperCase();
String w1="";
int l=w.length();
char ch='\u0000',ch1='\u0000';
for(int i=0;i<l;i++)
{
ch=w.charAt(i);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
w1=w1+ch;
else
{
if(ch!='A'||ch!='E'||ch!='I'||ch!='O'||ch!='U')
ch1=(char)(ch-1);
if(ch1=='A'||ch1=='E'||ch1=='I'||ch1=='O'||ch1=='U')
ch1=(char)(ch+1);
w1=w1+ch1;
}
}
System.out.println(w1);
}
}
Program 12
Consider the sentence as given below:
Blue bottle is in Blue bag lying on Blue carpet
Write a program to assign the given sentence to a string variable. Replace the word Blue with Red
at all its occurances. Display the new string as shown below:
Red bottle is in Red bag lying on Red carpet
import java.util.*;
public class Q12
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any word");
String s=”Blue bottle is in Blue bag lying on Blue carpet”, s1="",s2="";
s=s+" ";int l=s.length();
for(int i=0;i<l;i++)
{
char ch=s.charAt(i);
if(ch!=' ')
s1=s1+ch;
else
{
if(s1.equalsIgnoreCase("Blue"))
s1="Red";
s2=s2+" "+s1;
s1="";
}
}
System.out.println(s2);
}
}
Program 13
Write a program to accept a word and convert it into lower case, if it is in upper case. Display the
new word by replacing only the vowels with the letter following it.
Sample Input: computer
Sample Output: cpmpvtfr
import java.util.*;
public class Q13
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any word");
String s=sc.nextLine(),s1="",s2="";
int l=s.length();
s=s.toUpperCase();
for(int i=0;i<l;i++)
{
char ch=s .charAt(i);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
s1=s1+(char)(ch+1);
else
s1=s1+ch;
}
System.out.println(s1);
}}
Program 14
A string is said to be ‘Unique’ if none of the letters 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 Input: COMPUTER
Sample Output: Unique String
import java.util.*;
public class Q14
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any word");
String s=sc.nextLine(),s1="",s2="";
int l=s.length();int c=0;
for(int i=0;i<l;i++)
{
char ch=s .charAt(i);
for(int j=0;j<l;j++)
{
char ch1=s.charAt(j);
if(ch==ch1)
c++;
}
if(c>=2)
{
System.out.println("It is not a Unique String");
break;
}
else
c=0;
}
if(c==0)
System.out.println("It is a Unique String");
}
}
Program -15
A ‘Happy Word’ is defined as :
Take a word and calculate the word’s value based on position of the letters in English alphabet. On
the basis of word’s value, find the sum of the squares of its digits. Repeat the process with the
resultant number until the number equals 1 (one). If the number ends with 1 then the word is called
a ‘Happy Word’. Write a program to input a word and check whether it is a ‘Happy Word’ or not
import java.util.*;
public class Q15
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any word");
String s=sc.nextLine();String s1="";
int l=s.length();int c=0;
for(int i=0;i<l;i++)
{
char ch=s.charAt(i);
for(char ch1='A';ch<='z';ch1++)
{
if(ch!=ch1)
c++;
else
{
c++;
break;
}
}
s1=s1+Integer.toString(c);
c=0;
}
System.out.println(s1);
int n=Integer.valueOf(s1);
int m=n,sum=0,r;
while(m>9)
{
while(m!=0)
{
r=m%10;
sum=sum+(int)Math.pow(r,2);
m=m/10;
}
m=sum;
sum=0;
}
if(m==1)
System.out.println("A Happy Word");
else
System.out.println("Not A Happy Word");
}
}
Program -16
Write a program to input a sentence . Count and display the frequency of each letter of the
sentence in alphabetical order.
import java.util.*;
public class Q16
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any word");
String s=sc.nextLine();String s1="";
int l=s.length();int c=0;char ch;
for(int i=65;i<=90;i++)
{
for(int j=0;j<l;j++)
{
ch=s.charAt(j);
if(ch==(char)i)
c++;
}
if(c>=1)
System.out.println((char)i+ " Appears "+c+" Times");
c=0;
}
}
}
Program 17
Write a program to accept a string. Convert the string into upper case letter. Count and output the
number of double letter sequences that exist in the string.
import java.util.*;
public class Q17
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any word");
String s=sc.nextLine();String s1="";
s=s.toUpperCase();
int l=s.length();int c=0;char ch,ch1;
for(int i=0;i<l-1;i++)
{
ch=s.charAt(i);
ch1=s.charAt(i+1);
if(ch==ch1)
c++;
}
System.out.println("OUTPUT="+c);
}
}
Program 18
Write a program to accept a word . Check and display whether the word is a palindrome or only a
special word or none of them.
Special words are those words which starts and ends with the same letter.
Palindrome words are those words which read the same from left to right and vice-versa.
import java.util.*;
public class Q18
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any word");
String s=sc.nextLine();String s1="",s2="";
int l=s.length();
s=s.toUpperCase();
char ch,ch1;
ch=s.charAt(0);
ch1=s.charAt(l-1);
for(int i=0;i<l;i++)
{
s1=s.charAt(i)+s1;
}
if(ch==ch1 && s1.equals(s))
System.out.println("It is a palindrom word");
else if(ch==ch1)
System.out.println("It is a special word");
else
System.out.println("it is non of them");
}
}