Hi everyone,
I have a project for my class. I have to write a function that accepts 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 would return the number 6. I have worked on the project but I can't seem to put it all together to make it work.. Could someone please look it over and point to my errors and help me correct them? Appreciate the help, thank you.
// this program accepts a c-string as an argument and returns the number of words contained in the string
#include <iostream>
using namespace std;
// function prototype
int countChars(char *, char);
int main()
{
char userString[60], words;
cout << "enter a string (up to 60 characters): ";
cin.getline(userString, 61);
cin >> words;
cout << words << " has ";
cout << countChars(userString, words) << " words.\n";
return 0;
}
int countChars(char *strptr, char ch)
{
int words = 0;
while (*strptr != '\0')
{
if (*strPtr == ch)
words++;
strPtr++;
}
return words;