Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

1. Define a java class to accept a string to find number of vowels.

import java.util.Scanner ;
class NOV
{
String str;
int len;
int i;
int numov;
char x ;
public void main()
{
Scanner st = new Scanner (System.in);
System.out.println ( " Enter a STRNG");
str = st.next();
len =str.length();
str = str.toLowerCase();
numov=0;
for (i=0;i<len;i++)
{
x=str.charAt(i);
if ((x=='a') || (x=='e') || (x=='i') || (x=='o') || (x=='u'))
numov++;
}
System.out.println ( "NUMBER OF OVELS " + numov);
}
}
2. Define a java class to accept a string to find number of consonants

import java.util.Scanner ;
class conso
{
String str;
int len;
int i;
int numcon;
char x ;
public void main()
{
Scanner st = new Scanner (System.in);
System.out.println ( " Enter a STRNG");
str = st.next();
len =str.length();
str = str.toLowerCase();
numcon=0;
for (i=0;i<len;i++)
{
x=str.charAt(i);
if ((x=='a') || (x=='e') || (x=='i') || (x=='o') || (x=='u'))
numcon++;
}
System.out.println ( "NUMBER OF OVELS " + (len-numcon));
}
}
3. Define a java class to accept a string to print it in reverse.
Sample input : stanns
Sample output : snnts
import java.util.Scanner ;
class reverse
{
String str;
int len;
int i;
String rev;
char x ;
public void main()
{
Scanner st = new Scanner (System.in);
System.out.println ( " Enter a STRNG");
str = st.next();
len =str.length();
rev=" " ;
for (i=len-1; i>=0; i--)
{
x=str.charAt(i);
rev= rev + x ;

}
rev=rev.trim();
System.out.println( " ACCEPTED STRING " + str);
System.out.println ( "REVERSE OF THE STRIGN " + rev);
}
}
4. Define a java class to accept a string to check whether it is a palindrome or not.
Sample input : MADAM
Sample output : MADAM

import java.util.Scanner ;
class palin
{
String str;
int len;
int i;
String rev;
char x ;
public void main()
{
Scanner st = new Scanner (System.in);
System.out.println ( " Enter a STRNG");
str = st.next();
len =str.length();
rev=" " ;
for (i=len-1; i>=0; i--)
{
x=str.charAt(i);
rev= rev + x ;

}
rev=rev.trim();
if (str.equals(rev))
System.out.println( " ACCEPTED STRING is PALINDROME " + str);
else
System.out.println ( " ACCEPTED STRING IS NOT A PALINDROME " + str);
}
}
5. Define a class to accept a word and display the same in Pig Latin
form.
Sample input : TROUBLE
Sample output : OUBLETRAY
A word is converted into its Pig Latin form by using the following steps:
Find the index of the first vowel.
Obtain two substrings; one say s by using the characters from beginning
till index of first vowel-1 and other say S2 by using the characters from
index of vowel till the end of the string
Now Find S3 = s2 + s1+ “AY”.
The resulting string s3 will be in PIG LATIN form of the given string.

import java.util.Scanner;
public class piglatin
{
String x;
String y;
String z;
String a ;
int l;
int i;
char b;
piglatin()
{
x=y=z=a=" ";
}
public void main()
{
Scanner on = new Scanner (System.in);
System.out.println ( "Etner a string " );
x = on.next();
l = x.length();
for (i=0;i<l;i++)
{
b=x.charAt(i);
if ( b=='a' || b=='e'||b=='i' || b=='o' || b=='u')
{
break;
}
}
y =x.substring (i);
z= x.substring (0,i);
a = y + z + "ay";
System.out.println ( " PIGLATIN WORD " + a);
}
}

6. Define a java class to accept a string to print each alternative


character in capitals.
import java.util.Scanner;
class alternative
{
String str;
int len;
int i ;
char x;
String utd;
public void main ()
{
Scanner st = new Scanner (System.in);
System.out.println ( " Enter a string " ) ;
str = st.next();
len = str.length();
str = str.toLowerCase();
utd=" ";
for ( i=0;i<len ; i++)
{
x=str.charAt(i);
if ( i%2==0)
x = Character.toUpperCase(x);
utd = utd + x ;
}
System.out.println ( " Accepted string " + str ) ;
System.out.println ( " Modified string " + utd ) ;
}
}

7. 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.Scanner;
class spequest
{
String str1;
String str2;
int len;
int len1;
public void main ()
{
Scanner st = new Scanner (System.in);
System.out.println ( " Enter a string " ) ;
str1 = st.next();
len = str1.length();
System.out.println ( " Enter a string " ) ;
str2 = st.next();
len1 = str2.length();
str1=str1.toUpperCase();
str2=str2.toUpperCase();
if (str1.equals(str2))
{
System.out.println ( str1 + " and " + str2 + " are equal " );
}
else
{
if ( len1>len)
{
System.out.println (str2+ " is longest String " ) ;
}
else
{
System.out.println ( str1 + " is longest String " ) ;
}
}
}
}
8. Define a class to accept a string, convert it into lowercase and check
whether the string is a palindrome or not. A palindrome is a word which
reads the same backward as forward. Example: madam, racecar etc.

import java.util.Scanner ;
class palin8
{
String str;
int len;
int i;
String rev;
char x ;
public void main()
{
Scanner st = new Scanner (System.in);
System.out.println ( " Enter a STRNG");
str = st.next();
str= str.toLowerCase();
len =str.length();
rev=" " ;
for (i=len-1; i>=0; i--)
{
x=str.charAt(i);
rev= rev + x ;

}
rev=rev.trim();
if (str.equals(rev))
System.out.println( " ACCEPTED STRING is PALINDROME " + str);
else
System.out.println ( " ACCEPTED STRING IS NOT A PALINDROME " +
rev);
}
}
9. Define a class to accept a string to calculate and print sum of each
characters ascii code.
import java.util.Scanner ;
class SumOfAscii
{
String str;
int len;
int i;
int sum;
char x ;
public void main()
{
Scanner st = new Scanner (System.in);
System.out.println ( " Enter a STRNG");
str = st.next();
len =str.length();
for (i=len-1; i>=0; i--)
{
x=str.charAt(i);
sum = sum + x ;
}
System.out.println ( "Sum of Ascii Code " + sum );
}
}

10. Define a class to accept a string to display each letter of word


entered by the user. Sample : bluej
B
L
U
E
J

import java.util.Scanner ;
class PRIDILI
{
String str;
int len;
int i;
int numov;
char x ;
public void main()
{
Scanner st = new Scanner (System.in);
System.out.println ( " Enter a STRNG");
str = st.next();
len =str.length();
str = str.toUpperCase();
numov=0;
for (i=0;i<len;i++)
{
x=str.charAt(i);
System.out.println (x);

}
}
}
11. Define a java class to accept a word in lowercase and display the
new word after removing all the repeated letters.
import java.util.Scanner;
public class dupchar
{
public static void main()
{
Scanner st = new Scanner (System.in);
String s1,s2="";
System.out.println ( " Enter a string ");
int i,p,j,t;
s1=st.next();
char ch1, ch2;
p=s1.length();
for(i=0;i<p;i++)
{
ch1=s1.charAt(i);
t=0;
for(j=0;j<s2.length();j++)
{
ch2=s2.charAt(j);
if (ch1==ch2)
t=1;
}
if (t==0)
s2=s2+ch1;
}
System.out.println ( " The new String after removing duplicate characters" + s2);
}
}
12. Define a class to accept a word to arrange and print it in
Alphabetical order.
public class alpha
{
public void alpha ( String x )
{
String re; re=" ";
int len; len=0;
int i ; i=0;
char ch ; ch=' ';
len = x.length();
x=x.toUpperCase();
for ( int al=65 ; al<=91 ; al++)
{
for (i=0;i<len; i++)
{
ch = x.charAt(i);
if (al == ch )
re =re + ch;
}
}
System.out.println( " Alphabetical order " + re);
}
}
13.Define a class to accept a word to print in the following pattern
BLUEJ
LUEJ
UEJ
EJ
J
public class pat
{
public static void main (String str)
{
int len = str.length();
for ( int i=0;i<len ; i++)
{
System.out.println( str.substring(i));
}
}
}

14. Define a class to accept a word to print in the following pattern


BLUEJ
BLUE
BLU
BL
B
public class pat2
{
public static void main (String str)
{
int len = str.length();
for ( int i=len ; i>0;i--)
{
System.out.println( str.substring(0,i));
}
}
}

You might also like