Basic string class problem

Started by
3 comments, last by HughG 19 years, 3 months ago
I've decided to take a step back from game programming and brush up on some of my C++ basics. I picked up a book by Bjarne Stroustrup and am trying to do some of the exercises. Alright, so I'm trying to write a program that accepts a string (exercise calls for string and not a c style array of chars) and outputs the number of letters contained in the string. It does this by printing out the entire alphabet and the number of times a letter in the alphabet occured. Example: Input string: "abbbdsadjk" Output: "a: 2, b: 3, c: 0, d: 2, e: 0 ..... (entire alphabet) So this is what I came up with:

#include <string>
#include <iostream>
using namespace std;

void count(string*, struct Alphabet[]);

struct Alphabet{
	char letter;
	int number;
};


int main(){

	string blah = "xxadfjkljsduio";
	Alphabet alphabet[] = {{'a'}, {'b'}, {'c'}, {'d'}, {'e'}, {'f'}, {'g'}, {'h'}, {'i'}, {'j'}, 
		{'k'}, {'l'}, {'m'}, {'n'}, {'o'}, {'p'}, {'q'}, {'r'}, {'s'}, {'t'}, {'u'}, {'v'}, {'w'}, 
		{'x'}, {'y'}, {'z'}};
	
	count(&blah, alphabet);
	
	for(int i = 0; i <27; i++)		
		cout << alphabet.letter << " " << alphabet.number << "\n";
		
}

void count(string *s, Alphabet a[]){
	
	for(int i = 0; i < s->length(); i++){
		char temp = *s; //compiler doesn't like this line
		for(int j = 0; j < 26; j++){
			if(a[j].letter == temp)
				a[j].number++;
		}
        s++;
	}
	

}
Now I can't figure out how to compare the individual letters of the string to the individual characters inside the Alphabet struct. I looked up some methods in the string library and came across one called c.str() but I could not figure out how to make it work in this instance. Any ideas? Also, how could I modify the program to pass a pointer to an array of alphabet structs instead of passing the entire array of structures? TIA
Advertisement
std::string temp("abc");
char c = temp[1]; // c = 'b'
char temp = *s; //compiler doesn't like this line

you are trying to assign a string to a char here. You probably wanted to do
char temp = ( *s );

[edit] hmph, beaten
Also, I would suggest you use a reference instead of a pointer to pass the string into a function, like so: 'string *s' becomes 'const string &s', that way, you don't need to dereference the pointer above ( 'char temp = ( *s );' becomes 'char temp = s;' )

Regards,
jflanglois
Since you're brushing up on your fundamental C++...

1. Use std::map.
#include <map>std::map<char, int> Frequency;...// for each character in string  // if character is key in map, increment value  // else insert new key-value pair, with value as 1
As it stands, this does not represent zero-frequency items.

Using maps, vectors, strings and so forth also eliminates hard-coded numerical limits and other magic values in your code. Here's a complete example:
#include <algorithm>#include <iostream>#include <locale>#include <map>#include <string>int main(){  using namespace std;  char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";  map<char, size_t> Frequency;  for(int i = 0; i < strlen(alphabet); ++i)    Frequency.insert(pair&lt;char, size_t>(alphabet, 0));  string s;  cout << "Please enter a string of text: ";  getline(cin, s);  // first, convert the text to all uppercase for case-insensitive tally  transform(s.begin(), s.end(), s.begin(), toupper);  for(int i = 0; i < strlen(alphabet); ++i)    Frequency[alphabet] = count(s.begin(), s.end(), alphabet);  // print out the results  map<char, size_t>::iterator iter, stop = Frequency.end();  for(iter = Frequency.begin(); iter != stop; ++iter)    cout << iter->first << ": " << iter->second << endl;  return 0;}
Obviously, there are inefficiencies in this code, but look how much more compact it is, and how much faster it is to write once you're familiar with all the constructs.
jflanglois and ftn: Thanks, that is exactly what I was trying to do. I guess I assumed that the string pointer would point at the first letter in the string (like a char pointer to a char array).

Oluseyi: Beautiful! I really appreciate the advice. I have no experience with the STL at all and Stroustrup actually mentions the map class early in the book but fails to show any examples with it.

This topic is closed to new replies.

Advertisement