String Programme
String Programme
String Programme
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);
}
}
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 );
}
}
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));
}
}
}