Showing posts with label Logical Question Exercise. Show all posts
Showing posts with label Logical Question Exercise. Show all posts

Wednesday, 28 January 2015

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

Structure of the Problem Requirements 

This Program count 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++