String pgms second set
[Link] to get the pattern
J
JA
JAV
JAVA
//to print the pattern
import [Link].*;
public class pattern1
{
public void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the String");
String a = [Link]();
int l = [Link]();
for(int i=0;i<=l;i++)
[Link]([Link](0,i));
}}
[Link] to get the pattern
JAVA
JAV
JA
J
//to print the pattern
import [Link].*;
public class pattern2
{
public void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the String");
String a = [Link]();
int l = [Link]();
for(int i=l;i>=1;i--)
[Link]([Link](0,i));
}
}
[Link] to get the pattern
A
VA
AVA
JAVA
//PGM to print the pattern
import [Link].*;
public class pattern3
{
public void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the String");
String a = [Link]();
int l = [Link]();
for(int i=l;i>=0;i--)
[Link]([Link](i));
}
}
[Link] to get the pattern
JAVA
AVA
VA
A
//to print the pattern
import [Link].*;
public class pattern4
{
public void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the String");
String a = [Link]();
int l = [Link]();
for(int i=0;i<=l;i++)
[Link]([Link](i));
}
}
[Link] to interchange the surnames of two names
Eg- input
JOHN SMITH
MARK ANTONY
OUTPUT
JOHN ANTONY
MARK SMITH
import [Link].*;
public class Last_Name_Swaper
{
public void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the 1st full name.");
String a =[Link]();
[Link]("Enter the 2nd full name.");
String b = [Link]();
int p=[Link](' ');
int q=[Link](' ');
String c=[Link] (0,p)+[Link](q);
String d=[Link] (0,q)+[Link](p);
[Link]("Swapped name 1 = "+ c);
[Link]("Swapped name 2 = "+ d);
}
}
[Link] to interchange the name and surname
Eg-
Input MINNAL MURALI
Output MURALI MINNAL
import java .util.*;
public class Surname
public void main()
Scanner sc=new Scanner([Link]);
[Link]("Enter the String having 2 parts");
String a=[Link]();
int p= [Link](' ');
[Link]([Link](p)+" "+[Link](0,p));
[Link] to create initials .
Eg ADITHYA SUNIL NAIR
[Link]
Another eg - To print Mohandas Karamchand Gandhi as [Link]
//Pgm to create initials
import java .util.*;
public class initialpgm
public void main()
Scanner sc=new Scanner([Link]);
[Link]("Enter the first String having 3 parts");
String a=[Link]();
[Link]([Link](0)+".");
int p=[Link](' ');
int q=[Link](' ');
[Link]([Link](p+1)+".");
[Link]([Link](q));
}}
8) pgm to print shortform
Eg Input-This is a cat
Output - T.I.A.C.
//To print shortform of a string
import java .util.*;
public class shortform
public void main()
Scanner sc=new Scanner([Link]);
[Link]("Enter the String");
String a=[Link]();
String ns="";
a=[Link]();//To delete the leading and trailing blank spaces in a sentence
a=" "+a;//to add a space in the beginning
int l=[Link](); //to find the length of the string
for(int i=0;i<l;i++)
if([Link](i)==' ')
ns=ns+[Link]([Link](++i))+".";
[Link](ns);
}}
9)pgm to print piglatin string.(consonants at the beginning will be transferred to
the last word +"AY")
Eg -input: Trash
output : ASHTRAY
//pgm to to print piglatin string
import [Link].*;
public class Piglatin
public void main()
String b="";int i;
Scanner sc= new Scanner([Link]);
[Link]("Enter the String");
String a = [Link]();
a=[Link]();
int l=[Link]();
for(i=0;i<l;i++)
char x=[Link](i);
if(x=='A'||x=='E'||x=='I'||x=='O'||x=='U')
break;
b=b+[Link](i)+[Link](0,i)+"AY";
[Link]("PigLatin: "+b);
}}
10) Write a program in java to accept a string/word and display the new string
after removing all the vowels present in it.
SampLe i/p: COMPUTER
Sample o/p: CMPTR
import [Link].*;
public class Q4
public void main()
String b="";
Scanner sc=new Scanner([Link]);
[Link]("Enter a String");
String a=[Link]();
int l=[Link]();
for(int i=0;i<l;i++)
char x=[Link]([Link](i));
if(x=='A' || x=='E' || x=='I' || x=='O' ||x=='U' )
continue;
else
b=b+x;
[Link](b);
11) Write a program to accept a word & convert it into lower case, if it is in upper
case. Display a new word by replacing only the vowels with the character
following it.
Sample i/p: Computer
Sample o/p: cpmpvtfr
import [Link].*;
public class Q22
public void main()
char k;
Scanner sc= new Scanner([Link]);
[Link]("enter a string");
String a=[Link]();
a=[Link]();
int l=[Link]();
for(int i=0;i<l;i++)
k=[Link](i);
if(k=='a'||k=='e'||k=='i'||k=='o'||k=='u')
a=[Link](k,++k);
[Link](a);
12)/*Frequency of double letter in a string/word
Eg: input Rabbit
Output
Frequency of double letter in the string is=1
Input - Mummy was feeding the rabbit
Output
Frequency of double letter in the string is=3
import [Link].*;
public class DoubleLetterSeq
{
public void main()
Scanner sc = new Scanner([Link]);
[Link]("Enter string: ");
String s = [Link]().toUpperCase();
int c=0;
int l = [Link]();
for (int i = 0; i < l-1 ; i++)
if ([Link](i) == [Link](i + 1))
c++;
[Link]("Frequency of Double Letter in the string = " + c);
}}
13)pgm to accept a string ‘ January 26 is celebrated as Republic day of India’
And Generate the output using replace function
August 15 is celebrated as Independence day of India
//To change 26 to 15, January to August and Republic to Independence
import [Link].*;
public class Q21
public void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a string:");
String st=[Link]();
st=[Link]("January","August");
st=[Link]("26","15");
st=[Link]("Republic","Independence");
[Link](st);
14)Write a program to accept a word and display the ASCII of each character.
Sample i/p: BLUEJ
Sample o/p: ASCII of B = 66
ASCII of L = 75
ASCII OF U = 84
ASCII OF E = 69
ASCII of J = 73
// ASCII value of each character
import [Link].*;
public class Q13
public void main()
Scanner sc=new Scanner([Link]);
[Link]("enter a word");
String st=[Link]();
for(int i=0;i<=[Link]()-1;i++)
char x=[Link](i);
[Link]("ASCII of "+x+" = "+(int)x);
15)To print the worth of a word
i.e Worth of a word = Sum of positions of letters in the English Alphabet
eg: Worth of the word "HI" = 8+9=17
//Pgm to print worth of a word
import [Link].*;
public class Worth_of_a_word
public void main()
Scanner sc = new Scanner([Link]);
[Link]("Enter the word");
String a= [Link]();
int i,l=[Link](),worth=0;
for(i=0;i<l;i++)
{
char ch=[Link](i);
if(ch>='A'&& ch<='Z')
worth+=(int)(ch)-64); or (int)ch-64; or ( int)(ch-64);
else
worth+=(int)(ch)-96); or(int) ch-96; or (int)(ch-96);
[Link]("Worth ="+worth);
Same pgm another method
import [Link].*;
public class Worth_of_a_word1
public void main()
Scanner sc = new Scanner([Link]);
[Link]("Enter the word");
String a= [Link]();
a=[Link]();
int i,l=[Link](),worth=0;
for(i=0;i<l;i++)
{
char ch=[Link](i);
worth+=(int)(ch)-64); or (int)ch-64; or ( int)(ch-64);
[Link]("Worth ="+worth);