Question

C++ Concepts: Single-Dimension Array Write a program, using 25 or fewer lines of code, to count...

C++ Concepts: Single-Dimension Array

Write a program, using 25 or fewer lines of code, to count each occurrence of each lower-case letter in a string.

Start your program by declaring a character array, msg, with a size of 30 or greater.

char msg[MAX];

The user should then type a text string into the array:

cin.getline(msg, MAX);

Your program should now report the number of each character (a-z) found in the array:

a face at the beach

a - 4
b - 1
c - 2
d - 0
e - 3
f - 1
g - 0
h - 2
i - 0
j - 0
k - 0
l - 0
m - 0
n - 0
o - 0
p - 0
q - 0
r - 0
s - 0
t - 2
u - 0
v - 0
w - 0
x - 0
y - 0
z - 0

Here is one way of creating that output using characters in a loop:

for (i=0; i<26; i++)

cout << static_cast(i + 'a') << " - "
<< etc..

0 0
Add a comment Improve this question Transcribed image text
Answer #1

For counting the alphabets , we need to count the particular  alphabet by traversing the whole string . To do this, we need to use two loops - one for traversing the alphabets and the other for traversing the whole string .

CODE :

#include <iostream>

#define MAX 30 // defining the variable MAX as 30
using namespace std;

int main() {
  
   char msg[MAX] ; // declaring the char array with size 30
   cin.getline(msg,MAX) ; // taking input from the user
  
   for(int i =0;i<26;i++) // for traversing the alphabets from 'a ' to 'z' .
   {
   int count= 0; // for counting the no.of occurrences of the particular alphabet.
  

for(int j=0;j<MAX;j++) // for traversing the whole string
   {
  

if(static_cast<char>(i+'a')==msg[ j]) // checking the equality and incrementing the value of count
count++ ;

}
   cout << static_cast<char>(i + 'a') << " - " <<count<<endl;
   }

return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ Concepts: Single-Dimension Array Write a program, using 25 or fewer lines of code, to count...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • // Write a program that determines how many of each type of vowel are in an...

    // Write a program that determines how many of each type of vowel are in an entered string of 50 characters or less. // The program should prompt the user for a string. // The program should then sequence through the string character by character (till it gets to the NULL character) and count how many of each type of vowel are in the string. // Vowels: a, e, i, o, u. // Output the entered string, how many of...

  • Write a C program that takes two sets of characters entered by the user and merge...

    Write a C program that takes two sets of characters entered by the user and merge them character by character. Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl Output: defend the castle Your program should include the following function: void merge(char *s3, char *s1, char *s2); The function expects s3 to point to a string containing a string that combines s1 and s2 letter by letter. The first set might be longer...

  • In C++ write a program that will read three strings from the user, a phrase, a...

    In C++ write a program that will read three strings from the user, a phrase, a letter to replace, and a replacement letter. The program will change the phrase by replacing each occurrence of a letter with a replacement letter. For example, if the phrase is Mississippi and the letter to replace is 's' the new phrase will be "miizzizzippi". in the function, replace the first parameter is a modifiable string, the second parameter is the occurrence of a letter...

  • 6.15 Program 6: Using Arrays to Count Letters in Text 1. Introduction In this program, you...

    6.15 Program 6: Using Arrays to Count Letters in Text 1. Introduction In this program, you will practice working with arrays. Your program will read lines of text from the keyboard and use an array to track the number of times each letter occurs in the input text. You will use the contents of this array to generate a histogram (bar graph) indicating the relative frequencies of each letter entered. Test cases are available in this document. Remember, in addition...

  • I need eclipse code for : Write a program that analyzes text written in the console...

    I need eclipse code for : Write a program that analyzes text written in the console by counting the number of times each of the 26 letters in the alphabet occurs. Uppercase and lowercase letters should be counted together (for example, both ‘A’ and ‘a’ should count as an A). Any characters that are not letters should be ignored. You must prompt the user to enter the text to be analyzed. Then, for any letter that appeared at least once...

  • This is for C++ Write a program that reads in a sequence of characters entered by...

    This is for C++ Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along with the number of times it occured. All non-alphabetic characters must...

  • /// c ++ question plz help me fix this not a new code and explain to...

    /// c ++ question plz help me fix this not a new code and explain to me plz /// Write a function to verify the format of an email address: bool VeryifyEmail(char email[ ]); Do NOT parse the email array once character at a time. Use cstring functions to do most of the work. Take a look at the available cstring and string class functions on cplusplus website. Use a two dimensional array to store the acceptable top domain names:...

  • Take the following C++ code and add to it the following: Write a program that reads...

    Take the following C++ code and add to it the following: Write a program that reads a string and outputs the number of times each lowercase vowel appears in it. Your program must contain a function with one of its parameters as a char variable, and if the character is a vowel, it increments that vowel's count. #include<iostream> #include<string> using namespace std; int countVowel(char, char); int main(void) { string str1; int countA = 0; int countE = 0; int countI...

  • I need my c++ code converted to MASM (assembly language). The instructions below: write an assembly...

    I need my c++ code converted to MASM (assembly language). The instructions below: write an assembly program that does the following; 1. count and display the number of words in the user input string. 2. Flip the case of each character from upper to lower or lower to upper. For example if the user types in:   "Hello thEre. How aRe yOu?" Your output should be: The number of words in the input string is: 5 The output string is : hELLO...

  • IN C language Write a C program that prompts the user to enter a line of...

    IN C language Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr andcreadLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT