write a program to count no. of vowel and consonant in a string.

import java.util.*;
class Count
{
public static void main(String args[])
 {
 Scanner a=new Scanner(System.in);
System.out.println("enetr the string");
String str=a.nextLine();
int count=0;
int num=0;
for (int i = 0; i < str.length(); i++)
 {
  char c = str.charAt(i);
  if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u')
  {
  count++;
  }
   else
   {
    num++;
}
  }
  System.out.println("There are" + " " + count + " " + "vowels and"+num+"  consonant");
}
}

6 comments:

  1. Not sure when this was posted, but I just started my Java classes and this code really helped me to develop my program. Thanks

    ReplyDelete
  2. if i give any special character like * ,\,/,.,,, like will u count this as as consonants?

    ReplyDelete
  3. import java.util.*;

    class matrix
    {
    int i,j,rows,cols;
    int [][] a1;
    int [][] a2;
    int [][] a3;
    String str="";
    matrix(int rows,int cols)
    {
    this.rows=rows;
    this.cols=cols;
    a1=new int[rows][cols];
    a2=new int[rows][cols];
    a3=new int[rows][cols];
    }
    void setMat(int [][] a1,int [][] a2)
    {
    for(i=0;i<rows;i++)
    {
    for(j=0;j<cols;j++)
    {
    this.a1[i][j]=a1[i][j];
    this.a2[i][j]=a2[i][j];
    }
    }
    }
    void addition()
    {
    for(i=0;i<rows;i++)
    {
    for(j=0;j<cols;j++)
    {
    a3[i][j]=this.a1[i][j]+this.a2[i][j];

    }

    }
    toString(a3);
    System.out.print(str);
    }
    public String toString(int [][] a3)
    {
    for(i=0;i<rows;i++)
    {
    for(j=0;j<cols;j++)
    {
    str+=Integer.toString(a3[i][j])+" ";
    }
    str+="\n";
    }
    return str;
    }
    }
    class add
    {
    public static void main(String [] args)
    {
    int i,j,rows,cols;
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter Row And Column Of Both Matrix:");
    rows=sc.nextInt();
    cols=sc.nextInt();
    matrix m1=new matrix(rows,cols);
    System.out.print("Enter 2 matrix:");
    int [][] a1=new int [rows][cols];
    int [][] a2=new int [rows][cols];
    for(i=0;i<rows;i++)
    {
    for(j=0;j<cols;j++)
    {
    a1[i][j]=sc.nextInt();
    }
    }
    for(i=0;i<rows;i++)
    {
    for(j=0;j<cols;j++)
    {
    a2[i][j]=sc.nextInt();
    }
    }
    m1.setMat(a1,a2);
    m1.addition();


    }
    }

    ReplyDelete
  4. public class CountVowelConstant {
    public static void countVowCons(String s){
    char c; int countVow = 0; int countCon = 0;
    String s1=s.toLowerCase();
    if(s1.length()==0 || s1==null){
    System.out.println("enter valid string");
    }
    for(int i=0; i='a' && c<='z'){
    if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u'){
    countVow++;
    }else{
    countCon++;
    }
    }

    }
    System.out.println("no. of vowels = "+ countVow + " and constant = "+countCon);
    }
    public static void main(String[] args) {
    countVowCons("12sonu");
    }

    }

    ReplyDelete
  5. Very informative article.Thank you author for posting this kind of article .

    http://www.wikitechy.com/view-article/vowel-checking-program-in-c-with-example-and-explanation


    Both are really good,
    Cheers,
    Venkat

    ReplyDelete
  6. one line may be added in between as follows to trap blank space.....

    else if(c==' ')
    {

    }

    ReplyDelete