COMPLETE IN C++
Declare and initialize a global constant named SIZE with the value 50.
Write a void function called count that accepts two parameters-- a C-string called str representing a C-string passed to the function and an array of integers called alphabets to store the count of the letters of the C-string.
Note that the alphabets array stores 26 counters – one for each letter of the alphabet. Use the same counter for lowercase and uppercase characters. The first element is the counter for ‘A’ or ‘a’, the second element is the counter for ‘B’ or ‘b’ and so on.
Inside your function,
o Using a loop of your choice, process one character at a time. o
Test whether the character is an alphabet.
▪ If it is an alphabet, convert the alphabet to uppercase and find its ascii value.
▪ Increment the appropriate counter of the alphabets array.
• Subtract 65 from the ascii value of the character to obtain the index
of the array to increment the correct counter. • Inside your main function:
o Declare a C-string named str and use SIZE as the size of the
C-string.
o Declare an array of integers called alphabets of size 26 and
initialize all
values with 0.
o Using a suitable message, get the C-string from the user. The user may enter multiple words.
o Call function count and pass the appropriate arguments.
o Inside a loop of your choice, process each element of the
alphabets array, one at
time.
▪ If the count is non-zero, display the corresponding uppercase characters as well as the counts.
• Due to time constraints, no comments are required in this code.
For this program we have used a string function strlen to get the length of the string passed. Ascii value is obtained by using int() function. For characters which are in capital letters 65 is subtracted from their ascii value to get the corresponding position in alphabets array. For small letters, first it is coverted to capital letters by subtracting 32 from their ascii value and then 65 is subtracted to get corresponding position in the alphabets array. After getting corresponding position the value in the array is incremented by 1.
Program code and output screen is given for your reference.
#include <iostream>
#include<string.h>
using namespace std;
const int SIZE=50;
void count(char str[50],int alphabets[26])
{
int n,p,i;
n=strlen(str);
for (i=0;i<n;i++)
{
if(int(str[i]>=65 and int(str[i])<=90))
{
p=int(str[i])-65;
alphabets[p]=alphabets[p]+1;
}
else if (int(str[i]>=97 and int(str[i])<=122))
{
p=int(str[i])-32;
p=p-65;
alphabets[p]=alphabets[p]+1;
}
}
for(i=0;i<26;i++)
{
if (alphabets[i]!=0)
{
cout<<char(i+65)<<"
"<<alphabets[i]<<endl;
}
}
}
int main()
{
char str[SIZE];
int alphabets[26],i;
for (i=0;i<26;i++)
{
alphabets[i]=0;
}
cout<<"Enter a string : ";
cin.getline(str,50);
cout<<endl;
count(str,alphabets);
return 0;
}
PROGRAM CODE
#include <iostream>
#include<string.h>
using namespace std;
const int SIZE=50;
void count(char str[50],int alphabets[26])
{
int n,p,i;
n=strlen(str);
for (i=0;i<n;i++)
{
if(int(str[i]>=65 and int(str[i])<=90))
{
p=int(str[i])-65;
alphabets[p]=alphabets[p]+1;
}
else if (int(str[i]>=97 and int(str[i])<=122))
{
p=int(str[i])-32;
p=p-65;
alphabets[p]=alphabets[p]+1;
}
}
for(i=0;i<26;i++)
{
if (alphabets[i]!=0)
{
cout<<char(i+65)<<" "<<alphabets[i]<<endl;
}
}
}
int main()
{
char str[SIZE];
int alphabets[26],i;
for (i=0;i<26;i++)
{
alphabets[i]=0;
}
cout<<"Enter a string : ";
cin.getline(str,50);
cout<<endl;
count(str,alphabets);
return 0;
}
OUTPUT SCREEN
Get Answers For Free
Most questions answered within 1 hours.