Rate my Program

This program counts the amount of words in a string:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

int word_counter(char *);

int main()
{
	const int SIZE = 100;
	char user_string[SIZE];

	std::cout << "Enter a string: ";
	std::cin.getline(user_string, SIZE);

	int words = word_counter(user_string);
	
	std::cout << "Amount of words: " << words << std::endl;
	return 0;
}

int word_counter(char *user_string)
{
	int spaces = 0, i;
	for(i = 0; user_string[i] != '\0'; i++)
	{
		if(user_string[i] == ' ')
			spaces++;
	}
	if(i != 0)
	{
		spaces++;
	}
	return spaces;
}

Is this a good way of coding such a program? I don't really know what I'm doing, I just mess around with the code until I can get it work, especially that last function-- and I don't have a teacher, so I have no way of knowing if what I'm doing is acceptable or not. Is this a good program? Could you break it-- meaning, could you input data that would get it to produce an incorrect result?
Last edited on
You use descriptive names and the indention is ok.

Your program indeed does count the spaces not the words. I.e. If you enter 4 spaces and 2 words the answer would be 'Amount of words: 4'
Damn, you're right. That was easy to break . . .
I fixed it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <cctype>

int word_counter(char *);

int main()
{
	const int SIZE = 100;
	char user_string[SIZE];

	std::cout << "Enter a string: ";
	std::cin.getline(user_string, SIZE);

	int words = word_counter(user_string);
	
	std::cout << "Amount of words: " << words << std::endl;
	return 0;
}

int word_counter(char *user_string)
{
	int spaces = 0, i;
	for(i = 1; user_string[i] != '\0'; i++)
	{
		if(isalpha(user_string[i-1]) && user_string[i] == ' ')
			spaces++;
	}
	if(i != 0)
	{
		spaces++;
	}
	return spaces;
}

You can type as many spaces as you want and it won't effect the program, because I made it test if the previous character is an alphabetical character, and the current character is a space, then increment spaces. It won't increment if two spaces are in a row. Someone try to break it again.
1
2
// int word_counter(char *user_string) ;
int word_counter( const char *user_string) ; // const-correct 


And ideally:
1
2
3
// treat C++14 etc. as words and take care of tabs etc. as separators between words
// if(isalpha(user_string[i-1]) && user_string[i] == ' ')
if( !isspace(user_string[i-1]) && isspace(user_string[i]) )
28
29
30
31
	if(i != 0)
	{
		spaces++;
	}


What is this supposed to be doing? I starts at 1 and goes up in the loop at line 23, so it will never be 0, so this loop always executes.

You need to test your code with a number of scenarios.
- an empty string
- 1 word with no space before or after
- 1 word with space(s) before, none after
- 1 word with no space before, space(s) after
- 1 word with space(s) before and after
- The previous 4 tests with 2 word separated by space(s).

This will make sure your algorithm handles all scenarios properly.

As far as rating your coding...

It looks pretty good to me. My comments would be:

22
23
	int spaces = 0, i;
	for(i = 1; user_string[i] != '\0'; i++)


I personally don't like declaring 2 variables in the same line (line 22)--it gets kind of confusing.

If you don't need to use 'i' after the loop (after you figure out what you're doing at line 28), you can declare it within the for statement

for(int i = 1; user_string[i] != '\0'; i++)
Topic archived. No new replies allowed.