STRINGS : Counting words with WHILE LOOPS

I'm horrible at C++, I need to count Words in a string in <stdio.h> and <string.h> format ONLY, must be in WHILE Loop! Our professor acts like were programming students when were just marketing students, it's so hard! Please I really need the codes to make it happen!
Hi again!

I see you're still stuck. So I wrote something for you. It works....sort of. There are two things you need to fix. I used cout to print out stuff, and you have to find the <stdio.h> equivalence.

I did not test all cases. So you should test as many cases as you can think of. Try to come up with weird inputs, like "fij____iije_1ij_iij" ...... to see if it works.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <stdio.h>

using namespace std;

int main(){


	char sentence[256];//Stores the sentence
	int vowels = 0; //vowels, spaces, and words are used to count.
	int spaces = 0;
	int words = 0;

	int position = 0;//These two variables go through sentence[256] and checks for vowels,
					// spaces, and words.
	char pointer = 0;

	cout << "To stop, enter in zero." << endl << endl;

	cout << "Enter in a sentence: ";
	fgets(sentence, 256, stdin);

	pointer = sentence[position];

	while(pointer != NULL){
		
		pointer = sentence[position];		
		position++;

			//if it was a vowel.
		if (sentence[position] == 'a' || sentence[position] == 'e' || 	
			sentence[position] == 'i' || sentence[position] == 'o' ||
			sentence[position] == 'u' || sentence[position] == 'y')
				vowels++;

			//if it was a space at that position in the sentence
		else if (sentence[position] == ' ')	
			spaces++;

		//if position was a space, and the one before it wasn't, that means
		//a word ended. If there was a double space, the second space would not trigger this if-statement.
		if ((sentence[position] == ' ' || sentence[position] == NULL)&& 
			(sentence[position - 1] != ' '))
			words++;
		}

	cout << "There were " << vowels <<  " vowels." << endl;
	cout << "There were " << spaces << " spaces." << endl;
	cout << "There were " << words << " words." << endl << endl;

	cout << endl << "--DONE!" << endl;
	system("pause");
}
Topic archived. No new replies allowed.