Language is C++. any help is appreciated, thank you! Write a program called Coun
ID: 3835181 • Letter: L
Question
Language is C++. any help is appreciated, thank you!
Write a program called CountElements that does the following:
· Declare a char array called sentence of 40 elements and assign the following: (I have noted the spacing)
How(sp)is(sp)Course(sp)1,(sp)C++,(sp)going(sp)4(sp)u?
· Declare counters that will count the number of elements that are:
alphabetic, digits, upper case, lower case, punctuation and spaces
(ex: numalph=0, numdig=0, etc)
· Loop through each character in the array and increase the proper counter.
Write the lines to determine each of the outputs.
Output: alphabet letters:
digits:
etc.
(ex: while (sentence [count] != ‘’))
Run the program. Print the code and a screenshot.
Sample Output:
alphabet letters: 18
digits: 2
upper case: 3
lower case: 15
spaces: 7
punctuation: 5
Press any key to continue . . .
Explanation / Answer
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main()
{
char str [40];
int numalph=0, numdigit=0, numup=0,numlo=0,numpun=0,numspa=0,numother=0; /* char counts */
int i=0;
cout<<"Enter a line of text(Max 40 characters): ";
cin.get(str,40);
/* Traverse the string and increment appropriate counter */
while (str[i] != '')
{
char ch= str[i];
if (ch>= 'A' && ch<= 'Z' || ch>= 'a' && ch<= 'z')
{
numalph++;
if (ch>= 'A' && ch<= 'Z')
numup++;
else
numlo++;
}
else if (ch>= '0' && ch<= '9')
numdigit++;
else if (ch == ' ' || ch ==' ' || ch == ' ')
numspa++;
else if (ch == '-' || ch == '+' || ch == ',' || ch == ';' || ch == ':' ||ch == '.' || ch == '?' || ch == '!' || ch == '(' || ch == ')' || ch == '*' || ch == '&')
numpun++;
else numother++;
i++;
}
/* Print the counters */
cout<<" Aplhabets: "<<numalph<<endl;
cout<<"Digits: "<<numdigit<<endl;
cout<<"Uppercase Alphabets: "<<numup<<endl;
cout<<"Lowercase Alphabets : "<<numlo<<endl;
cout<<"Spaces: "<<numspa<<endl;
cout<<"Punctuation: "<<numpun<<endl;
return 0;
}
output screenshot:
http://imgur.com/HD1Wsss
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.