Return the number of times the character " " appears in the text.

I need to make a Word Counter, I need to Return the number of times the character " " appears in the text.

with a function that accepts a pointer to a C-string as an argument and returns the number of words contained in the string.

•For instance, if the string argument is "Four Score and seven years ago" the function should return the number 6.

•Demonstrate the function in a program that asks the user to input a string and then passes it to the function.

•The number of words in the string should be displayed on the screen.
•Allow for sentences of up to 100 characters.




I must have all of this in the program otherwise it will be rejected:


Correct Prototype.
Correct function declaration
Returns the number of times the character " " appears in the text.
Got the word count correctly


Here is my code
I just need to return the number of times the character " " appears in the text.

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 <string>
using namespace std;

// function prototype
int numWords(char*);
int numWords(string);

int main()
{
	char* myPtr = nullptr;

	const int SIZE = 101;
	char userSent[SIZE];

	myPtr = userSent;

	string sent;

	cout << "Enter a sentence less than " << (SIZE - 1) << " characters" << endl;
	cin.getline(myPtr, SIZE);
	//getline(cin, sent);

	//display the number of words contained in the user sentence
	cout << "Your sentence contains " << numWords(myPtr) << " words" << endl;

	return 0;
}

//======================================================================
// function definition - counts how many words are in the string that is passed as an arg
int numWords(char* sentence)
{
	int totalWords = 0; // counts words

	if (*(sentence) != '\0') // if some input then there is at least 1 word
		++totalWords;

	for (int count = 0; *(sentence+count) != '\0'; count++)
	{
		if (*(sentence + count) == ' ')
		{
			++totalWords;
		}
	}
	return totalWords;
}





Last edited on
So did you test it with "hello world".

How many spaces do you see?
How many words do you see?

Try calling your function 'numSpaces', not 'numWords', because you're counting spaces at the moment, not counting words.

If you're building a fence, how many fence posts do you need?
https://en.wikipedia.org/wiki/Off-by-one_error#Fencepost_error
your answer doesn't relate to my question but thanks for your try
Maybe you should read it.

Not fixing your bad thought process just means you're back here tomorrow with your latest confusion.
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
#include <iostream>
using namespace std;

int numWords(const char*);

int main()
{
	const int SIZE {101};

	char userSent[SIZE];

	cout << "Enter a sentence less than " << (SIZE - 1) << " characters" << endl;
	cin.getline(userSent, SIZE);

	//display the number of words contained in the user sentence
	cout << "Your sentence contains " << numWords(userSent) << " words" << endl;
}

//======================================================================
// function definition - counts how many words are in the string that is passed as an arg
int numWords(const char* sentence)
{
	int totalWords {};

	if (*sentence)
		for (++totalWords; *sentence; totalWords += *(sentence++) == ' ');

	return totalWords;
}

Last edited on
Hey Seeplus, that's not what I want,
I just want to add the number of times the quotation mark appear (" " ) in the text.
Last edited on
Sorry, I must have mis-read what you wanted. This will return the number of the specified char (default to ' ') in the c-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
#include <iostream>
using namespace std;

int numChar(const char*, char toFind = ' ');

int main()
{
	const int SIZE {101};

	char userSent[SIZE];

	cout << "Enter a sentence less than " << (SIZE - 1) << " characters" << endl;
	cin.getline(userSent, SIZE);

	//display the number of words contained in the user sentence
	cout << "Your sentence contains " << numChar(userSent) << endl;
	cout << "Your sentence contains " << numChar(userSent, '\'') << endl;
}

int numChar(const char* sentence, char tofind)
{
	int totalChar {};

	for (; *sentence; totalChar += *(sentence++) == tofind);

	return totalChar;
}

Last edited on
thanks but it didn't work
it has to be the double quotation marks ""
also the count of "Four Score and seven years ago" is now 5 it was 6

I want the output to be like this


Enter a sentence less than 100 characters:
"Four Score and seven years ago"
Your sentence contains 6 words and 2 quotation marks



thx I solved it by adding new function
Last edited on
I ignored the untidy 101 characters because the hidden last character is always a bone of contention. Maybe choose 99 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <cstring>

using namespace std;

int numWords(char*, int);

int main()
{
    const int SIZE = 100;
    char sentence[SIZE];
    
    cout << "Enter a sentence less than " << SIZE << " characters: ";
    cin.getline(sentence, SIZE);
    
    cout
    << "Your sentence contains " << numWords(sentence, strlen(sentence))
    << " words" << endl;

    return 0;
}

int numWords(char* sentence, int length)
{
    int word_count{0};
    
    if(length == 0)
        return word_count;
    else
        word_count = 1;
    
    
    for (int i = 0; sentence[i] != '\0'; i++)
    {
        if (sentence[i] == ' ')
        {
            word_count++;
        }
    }
    return word_count;
}


Enter a sentence less than 100 characters: this is a lot of words
Your sentence contains 6 words
Program ended with exit code: 0
it has to be the double quotation marks ""
also the count of "Four Score and seven years ago" is now 5 it was 6

I want the output to be like this


You only really need the one function:

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
#include <iostream>
using namespace std;

int numChar(const char*, char toFind = ' ');

int main()
{
	const int SIZE {101};

	char userSent[SIZE];

	cout << "Enter a sentence less than " << (SIZE - 1) << " characters" << endl;
	cin.getline(userSent, SIZE);

	cout << "Your sentence contains " << numChar(userSent) + (*userSent != 0) << " words and " << numChar(userSent, '\"') << " quotation marks\n";
}

int numChar(const char* sentence, char tofind)
{
	int totalChar {};

	for (; *sentence; totalChar += *(sentence++) == tofind);

	return totalChar;
}

Last edited on
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
#include <iostream>
#include <cstring>

using namespace std;

int numWords(char*, int, int&);

int main()
{
    const int SIZE = 100;
    char sentence[SIZE];
    
    cout << "Enter a sentence less than " << SIZE << " characters: ";
    cin.getline(sentence, SIZE);
    int qmc{0};
    
    cout
    << "Your sentence contains " << numWords(sentence, strlen(sentence), qmc)
    << " words and " << qmc << " quotation marks\n";

    return 0;
}

int numWords(char* sentence, int length, int& qm_count)
{
    int word_count{0};
    
    if(length == 0)
        return word_count;
    else
        word_count = 1;
    
    
    for (int i = 0; sentence[i] != '\0'; i++)
    {
        if (sentence[i] == ' ')
        {
            word_count++;
        }
        
        if (sentence[i] == '"')
        {
            qm_count++;
        }
    }
    return word_count;
}


Enter a sentence less than 100 characters: "Four Score and seven years ago"
Your sentence contains 6 words and 2 quotation marks
Program ended with exit code: 0
Topic archived. No new replies allowed.