Using a Pointer as the String

I have a question that I haven't got yet. I want to pass a pointer in a void function. And count the words in the pointer. Well I got it to echo back what I typed in so I know that works. But what I'm not for sure is when I use this for statement to go through that pointer I don't know what condition expression(inside the parathesis) to use in it. I know that I can't use .length() because it would work right. And I tried to say not a newline character and doesn't work. I just would like some hints please no answer. Since I just need to know how to setup the condition and that will work for using string pointers.
To see what Im talking about I enclosed the input function and counting word function.

void input(string* pointer)
{
getline(cin, *pointer);

cout << endl;
// cout << *pointer;
}

void counting words(string* pointer)
{
int words = 0;
for(int index = 0; index != '\n'; index++)
{

}
}
Last edited on
It's either (*pointer).length() or pointer->length().

An alternative to passing a ponter to a string is to use a reference. It may be more suitable in the example you've shown.
1.) cout and getline don't support the string type
2.) Why can't you use string::length()? It returns the number of characters in the string (including \n, excluding \0)
3.) "counting words" should be "counting_words" - you can't have spaces in any sort of variable/class/function name
4.) Why make them void? For counting_words you'd want to return the number of words that you find in the string...
5.) Inside the braces you should put something that does the following in order:
--a.) Iterate over any whitespace
--b.) Add 1 to words
--c.) Iterate over any non-whitespace
Last edited on
1.) cout and getline don't support the string type

Actually, they do.
Last edited on
And besides, std::getline() doesn't give you the '\n' character anyway so that for loop would loop forever.
I got the for loop to work this is the setup I used. Since you cant say .length() with a pointer I looked around and saw people use sizeof(pointer). So I did and it worked.

for(int index = 0; index < sizeof(pointer); index++)

well now I am having problems with say if it is a space or even is alpha. This is how I typed it but pointers evedently has problems with those bool functions it gives pointer error messages. Here is what I typed.

if(isalpha(pointer[index])) or this one if(isspace(pointer[index]))

I never had any problems with this project using arrays or vectors.
But declaring a pointer to point at my string I am having a rough time with. Curiousity is this project a good one to do with pointers or should I stick with array and vector.
I'll just give you some code.

1
2
3
4
5
6
7
8
9
10
int word_count(string* str){
	int a=0,words=0,size=str->length();
	for(;a<size;++a){
		for(;a<size && str->at(a)==' ';++a){}
		//safety against trailing whitespace
		if(a==size){
			break;}
		++words;
		for(;a<size && str->at(a)!=' ';++a){}}
	return words;}


It takes a string and returns the number of words. The way it does this is as follows:

1.) initialize variables
2.) enter the main loop, terminating at the end of the string
3.) iterate over the whitespace
4.) check if the whitespace was at the end of the string (and break if it was)
5.) add 1 to words
6.) iterate over anything that isn't whitespace
7.) repeat steps 3-7 until the end of the string is reached
8.) return the number of words

You can use functions of pointers. As you can see in this code, all you have to do is use the dereferencing operator ( -> )
Last edited on
Thank You for the help
You're welcome
Topic archived. No new replies allowed.