Sign in to follow this  

Basic string class problem

This topic is 4735 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[i].letter << " " << alphabet[i].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

Share this post


Link to post
Share on other sites
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 )[i];

[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 )[i];' becomes 'char temp = s[i];' )

Regards,
jflanglois

Share this post


Link to post
Share on other sites
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[i], 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[i]] = count(s.begin(), s.end(), alphabet[i]);

// 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4735 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this