
November 22nd, 2004, 12:11 AM
|
|
Registered User
|
|
Join Date: Nov 2004
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Sorting a string of characters
Here's what i need to do:
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 be ignored. Any characters entered after the period ('.') should be left in the input stream unprocessed. No limit may be placed on the length of the input. Use an array with a struct type as its base type so that each array element can hold both a letter and an integer. The table should not be case sensitive -- for example, lower case 'a' and upper case 'A' should be counted as the same letter. Here is a sample run:
PHP Code:
Enter a sequence of characters (end with '.'): do be Do bo. xyz Letter: Number of Occurrences o 3 d 2 b 2 e 1
</FONT>
Here is what i have:
PHP Code:
#include <iostream> using namespace std;const int ARRAY_SIZE = 1000;void readChars(string list[], int &numItems); void sort(string list[], int numItems); void printChars(const string list[], int &numItems);int main() { string list[ARRAY_SIZE]; int numItems; readChars(list, numItems); // sort(list, numItems); printChars(list, numItems, sort(list[ARRAY_SIZE], numItems);, int numItems); } void readChars(string list[], int &numItems) { string Char; char count = 0; cout << "Enter a sequence of characters (end with '.'): "; cin >> Char; int subCounter = 0; list[subCounter] = Char.substr(subCounter, 1); char period; while (list[subCounter] != ".") { list[subCounter] = Char.substr(subCounter, 1); subCounter++; // cout << subCounter; // cout << list[2]; } numItems = subCounter; cout << numItems; }void sort(string list[], int numItems) { /* for (int count = 0; count < numItems - 1; count++){ swap(list[indexOfSmallest(list, count, numItems)], list[count]); } */ }void printChars(const string list[], int& numItems) { for (int count = 0; count < numItems; count++){ cout << list[count] << " "; }}
|