We will be counting the number of each character in the user input paragraph through below program :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <iostream.h> #include <conio.h> #include <string.h> void main(){ static int array[256]; int i; char string[1000]; clrscr(); cout << "Enter the paragraph"<<endl; cin.getline(string,1000); // get the user input cout <<endl; // loop to count and store each value for(i=0;i<strlen(string);i++){ array[(int)string[i]]+=1; } //loop through array to display the counts for(i=1;i<256;i++){ if(array[i]>0){ if(i==32){ cout << "space : " << array[i] <<endl; } else { cout << (char) i << " : " << array[i] << endl; } } } getch(); } |
Compiled in TurboC