Need a project done?

C++ Programming Developer

Search This Blog

Search through the elements of character type array and tell the number of each words which user enters.

/*
Program to search through elements of character type array and tells the number of Capital words.
*/

#include <iostream>
using namespace std;

int main()
{
cout << "Enter a Sentence: ";
char sentence[100];
cin.getline(sentence,100,'\n');

int tAlphabets[26] = {0};
char alphabets = 65;
int j = 0;
while (alphabets<=91)//till z.
{
for (int i = 0; i<100; i++)
{
if ( char(alphabets) == sentence[i] )
++tAlphabets[j];
}
j++;

alphabets++;
}
cout <<"CAPITAL:\n";
for (int i=0; i<26; i++)
{
if (tAlphabets[i] == 0)
continue;
cout << char(i+65) << ": " <<tAlphabets[i]<<endl;
}
}
//==============================================================
If you want your code to work for both Capital and Small letters then add a simple condition and your program will look like

/*
Program to search through elements of character type array and tells the number of Capital words.
*/

#include <iostream>
using namespace std;

int main()
{
cout << "Enter a Sentence: ";
char sentence[100];
cin.getline(sentence,100,'\n');

int tAlphabets[26] = {0};
char alphabets = 65;
int j = 0;
while (alphabets<=91)//till z.
{
for (int i = 0; i<100; i++)
{
if ( char(alphabets) == sentence[i] || char(alphabets+32) == sentence[i] )
++tAlphabets[j];
}
j++;

alphabets++;
}
for (int i=0; i<26; i++)
{
if (tAlphabets[i] == 0)
continue;
cout << char(i+65) << ": " <<tAlphabets[i]<<endl;
}
}

No comments:

Post a Comment

"Don't let anyone ever make you feel like you don't deserve what you want."