QB String Prog Answers
QB String Prog Answers
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 word: 8
import java.util.*;
System.out.println("Enter string");
String s=sc.nextLine();
s=s+" ";
int len=s.length();
int max_len=0,word_len=0;
String max_word="";
String w="";
char ch;
for(int i=0;i<len;i++)
ch=s.charAt(i);
if(ch!=' ')
w=w+ch;
else
1
{
word_len=word.length();
if(word_len>max_len)
max_len=word_len;
max_word=w;
w="";
2. 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.
import java.util.*;
2
System.out.println("Enter string");
String s=sc.next();
int len=s.length();
char ch1,ch2;
int temp=0;
s=s.toUpperCase();
for(int i=0;i<len;i++)
ch1=s.charAt(i);
for(int j=i+1;j<len;j++)
ch2=s.charAt(j);
if(ch1==ch2)
temp=1;
break;
if(temp==1)
else
3
3. Write a program that encodes a word into Piglatin. To translate word into Piglatin word,
convert the word into uppercase and then place the first vowel of the original word as the
start of the new word along with the remaining alphabets. The alphabets present before the
Output: ONDONLAY
Output: OLYMPICSAY
import java.util.*;
if(k == 0)
{
p = s + "AY";
4
}
System.out.println(s + " in Piglatin form is " + p);
}
}
4.Define a class to accept a string and print the same in reverse, also print the number of
No. of vowels = 5
import java.util.*;
System.out.println("Enter string");
String s=sc.next();
int len=s.length();
String rev="";
char ch;
int count=0;
for(int i=0;i<len;i++)
ch=s.charAt(i);
rev=ch+rev;
5
for(int i=0;i<len;i++)
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')
count++;
6. Define a class to accept two strings, convert them into uppercase, check and display whether two
strings are equal or not, if the two strings are not equal, print the string with the highest length or print the
message both the strings are of equal length.
import java.util.*;
public class el
String s=sc.nextLine();
String s1=sc.nextLine();
s=s.toUpperCase();
s1=s1.toUpperCase();
6
int len1=s.length();
int len2=s1.length();
if(s.equals(s1))
else
if(len1>len2)
else if(len1<len2)
else
}}
7. Write a program in Java to accept a word and display the ASCII code of each character of the word.
Sample Output:
ASCII of B = 66
ASCII of L = 76
ASCII of U = 85
ASCII of E = 69
ASCII of J = 74
import java.util.*;
7
{
System.out.println("Enter string");
String s=sc.next();
int len=s.length();
System.out.println("Character"+"\t"+"Ascii code");
for(int i=0;i<len;i++)
char ch=s.charAt(i);
8. Write a program to accept a word. Convert the word to lowercase, check and display whether word
contains two consecutive letters or not. Consecutive letters are hose letters occurring in their natural
alphabetical order.
import java.util.*;
8
System.out.println("Enter string");
String s=sc.next();
int len=s.length();
char ch1,ch2;
int temp=0;
s=s.toLowerCase();
for(int i=0;i<len;i++)
ch1=s.charAt(i);
ch2=s.charAt(i+1);
if(ch1-ch2== -1)
temp=1;
break;
if(temp==1)
else
}}
the vowels with‘*’,consonants with ‘@’ and other characters with ‘#’
Ex: BEAUTIFUL@123OUTPUT:@***@*@*@####
import java.util.*;
9
public class replace_characters
System.out.println("Enter string");
String s=sc.next();
int len=s.length();
char ch;
for(int i=0;i<len;i++)
ch=s.charAt(i);
if(Character.isLetter(ch)==true)
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'||ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
ch='*';
else
ch='@';
else
ch='#';
System.out.print(ch);
10
10. Write a program to input a sentence. Convert the sentence into upper case letters. Display
the words along with frequency of the words which have at least a pair of consecutive letters.
Sample Output:
MODEM
DEVICE
import java.util.*;
System.out.println("Enter string");
String s=sc.nextLine();
s=s+" ";
int len=s.length();
char ch,ch1,ch2;
int temp=0;
int count=0;
String w="";
s=s.toUpperCase();
for(int i=0;i<len;i++)
ch=s.charAt(i);
if(ch!=' ')
11
{
w=w+ch;
else
for(int j=0;j<w.length()-1;j++)
ch1=w.charAt(j);
ch2=w.charAt(j+1);
if(ch1-ch2==-1)
count++;
System.out.println(w);
break;
w="";
}}
11. Write a program in Java to enter a sentence. Display the words which are only palindrome.
import java.util.*;
12
public class palindrome_word
System.out.println("Enter string");
String s=sc.nextLine();
s=s+" ";
int len=s.length();
String w="",rev="";
char ch;
for(int i=0;i<len;i++)
ch=st.charAt(i);
if(ch!=' ')
w=w+ch;
rev=ch+rev;
else
if(word.equalsIgnoreCase(rev))
System.out.println(w);
w="";
rev="";
13
}}}
11. Write a program to input a sentence. Find and display the following:
Assume that the sentence has neither include any digit nor a special character.*/
import java.util.*;
System.out.println("Enter string");
String s=sc.nextLine();
int len=s.length();
int count=0;
for(int i=0;i<len;i++)
char ch=s.charAt(i);
if(ch==' ')
count++;
int l=len-count;
14
int w=(len-l)+1;
15