String Programs
String Programs
String Programs
STRING PROGRAM 1
A class Rearrange has been defined to modify a word by bringing all the vowels in the
word at the beginning followed by the consonants.
Some members of the class are given below:
Class name: Rearrange
Data Members:
String wrd
String newwrd
Member functions:
Rearrange( ) – default constructor initialize the variables
void readword( ) – to accept the sentence in uppercase
void freq_vow_cons( ) – finds the frequency of the vowels and consonants in the word
and displays them in an appropriate message
void arrange( ) – rearranges the word by bringing the vowels at the beginning followed
by the consonants
void display( ) – displays the original word along with the rearranged word
import java.util.*;
public class String1
{
Scanner sc = new Scanner(System.in);
String wrd,newwrd;
public String1( )
{
wrd="";
newwrd="";
}
public void readword( ) //to input the sentence
{
System.out.println("Enter a word ");
Page |2
wrd=sc.nextLine();
wrd=wrd.toUpperCase();
}
int f1=0,f2=0;
public void freq_vow_con() //to find the frequency of the vowels and consonants
{
for (int i=0; i<=(wrd.length()-1); i++)
{
char ch=wrd.charAt(i);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
{
f1++;
}
else
{
f2++;
}
}
System.out.println("Number of vowels = "+f1);
System.out.println("Number of consonants = "+f2);
}
ch=wrd.charAt(i);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
{
w1=w1+ch;
}
else if(ch!=' ')
{
w2=w2+ch;
}
}
newwrd=newwrd+w1+w2;
}
public void display() //to print the original and rearranged sentence
{
System.out.println("Original word = "+wrd);
System.out.println("The rearranged word = "+newwrd);
}
public void main()
{
String1 ob = new String1();
ob.readword();
ob.freq_vow_con();
ob.arrange();
ob.display();
}
}
OUTPUT
Page |4
Enter a word
the sky is blue and the grass is green
Number of vowels = 10
Number of consonants = 20
Original word = THE SKY IS BLUE AND THE GRASS IS GREEN
The rearranged word = EIUEAEAIEETHSKYSBLNDTHGRSSSGRN
STRING PROGRAM 2
Define a class Piglatin to find the piglatin of a string. A piglatin is the first vowel of the
original word becoming the start of the translation and any preceding letters being
shifted to the end and being followed by AY.
Class name : String2
Data members:
String txt – to store a word
Int len – to store the length of the string
Member functions:
String2( ) – constructor to initialize data members
void readstring( ) – to accept the word input in uppercase
void convert( ) – converts the word into piglatin form and displays it
void consonant( ) – counts and displays the number of consonants in the given word
import java.util.*;
public class String2
{
Scanner sc = new Scanner(System.in);
String txt;
int len;
public String2()
{
Page |5
txt="";
len=0;
}
public void readstring()
{
System.out.println("Enter the word ");
txt=sc.nextLine();
len=txt.length();
txt=txt.toUpperCase();
}
public void convert()
{
int pos = 0;
String s="";
for(int i=0; i<len; i++)
{
char ch = txt.charAt(i);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
{
pos=i;
break;
}
}
if(pos == 0)
{
s=txt+"AY";
}
else
{
s=txt.substring(pos)+txt.substring(0,pos)+"AY";
Page |6
OUTPUT
Enter the word
trouble
Piglatin word is OUBLETRAY
The number of consonants are 7
Page |7
STRING PROGRAM 3
Write a program to input a plain text and encrypt the text a per Ceasar Cypher.
Example : hello
O/P – uryyb
import java.util.*;
public class CaesarCipher
{
Scanner sc= new Scanner(System.in);
String s;
int x=0;
public void show()
{
System.out.println("Enter the text ");
s=sc.nextLine();
int l=s.length();
for(int i=0; i<l; i++)
{
char ch=s.charAt(i);
if(Character.isUpperCase(ch))
{
x=ch;
if(x<77)
{
System.out.print((char)(x+13));
}
else
{
System.out.print((char)(x-13));
Page |8
}
}
else if(Character.isLowerCase(ch))
{
x=ch;
if(x>109)
{
System.out.print((char)(x-13));
}
else
{
System.out.print((char)(x+13));
}
}
else
{
System.out.print(ch);
}
}
}
public void main()
{
CaesarCipher ob = new CaesarCipher();
ob.show();
}
}
OUTPUT
Enter the text
Page |9
NUMBER PROGRAM 1
{
if((isPrime(j)==true) && (isPrime(j+2)==true))
System.out.println(j+","+(j+2));
}
}
public void main( )
{
TwinPrime ob = new TwinPrime( );
ob.show();
}
NUMBER PROGRAM 2
Data members :
start : to store the start of range
end : to store the end of range
Methods/Member functions :
PrimePalin(int a, int b) : parameterized constructor to initialize the data
members start = a, end = b.
int isPrime(int i) : returns 1 if the number is prime otherwise returns 0
int isPalin(int i) : returns 1 if the number is palindrome otherwise returns 0
void generate( ) : generates all prime-palindrome numbers between start and
end by invoking the functions isPrime( ) and isPalin( )
P a g e | 11
}
if(rev==i)
return 1;
else
return 0;
}
public void generate( )
{
for(int x=start;x<=end;x++)
{
if(isPrime(x)==1 && isPalin(x)==1)
System.out.println(x);
}
}
public void main( )
{
PrimePalin ob=new PrimePalin(1 , 100);
ob.generate( );
}
}
NUMBER PROGRAM 3
Write a program to print the Tribonacci series upto n terms. These are series whose
first three terms are given, and every successive term is the sum of the previous term.
import java.util.*;
public class Tribonacci
{
Scanner sc = new Scanner(System.in);
P a g e | 13
int sum;
int a=0, b=1, c=2;
public void show()
{
System.out.println("Enter a number till which the tribonacci series have to be printed");
int n=sc.nextInt();
System.out.print(a+" ");
System.out.print(b+" ");
System.out.print(c+" ");
int ct=0;
while(ct<=(n-4))
{
sum=a+b+c;
System.out.print(sum+" "); //to print the Tribonacci numbers
a=b;
b=c;
c=sum;
ct++;
}
}
public void main()
{
Tribonacci ob = new Tribonacci();
ob.show();
}
}
OUTPUT
Enter a number till which the tribonacci series have to be printed
20
P a g e | 14
0 1 2 3 6 11 20 37 68 125 230 423 778 1431 2632 4841 8904 16377 30122 55403
ARRAY PROGRAM 1
Write a program to create a class Arrays1 and input the array elements and print them
in matrix form. Then find the right diagonal and left diagonal elements of the array and
print them.
import java.util.*;
public class Arrays1
{
Scanner sc = new Scanner(System.in);
int n,m;
public void show()
{
System.out.println("Enter the size");
m=sc.nextInt();
n=sc.nextInt();
int a[ ][ ] = new int[m][n];
System.out.println("Enter the elements");
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
a[i][j]=sc.nextInt( );
}
}
System.out.println("The matrix form of the array");
for(int i=0; i<m; i++)
{
P a g e | 15
OUTPUT
Enter the size
3
4
Enter the elements
4
2
7
6
9
5
1
7
3
5
0
6
The matrix form of the array
4 2 7 6
9 5 1 7
3 5 0 6
The elements in the right diagonal are:
2, 7, 6, 1, 7, 6,
P a g e | 17
ARRAY PROGRAM 2
Write a program to create a square matrix of size n×n and find the maximum and
minimum of each row and column.
import java.util.*;
public class Arrays2
{
Scanner sc = new Scanner(System.in);
int n;
public void show()
{
System.out.println("Enter the size");
n=sc.nextInt();
int a[][]=new int[n][n];
System.out.println("Enter the elements");
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("The matrix form of the array");
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
P a g e | 18
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
for (int i=0; i<n; i++)
{
int max=a[i][0],min=a[i][0];
for (int j=0; j<n; j++)
{
if (a[i][j]>max)
{
max=a[i][j];
}
if(a[i][j]<min)
{
min=a[i][j];
}
}
System.out.println("The maximum element in "+(i+1)+" row = "+max);
System.out.println("The minimum element in "+(i+1)+" row = "+min);
}
for (int j=0; j<n; j++)
{
int max=a[0][j],min=a[0][j];
for (int i=0; i<n; i++)
{
if(a[i][j]>max)
{
max=a[i][j];
P a g e | 19
}
if (a[i][j]<min)
{
min=a[i][j];
}
}
System.out.println("The maximum element in "+(j+1)+" column = "+max);
System.out.println("The minimum element in "+(j+1)+" column = "+min);
}
}
public void main()
{
Arrays2 ob = new Arrays2();
ob.show();
}
}
OUTPUT
Enter the elements
1
5
3
7
6
0
4
8
2
6
4
P a g e | 20
8
7
9
4
7
1
3
6
5
9
2
5
6
8
The matrix form of the array
15376
04826
48794
71365
92568
The maximum element in 1 row = 7
The minimum element in 1 row = 1
The maximum element in 2 row = 8
The minimum element in 2 row = 0
The maximum element in 3 row = 9
The minimum element in 3 row = 4
The maximum element in 4 row = 7
The minimum element in 4 row = 1
The maximum element in 5 row = 9
The minimum element in 5 row = 2
P a g e | 21
ARRAY PROGRAM 3
Write a program to input an array of size m×n and sort the elements in ascending order
and print the original and rearranged array.
import java.util.*;
public class Arrays3
{
Scanner sc = new Scanner(System.in);
int n,m;
public void show()
{
System.out.println("Enter the size");
m=sc.nextInt();
n=sc.nextInt();
int a[][] = new int[m][n];
System.out.println("Enter the elements");
for (int i=0; i<m; i++)
{
P a g e | 22
pos=i;
for(int j=i+1; j<l; j++)
{
if(b[j]<small)
{
small=b[j];
pos=j;
}
}
temp=b[i];
b[i]=b[pos];
b[pos]=temp;
}
k=0;
for (int i=0; i<m; i++)
{
for (int j=0; j<n; j++)
{
a[i][j]=b[k];
k++;
}
}
System.out.println("Sorted array: ");
for (int i=0; i<m; i++)
{
for (int j=0; j<n; j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
P a g e | 24
}
}
public void main()
{
Arrays3 ob = new Arrays3();
ob.show();
}
}
OUTPUT
Enter the size
3
4
Enter the elements
5
2
9
0
3
7
5
6
8
1
5
7
The matrix form of the array
5 2 9 0
3 7 5 6
8 1 5 7
P a g e | 25
Sorted array:
0 1 2 3
5 5 5 6
7 7 8 9