Use ASCII Values to Count Number of Character in C++

This Program counts number of characters in a string using ASCII values in C++ program. After getting string from user the program will display the table in which told how many times a characters occur . Note ASCII values has different code for upper and lower case and this program also manage it.

Source Code

#include <iostream>
using namespace std;
int main(int argc, char** argv) {
 string sentence;
 int count=0;
 int a;
 char c1;
 cout<< "Enter a sentence with \".\" at End"<<endl;
 getline(cin,sentence,'.') ;
 for(int i=65;i<123;i++){
  if(((i>64)&&(i<91))||((i>96)&&(i<123))){
   if(i==97){
    cout<<endl;  
   }
   cout<< (char)i<< " = ";
   for(int j=0;j<sentence.length();j++){
    a=sentence[j];
    if(a==i){
     count=count+1;
    }
   }
   cout<<count<< " | ";
  }
  count=0;  
 } 
 return 0;
}

Output of the Program

Use ASCII values to count number of character in C++

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment