0% found this document useful (0 votes)
40 views12 pages

String Functions

The document contains 18 Java programs that perform various string manipulations, including counting words and letters, removing vowels, displaying initials, reversing words, identifying palindromes, and more. Each program is structured with a main method that utilizes a Scanner for user input and includes logic to achieve the specified tasks. Sample inputs and outputs are provided for clarity on expected results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views12 pages

String Functions

The document contains 18 Java programs that perform various string manipulations, including counting words and letters, removing vowels, displaying initials, reversing words, identifying palindromes, and more. Each program is structured with a main method that utilizes a Scanner for user input and includes logic to achieve the specified tasks. Sample inputs and outputs are provided for clarity on expected results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Program 1

Write a program to input a sentence. Find and display the following:


i. Number of words in the sentence
ii. Number of letters present in the sentence
Assume that the sentence has neither include any digit nor a special character.

import [Link].*;
public class Q1
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("Enter any sentence");
String s=[Link]();
int w=0;
char ch;
int l=[Link]();
for(int i=0;i<l;i++)
{
ch=[Link](i);
if(ch==' ')
w++;
}
int a=l-w;
[Link]("Number of words="+(w+1));
[Link]("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 [Link].*;
public class Q2
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter any sentence");
String s=[Link]();
int l=[Link]();
String s1="";
for(int i=0;i<l;i++)
{
char ch=[Link](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;
}
[Link](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 [Link].*;
public class Q3
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter any name");
String s=[Link]();
s=" "+s;
int l=[Link]();
String s1="";
for(int i=0;i<l;i++)
{
char ch=[Link](i);
if(ch==' ')
s1=s1+[Link](i+1)+" ";
}
[Link](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 [Link].*;
public class Q4
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter any name");
String s=[Link]();
int id=[Link](' ');
String s1=[Link](0,id);
String s2=[Link](id+1);
[Link](s2+" "+s1);
}
}

Program 5
Write a program in Java to enter a String /Sentence and display the longest word and the length of
the longest word present in the String.
Sample Input: “TATA FOOTBALL ACADEMY WILL PLAY AGAINST MOHAN BAGAN”
Sample output: the longest word: FOOTBALL: The length of the longest word:8
import [Link].*;
public class Q5
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter any sentence");
String s=[Link]();
s=s+" ";
int l=[Link]();
int max =0;
int l1;
String s1="",s2="";
for(int i=0;i<l;i++)
{
char ch=[Link](i);
if(ch!=' ')
s1=s1+ch;
else
{
l1=[Link]();
if(l1>max)
{
max=l1;
s2=s1;
}
s1="";
}
}
[Link]("Longest Word +"+s2);
[Link]("Length of the word="+max);
}
}

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 [Link].*;
public class Q6
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter any word");
String s=[Link]();
int l=[Link]();
char ch;
for(int i=0;i<l;i++)
{
ch=[Link](i);
[Link]("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 [Link].*;
public class Q7
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter any word");
String s=[Link](),s2="";
int l=[Link]();
s=[Link]();
char ch;
for(int i=0;i<l;i++)
{
ch=[Link](i);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
s2=s2+"*";
else
s2=s2+ch;
}
[Link](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 [Link].*;
public class Q8
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter any word");
String s=[Link](),s1="";
s=" "+s;
int l=[Link]();
for(int i=0;i<l;i++)
{
char ch=[Link](i);
if(ch==' ')
s1=s1+[Link](i+1);
}
[Link](s1);
}
}

Program 9
Write a program in Java to enter a sentence. Display the word which are only palindrome.
Sample Input: MOM AND DAD ARE NOT AT HOME
Sample Output: MOM
DAD
import [Link].*;
public class Q9
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter any word");
String s=[Link](),s1="",s2="";
s=s+” “;
int l=[Link]();
for(int i=0;i<l;i++)
{
char ch=[Link](i);
if(ch!=' ')
{
s1=s1+ch;
s2=ch+s2;
}
else
{if([Link](s2))
[Link](s1);
s1="";
s2="";
}
}
}
}

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 [Link].*;
public class Q10
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter any word");
String s=[Link](),s1="",s2="";
s=s+" ";
int l=[Link]();
for(int i=0;i<l;i++)
{
char ch=[Link](i);
if(ch!=' ')
s1=s1+ch;
else
{
s2=s1+" "+s2;
s1="";
}
}
[Link](s2);
}

Program 11
Write a program to input a sentence and display the word of the sentence that contains maximum
number of vowels.
Sample Input: HAPPY NEW YEAR
Sample Output: The word with maximum number of vowels : YEAR
import [Link].*;
public class Q11
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter any word");
String s=[Link](),s1="",s2="";
s=s+" ";int c=0,max=0;
int l=[Link]();
for(int i=0;i<l;i++)
{
char ch=[Link](i);
if(ch!=' ')
s1=s1+ch;
else
{
s1=[Link]();
int l1=[Link]();
for(int j=1;j<l1;j++)
{
char ch1=[Link](j);
if(ch1=='A'||ch1=='E'||ch1=='I'||ch=='O'||ch=='U')
c++;
}
if(c>max)
{
max=c;
s2=s1;
}
s1="";
c=0;
}
}
[Link](“The word with maximum number of vowels:”+s2);
}
}

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 [Link].*;
public class Q12
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter any word");
String s=”Blue bottle is in Blue bag lying on Blue carpet”, s1="",s2="";
s=s+" ";int l=[Link]();
for(int i=0;i<l;i++)
{
char ch=[Link](i);
if(ch!=' ')
s1=s1+ch;
else
{
if([Link]("Blue"))

s1="Red";
s2=s2+" "+s1;
s1="";
}
}
[Link](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 [Link].*;
public class Q13
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter any word");
String s=[Link](),s1="",s2="";
int l=[Link]();
s=[Link]();
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;
}
[Link](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 [Link].*;
public class Q14
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter any word");
String s=[Link](),s1="",s2="";
int l=[Link]();int c=0;

for(int i=0;i<l;i++)
{
char ch=s .charAt(i);
for(int j=0;j<l;j++)
{
char ch1=[Link](j);
if(ch==ch1)
c++;
}
if(c>=2)
{
[Link]("It is not a Unique String");
break;
}
else
c=0;

}
if(c==0)
[Link]("It is a Unique String");
}
}

Program -15

import [Link].*;
public class Q15
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter any word");
String s=[Link]();String s1="";
int l=[Link]();int c=0;
for(int i=0;i<l;i++)
{
char ch=[Link](i);
for(char ch1='A';ch<='z';ch1++)
{
if(ch!=ch1)
c++;
else
{
c++;
break;
}
}
s1=s1+[Link](c);
c=0;
}
[Link](s1);
int n=[Link](s1);
int m=n,sum=0,r;
while(m>9)
{
while(m!=0)
{
r=m%10;
sum=sum+(int)[Link](r,2);
m=m/10;
}
m=sum;
sum=0;
}
if(m==1)
[Link]("A Happy Word");
else
[Link]("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 [Link].*;
public class Q16
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter any word");
String s=[Link]();String s1="";
int l=[Link]();int c=0;char ch;
for(int i=65;i<=90;i++)
{
for(int j=0;j<l;j++)
{
ch=[Link](j);
if(ch==(char)i)
c++;
}
if(c>=1)
[Link]((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 [Link].*;
public class Q17
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter any word");
String s=[Link]();String s1="";
s=[Link]();
int l=[Link]();int c=0;char ch,ch1;
for(int i=0;i<l-1;i++)
{
ch=[Link](i);
ch1=[Link](i+1);
if(ch==ch1)
c++;
}
[Link]("OUTPUT="+c);
}
}

Program 18

import [Link].*;
public class Q18
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter any word");
String s=[Link]();String s1="",s2="";
int l=[Link]();
s=[Link]();
char ch,ch1;
ch=[Link](0);
ch1=[Link](l-1);
for(int i=0;i<l;i++)
{
s1=[Link](i)+s1;
}
if(ch==ch1 && [Link](s))
[Link]("It is a palindrom word");
else if(ch==ch1)
[Link]("It is a special word");
else
[Link]("it is non of them");
}
}

You might also like