Write a c++ program that Given a character array A. Write a C++ program that com
ID: 3599142 • Letter: W
Question
Write a c++ program thatGiven a character array A. Write a C++ program that computes and outputs the number of letters in A and frequency (number of occurrence) of the letters in the array Sample Format A character string "My string is not very very long Sample Output Number of letters: 31 Frequency: a: o b: o c: o d: o e: 2 f: o 8: 2 : O i: 2 : 0 k: o n: 3 0: 2 p: o q: O r: 3 S: 2 t: 2 u: o V: 2 w: o x: O y: 3 z: 0 Instructions Write and run a C++ program to solve the problem using the sample data given. Provide on th back of this sheet: (1) the C++ program, and (2) the output obtained. Demonstration of th
Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
getline(cin,s); //takes input including white spaces
int temp[26]={0}; //an array to store all alphabets frequency
for(int i=0;i<s.length();i++)
{
if((s[i]>0&&s[i]<65)||(s[i]>90&&s[i]<97)||(s[i]>122)) //if any character is encountered other than alphapets
continue;
if(s[i]>65&&s[i]<91) //if an uppercase letter is encountered it is converted to lower case
s[i]=s[i]+32;
temp[s[i]-97]++; //the ascii value of each alphabet is subtracted by 97 in order to obtain index of temp array whose value within that particular index is incremented each time the character is encountered
}
cout<<"Number of letters:"<<s.length()<<endl;
cout<<"Frequency"<<endl;
for(int i=0;i<26;i++)
cout<<(char)(i+97)<<":"<<temp[i]<<endl; //explicit typecasting is done in order to display characters.
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.