Join 136,172 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,920 people online right now. Registration is fast and FREE... Join Now!
Hey, guys. I'm trying to write a program that takes a phrase input by the user, counts the number of times each letter shows up in the phrase, then displays this number to the screen beside the corresponding letter. If a letter does not show up in the phrase, then it will have no count on the screen (No zero's for letters not input, in other words.). This is what I have so far.
CODE
#include <iostream> #include <cmath> #include <iomanip> using namespace std;
void sort(char x[], int n); const int alphabet = 26;
int main(){ char limit = '.', sentence[255]; int length, letters, counters[alphabet] = {0}; cout << "Please type in a sentence composed of all lowercase letters, \n"; cout << "and include a period at the end: "; char next; int index = 0; cin.get(next); for (int i = 0; next != limit; i++) { cin.get(next); letters++; sentence[i] = next; }
void sort(char x[], int n) { for (int pass = 1; pass <= n; pass++) { for (int i = 0; i < n-pass; i++) if (x[i] > x[i+1]) { int temp = x[i]; x[i] = x[i+1]; x[i+1] = temp; }}}
Unfortunately, while it seems to be counting the letters (And they show up in the left column where they should, were the corresponding letters in alphabetical orders on the right), I can't get the program to assign a letter to only one array position if the letter in question is repeated in the user's phrase. I apologize if I'm wording this oddly, but if anyone could point me in the right direction, I'd be very thankful.
I don't quite understand what the sorting is about. In that last alphabet display, outputting sentence makes little sense. I think maybe you were aiming for this:
I don't quite understand what the sorting is about. In that last alphabet display, outputting sentence makes little sense. I think maybe you were aiming for this:
I don't quite understand what the sorting is about. In that last alphabet display, outputting sentence makes little sense. I think maybe you were aiming for this:
PS. you win the most funky block style I've seen in a very long time. }}}}}
That did it. Thanks a bunch for the help, I appreciate it.
(Yeah, that's the blocking I like to call "Here's a bracket I forgot so you can let me compile you stupid program.")
Agh, perhaps I spoke too soon. I'd forgotten that I need the output to display the first most frequent letter first, followed by the second, and so on. The sort function I have in there doesn't seem to be doing this for me in the current setup, however. Any suggestions on what needs to be changed? (I've also found a strange anomaly that I can't quite figure out. If the user inputs only three letters, then every time it will return the accurate letter count for the three entered, and then an 'x' counted once! It doesn't do this if the user inputs more than or less than three.)
This is a bit of a challenge. You essentially need to sort a list of items with two values, (letter, count). You can create an array of struct, load it up with your hits, and then sort that.
Another attack, unorthodox but fairly straight forward. You can find the position of the maximum ( or minimum) value, print that, zero out that position, and repeat. You keep going until you get back a count of zero and you're done.
This is a bit of a challenge. You essentially need to sort a list of items with two values, (letter, count). You can create an array of struct, load it up with your hits, and then sort that.
Another attack, unorthodox but fairly straight forward. You can find the position of the maximum ( or minimum) value, print that, zero out that position, and repeat. You keep going until you get back a count of zero and you're done.
Good luck.
Could I do something similar to the following to the sort code I have, in that case? (Unfortunately, we aren't yet allowed to use structs in the class.)
CODE
void sort(int a[], char x[], int number){ int temp, i, j; char temp2; for ( i = 0; i <= number; i++ ) { for ( j = i; j <= number; j++ ) { if ( a[j] < a[i] ) { temp = a[i]; a[i] = a[j]; a[j] = temp; temp2 = x[i]; x[i] = x[j]; x[j] = temp2; } } } }
This post has been edited by Jerichozero: 14 Dec, 2007 - 09:05 AM