function that counts words in a string & function counts consonants in a string

I have to write two functions, one that counts words in a string and one that counts consonants in a string. I've already asked for user to input string in main function.
Here is my function code for counting words.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  int countWords (string& str, int spaces)
{
	spaces = 0;
	for (int i=0; i<int(str.length()); i++)//checks characters in string
	{
		if (isspace(str[i]))//if there is a space
		{
			spaces++;
		}
	}

	
	return spaces;
}


And here is my code for counting consonants in a string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  int countConsonants (string& str, int count)
{

	for (int i=0; i<int(str.length()); i++)
	{
		i = toupper(i); //so all characters are uppercase
		if (i != 'A' && i != 'E')
		{
			if (i != 'I' && i != 'O')
			{
				if (i != 'U')
				{
					count++;
				}
			}
		}
	}
	
	return count;
}


Here is the code where I call for both functions and display the output.
1
2
3
4
5
6
7
8
9
10
11
12
13
                if (choice == '5')
		{
			int countWords(string& str, int spaces);
			countWords(str, spaces);//calls function to count words in string
			cout << "The string " << str << " has " << spaces << " words." << endl;
		}
		
		if (choice == '6')
		{
			int countConsonants(string& str, int count);
			countConsonants(str, count);
			cout << "The number of consonants in the string is " << count << endl;
		}


The output for both functions are 0 even when I input a string with multiple words.
Last edited on
The second parameter for countConsonants (...) and countWords(...) doesn't make sense. Use the result instead:
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
  int countConsonants (string& str, int count)
{
	int count=0;
	for (int i=0; i<int(str.length()); i++)
	{
		i = toupper(i); //so all characters are uppercase
		char ch = toupper(str[i]); //Note: use the character of the string not the index
		if (ch != 'A' && ch != 'E') // Note: use ch
		{
// etc...
		}
	}
	
	return count;
}

...
		
		if (choice == '6') // similar for countWords 
		{
			int countConsonants(string& str, int count);
			countConsonants(str, count);
			cout << "The number of consonants in the string is " << count << endl;
			cout << "The number of consonants in the string is " << countConsonants(str) << endl;
		}

By the way: countWords(...) indeed does only count spaces. What if there are more than one spaces between the words?
Hi, I hope this code help you.

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
#include <stdio.h>
#include <string.h>
#include <ctype.h>


unsigned const_count(char* string){
	unsigned const_count =0 ;
	for(int j=0;j < strlen(string);j++){
		switch(toupper(string[j])){
			case 'A':;break;
			case 'E':;break;
			case 'I':;break;
			case 'O':;break;
			case 'U':;break;
			default:const_count++;
		}
	}
	return const_count;
}

int main(void){
	char str[] = "this is a string 01 3.14"; /* actual string */
	char buff[32] = { 0 };              /* a copy of actual string */
	char delim[] = " ";                 /* used to specify the substrings */
	char* token = NULL;                 /* used to store substring token from the actual string */
	unsigned word_count =0;
	strcpy(buff,str);
	token = strtok(buff,delim);
	/* (0) output provided data */
	if(token != NULL){
		printf("\n actual = '%s'\n",str);
		printf(" delim  = '%s'\n",delim);
	}
	/* (1) check if delim exists, show substring and increase word counter */
	if(strlen(token) != strlen(str)){
		printf(" substrings = '%s'%s",token,delim);
		printf("(%d)",const_count(token));
		word_count++;
	}else 
		printf("\n (!) error, couldn't find '%s'\n",delim);
	/* (2) proccess each substring if found any, show it and increase word counter */
	while((token = strtok(NULL,delim)) != NULL){
		printf("'%s'%s",token,delim);
		printf("(%d)",const_count(token));
		word_count++;
	}
	printf("\n");
	printf("\n  - total words in '%s' = %d words\n",str,word_count);
	return 0;
}


Output:


 actual = 'this is a string 01 3.14'
 delim  = ' '
 substrings = 'this' (3)'is' (1)'a' (0)'string' (5)'01' (2)'3.14' (4)

  - total words in 'this is a string 01 3.14' = 6 words

Last edited on
Topic archived. No new replies allowed.