Sunday, 25 August 2013

Write a Java program to show number of word and vowel in a string.

test.java
----------------------
import java.util.Scanner;
class test
{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
String str;
int w=1,v=0,i,len;
char ch;
System.out.print("Enter The String:");
str=in.nextLine();
len=str.length();

for(i=0; i<len; i++){
ch=str.charAt(i);
if(ch==' '){
w=w+1;
}
else if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U'){
v=v+1;
}
}
System.out.println("Number of Word is "+w+"\nand number of vowel is "+v);
}
}

Output:
---------------------
Enter The String:how are you?
Number of Word is 3
and number of vowel is 5

No comments:

Post a Comment