Professional Documents
Culture Documents
Question 1
Write a program to enter a string and print the number of occurrences of each
vowel in the string.
import java.util.*;
public class Q1
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String s;
System.out.println("Enter the string");
s=sc.nextLine();
int a=0,e=0,i=0,o=0,u=0;
char ch;
int l=s.length();
s=s.toUpperCase();
int j;
for(j=0;j<l;j++)
{
ch=s.charAt(j);
if(ch=='A')
a++;
if(ch=='E')
e++;
if(ch=='I')
i++;
if(ch=='O')
o++;
if(ch=='U')
u++;
}
System.out.println("The number of a's="+a);
System.out.println("The number of e's="+e);
System.out.println("The number of i's="+i);
System.out.println("The number of o's="+o);
System.out.println("The number of u's="+u);
}
}
Sample output
Enter the string
apple a day keeps the doctor away
The number of a's=5
The number of e's=4
The number of i's=0
The number of o's=2
The number of u's=0
HW
Question 2
Write a program to find the total number of vowels in a string.
public class VowelCount
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String s;
System.out.println("Enter the string");
s=sc.nextLine();
int v=0;
char ch;
int l=s.length();
s=s.toUpperCase();
int j;
for(j=0;j<l;j++)
{
ch=s.charAt(j);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
v++;
}
System.out.println("The no of vowels="+v);
}
}
Question 3
Write a program to input five names in an array and display the names begin
with a vowel?
import java.util.*;
public class strin1
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String a[]=new String[5];
int j;
char c;
for(j=0;j<5;j++)
{
System.out.println("Enter String"+(j+1)+": ");
a[j]=sc.nextLine();
}
for(j=0;j<5;j++)
{
c=a[j].charAt(0);
if (c=='A'||c=='a'||c=='E'||c=='e'||c=='O'||c=='o'||c=='I'||
c=='i'||c=='U'||c=='u')
System.out.println("The first char in String"+(j+1)+" is vowel:"+a[j]);
}
}
}
Question 4
Write a program to enter a string and find the total number of characters in a string and also
reverse the string?
import java.util.*;
public class Reverse
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String s;
System.out.println("Enter the string");
s=sc.nextLine();
int l=s.length();
int i;
char c;
String rev=" ";
for(i=l-1;i>=0;i--)
{
c=s.charAt(i);
rev=rev+c;
}
System.out.println("No of characters="+l);
System.out.println("The reversed string="+rev);
}
}
HW
Question 5
Write a program to check whether a string is palindrome or not.
import java.util.*;
public class striPali
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String s;
System.out.println("Enter the string");
s=sc.nextLine();
int l=s.length();
int i;
char c;
String rev=" ";
for(i=l-1;i>=0;i--)
{
c=s.charAt(i);
rev=rev+c;
}
if(rev.compareTo(s)==0)
System.out.println("The given string is a Palindrome");
else
System.out.println("The given string is not a Palindrome");
}
}
Chinni. R. Pillai
Computer Department
Mob:9620184111