Computer Programs

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

Programs:

4.Write a program to accept a string and display the string by replacing vowels with ‘*’.
Sample Input: computer Sample Output:c*mp*t*r
import java.util.*;
class vowels
{
public void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
String s=in.nextLine();
int l= s.length();
char c;
for(int i=0;i<l;i++)
{
c=s.charAt(i);
if(c==’a’|| c==’e’||c==’I’||c==’o’||c==’u’||c==’A’||c==’E’||c==’I’||c==’O’||c==’U’)
System.out.print('*');
else
System.out.print(c);
}}}
5.Write a program to accept a string and display each letter.
Sample Input: BLUEJ Sample Output: B
L
U
E
J
import java.util.*;
class letter
{
public void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
String s=in.nextLine();
int l= s.length();
char c;
for(int i=0;i<l;i++)
{
c=s.charAt(i);
System.out.println(c);
}
}
}
6.Write a program to accept a string and check whether it is palindrome word or not.
Sample Input: Malayalam. Sample Out: It is Palindrome.
import java.util.*;
class palindrome
{
public void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
String s=in.nextLine();
int l= s.length();
char c;
String s1=" ";
for(int i=0;i< l ; i++)
{
c=s.charAt(i);
s1 = c + s1;
}
s1=s1.trim(); [here s1 store the new string after removing space]
if(s.equalsIgnoreCase(s1))
System.out.println("It is palindrome");
else
System.out.println("It is not palindrome");

}
}
7.Write a program to accept a word and display the same in Piglatin form:
S.I: TROUBLE S.O: OUBLETRAY

import java.util.*;
class piglatin
{
public void main()
{
int i;
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
String s=in.nextLine();
int l= s.length();
char c;
for( i=0;i<l;i++)
{
c=s.charAt(i);
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
break;
}
String s1=s.substring(i);
String s2 = s.substring(0,i);
System. out. println(s1+s2+"AY");

}
}

NOTE: In this program declare i in the beginning, because the value of i is used in substring
function which is outside the loop. If i is declared inside for loop, the value of i is available
only inside the for loop. That is why it is declared in the beginning.

8. Write a program to accept a string. Count and output the number of double letter sequences
that exist in the string.
Sample Input: “SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE”
Sample Output: 4

import java.util.*;
class doubleletter
{
public void main()
{
int i;
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
String s=in.nextLine();
int l= s.length();
char c,c1;
int count=0;
for( i=0;i<l-1;i++)
{
c=s.charAt(i);
c1=s.charAt(i+1);
if(c == c1)
count++;
}
System.out.println(count);
}
}

Note: See children, If the program ask to convert the input string to capital letters.
After reading the string, convert to capital letters by

String s= in.nextLine( );
s = s.toUpperCase(); This line will convert your input string in to capital letters.
Please note down all these things.
9.Write a program to accept a string and count the consecutive letters present in the string.
S.I.: IT WAS NOT TOUGH FOR HIM TO RESIDE ABOVE THE HILL.
S.O: No. of consecutive letters-6

import java.util.*;
class consecutive
{
public void main()
{
int i;
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
String s=in.nextLine();
int l= s.length();
char c,c1,c2;
int count=0;
for( i=0;i<l-1;i++)
{
c=s.charAt(i);
c1=s.charAt(i+1);
c2=(char)(c+1);
if(c1 == c2)
count++;
}
System.out.println("No.of Consecutive letters:"+count);
}
}

10.Write a program to accept a string and display the last word first followed by the first and
second word.
S.I : Mohandas Karamchand Gandhi
S.O : Gandhi Mohandas Karamchand

import java.util.*;
class Mohandas
{
public void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
String s=in.nextLine();
int l= s.length();
int x= s.lastIndexOf(' ');
String s1 = s.substring(x+1);
String s2 = s.substring(0,x);
System.out.println(s1+" "+s2);

}
}

11. .Write a program to accept a string and display only the first and last word.
S.I : Mohandas Karamchand Gandhi
S.O : Mohandas Gandhi

import java.util.*;
class Mohandas1
{
public void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
String s=in.nextLine();
int l= s.length();
int x= s.indexOf(' ');
int y =s.lastIndexOf(' ');
String s1 = s.substring(0,x);
String s2 = s.substring(y+1);

System.out.println(s1+" "+s2);

}
}

12. . .Write a program to accept a string and display the first letter of each word.
S.I : Subash Chandra Bose
S.O : S.C.B.

import java.util.*;
class SCB
{
public void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
String s=in.nextLine();
int l= s.length();
char c=' ';
s=" "+s;
for(int i=0;i<l;i++)
{
c=s.charAt(i);
if(c==' ')
System.out.print(s.charAt(i+1)+".");
}
}
}

Home Work

13 .Write a program to accept a string and display the first letter of each word except the last
word .
S.I : Subash Chandra Bose
S.O : S.C.Bose

import java.util.*;
class SCBose
{
public void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
String s=in.nextLine();
int l= s.length();
char c=' ';
s=" "+s;
int x= s.lastIndexOf(' ');
String s1=s.substring(x+1);
for(int i=0;i<x;i++)
{
c=s.charAt(i);
if(c==' ')
System.out.print(s.charAt(i+1)+".");
}

System.out.print(s1);
}}

14.Write a program to accept the path given below and extract the filename and pathname.

C:\Pictures\sample\flowers.jpg

Filename : flowers
Pathname : C:\Pictures\sample
import java.util.*;
class path
{
public void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
String s=in.nextLine();
int l= s.length();
int x= s.lastIndexOf("\\" );
int y=s.indexOf('.');
System.out.println("Filename :"+ s.substring(x+1,y));
System.out.println("Pathname :"+s.substring(0,x));
}}

Note: int x= s.lastIndexOf("\\" ); - Here \\ because it is an escape sequence. Inorder to get a


single slash you have to give like this.

15. Write a program in java to enter a sentence.Display the words which are only palindrome.
S.I: MOM AND DAD ARE NOT AT HOME
S.O: MOM
DAD

import java.util.*;
class palin
{
public void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
String s=in.nextLine();
int l= s.length();
char c;
String s1="", s2="";
for(int i=0;i<l;i++)
{
c=s.charAt(i);
if(c!=' ')
{
s1 = s1+c;
s2= c+s2;
}
else
{
if(s1.equalsIgnoreCase(s2))
System.out.println(s1);
s1="";
s2="";
}
}
}
}

Note: When you initialize s1,s2 .Remember initialize like this s1= “”. Don’t initialize s1= “ “.
The above logic works like that. Otherwise you have to make use of trim function.

16.Write a program to accept a sentence. Display the sentence in reversing order of its word.
S.I : Computer is Fun
S.O:Fun is Computer.

import java.util.*;
class reverse
{
public void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
String s=in.nextLine();
s=s+" ";
int l= s.length();
char c;
String s1="", s2="";
for(int i=0;i<l;i++)
{
c=s.charAt(i);
if(c==' ')
{
s2=s1+" "+s2;
s1=" ";
}
else
s1=s1+c;
}
System.out.println(s2);
}
}
17.Write a program to accept a word/string and display new string by removing vowels present
in it.
S.I :COMPUTER APPLICATIONS
S.O: CMPTR PPLCTNS
import java.util.*;
class removevowels
{
public void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
String s=in.nextLine();
int l= s.length();
char c;
for(int i=0;i<l;i++)
{
c=s.charAt(i);
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
continue;
else
System.out.print(c);
}
}
}
18.Write a program to accept a word/string and display the number of consecutive vowels
present in it.
S.I : BEAUTIFUL BEAUTIES
S.O: Pair of vowels – 5

[ EA, AU, EA,AU,IE]

import java.util.*;
class pairofvowels
{
public void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
String s=in.nextLine();
int l= s.length();
char c,c1;
int count=0;
for(int i=0;i<l-1;i++)
{
c=s.charAt(i);
c1=s.charAt(i+1);
if((c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U')&&(c1=='a'||c1=='e'||
c1=='i'||c1=='o'||c1=='u'||c1=='A'||c1=='E'||c1=='I'||c1=='O'||c1=='U'))
{
System.out.print(c);
System.out.print(c1);
count++;
}
}
System.out.print(count);

}
}

19.Write a program to accept a string and convert to uppercase. Display the number of vowels
present in each word.

S.I: MASTERING INFORMATION TECHNOLOGY


S.O: No. of vowels in MASTERING = 3
No of vowels in INFORMATION = 5
No. of vowels in TECHNOLOGY = 3

import java.util.*;
class vowelword
{
public void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
String s=in.nextLine();
s=s+" ";
int l= s.length();
char c;
String s1="", s2="";
int count=0;
for(int i=0;i<l;i++)
{
c=s.charAt(i);
if(c!=' ')
{
if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
count++;
s1=s1+c;
}
else
{
System.out.println("No of vowels in"+s1+"="+count);
s1="";
count=0;
}
}
}
}

20. Write a program to accept a sentence and display the longest word and its length.

S.I :TATA FOOTBALL ACADEMY WILL PLAY AGAINST MOHAN BEGAN


S.O :The longest word is : FOOTBALL
The length of the word is : 8

import java.util.*;
class Longestword
{
public void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
String s=in.nextLine();
s=s+" ";
int l= s.length();
char c;
int count=0,max=0;
String s1="", s2="";
for(int i=0;i<l;i++)
{
c=s.charAt(i);
if(c!=' ')
{s1=s1+c;count++;}
else
{
if(count>max)
{ max=count; s2=s1; }

count=0;
s1=" ";
}}

System.out.println("The Longest word is"+s2);


System.out.println("The length of the word is"+max);
}
}
21.Write a program to accept a string as a sentence. Display the new string after reversing the
characters of each word.
S.I : New Delhi is the capital of India.
S.O: weN ihleD si eht latipac fo aidnI

import java.util.*;
class reverseword
{
public void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
String s=in.nextLine();
s=s+" ";
int l= s.length();
char c;
String s1="";
for(int i=0;i<l;i++)
{
c=s.charAt(i);
if(c!=' ')
s1=c+s1;
else
{
System.out.print(s1+" ");
s1=" ";
}
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy