You are on page 1of 15

1.

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.*;

public class longest_word

public static void main(String args[])

Scanner sc= new Scanner(System.in);

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="";

System.out.println("Longest word="+ max_word);

System.out.println("Length of the longest word="+max_len);

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.

Sample Input: COMPUTER

Sample Output: Unique String

import java.util.*;

public class unique_string

public static void main(String args[])

Scanner sc= new Scanner(System.in);

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)

System.out.println(st+" is not a unique string");

else

System.out.println(st+" is a unique string");

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

vowel being shifted towards the end followed by “AY”.

Sample Input 1: London

Output: ONDONLAY

Sample Input 2: Olympics

Output: OLYMPICSAY

import java.util.*;

public class PigLatin


{
public static void main(String args[])
{

Scanner sc = new Scanner(System.in);


System.out.print("Enter any word: ");
String s = sc.next();
int len = s.length();
s=s.toUpperCase();
String p="";
int k=0;

for(int i = 0; i < len; i++)


{
char ch = s.charAt(i);
if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
{
p=s.substring(i) + s.substring(0,i) + "AY";
k=1;
break;
}
}

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

vowels in the string.

Eg : S=“BEAUTIFUL” Output — “LUFITUAEB”

No. of vowels = 5

import java.util.*;

public class rcv

public static void main(String args[])

Scanner sc= new Scanner(System.in);

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++;

System.out.println("Reversed string ="+rev);

System.out.println("number of vowels ="+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

public static void main(String args[])

Scanner sc= new Scanner(System.in);

System.out.println("Enter first string");

String s=sc.nextLine();

System.out.println("Enter second string");

String s1=sc.nextLine();

s=s.toUpperCase();

s1=s1.toUpperCase();

6
int len1=s.length();

int len2=s1.length();

if(s.equals(s1))

System.out.println("Both strings are equal");

else

if(len1>len2)

System.out.println(s+ " is the string with highest length");

else if(len1<len2)

System.out.println(s1+ " is the string with highest length");

else

System.out.println("both the strings are of equal length");

}}

7. 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 = 76

ASCII of U = 85

ASCII of E = 69

ASCII of J = 74

import java.util.*;

public class ASCII_code

7
{

public static void main(String args[])

Scanner sc= new Scanner(System.in);

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);

System.out.println(ch+ "\t" + (int)ch);

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.

Sample Input: achieve

Sample Output: Given word contains consecutive letters.

import java.util.*;

public class consecutive_letter

public static void main(String args[])

Scanner sc= new Scanner(System.in);

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)

System.out.println("Given word contains consecutive Letters");

else

System.out.println("Given word does not contains consecutive Letters");

}}

9. Write a program to accept a string and print the string replacing

the vowels with‘*’,consonants with ‘@’ and other characters with ‘#’

Ex: BEAUTIFUL@123OUTPUT:@***@*@*@####

import java.util.*;

9
public class replace_characters

public static void main(String args[])

Scanner sc= new Scanner(System.in);

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 Input: MODEM IS AN ELECTRONIC DEVICE

Sample Output:

MODEM

DEVICE

import java.util.*;

public class consecutive_word

public static void main(String args[])

Scanner sc= new Scanner(System.in);

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="";

}}

System.out.println("frequency of consecutive word="+count);

11. Write a program in Java to enter a sentence. Display the words which are only palindrome.

Sample Input: MOM AND DAD LOVES ME

Sample Output: MOM DAD

import java.util.*;

12
public class palindrome_word

public static void main(String args[])

Scanner sc= new Scanner(System.in);

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:

(i) Number of words present 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 java.util.*;

public class word_letters

public static void main(String args[])

Scanner sc= new Scanner(System.in);

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;

System.out.println("Number of letters ="+l);

System.out.println("Number of words ="+w);

15

You might also like