I'm practicing my parsing, and I'm writing a program with an overloaded function that counts the words in a user entered cstring and regular string. Suprisingly the cstring was easy peasey but the string object is has me a stumped. I don't think my cstring was the most efficient solution, now that I think about it.
so

(1) How should I approach the string case? Seriously stumped on that one (I can use the same approach as I did for the cstring only the lack of NULL at the end complicates things.

(2) What are other ways to accomplish this task with the cstring (can be better or worse than my solution) just want to see other approaches to the problem. As parsing is definatley one of those, several-ways-to-do-it deals.

int main()
{
    char *input;
    int charLen = 80;
    input = new char[charLen];

    cout << "Please input a string: ";
    cin.getline(input, 80);
    string inputS = input;
   // cout << "CString ----\n";
   // cout << wordCount(input);

    cout << endl << "String ----\n";
     wordCount(inputS);

    delete [] input;
    return 0;
}

// ------ FUNCTION: Word Count C-String---------------------------
// Looks to next and previous chars, if alpha followed by space
// then it counts a word.
// ---------------------------------------------------------------
int wordCount(string input)
{

    int wCount = 0;
    for(int i; i < input.size(); i++)
    {
        //cout << "input[" << i << "] "  << input[i] << " isalpha? " << isalpha(input[i]) << endl;

        if((isalpha(input[i]) != 0) && (isalpha(input[i + 1]) == 0))
        {
            wCount++;
        }
        if(isalpha(input[input.length() - 1]) == 0 && isalpha(input[input.length()]) != 0)
        {
            wCount++;
        }
    }

    cout <<"-1 " << input[input.length() - 1] << endl;
    cout << "REG " << input[input.length() - 2];
    return 0;
}

// ------ FUNCTION: Word Count C-String---------------------------
// Looks to next and previous chars, if alpha followed by space
// then it counts a word.
// ---------------------------------------------------------------
int wordCount(char *input)
{
    int wCount = 0;

    for(unsigned int i = 0; i < strlen(input); i++)
    {
        //cout << "input[" << i << "] "  << input[i] << " isalpha? " << isalpha(input[i]) << endl;

        if((isalpha(input[i]) != 0) && (isalpha(input[i + 1]) == 0))
        {
            wCount++;
        }

    }

    return wCount;
}

You could count the spaces, and add 1.

That's assuming there are no leading or tailing spaces.

(edit) double post <lag>

Member Avatar for Rahul47

I have just started with C++ and dont know much of it. But days ago I coded same problem in C.
I think this might be against the forum rules. But still I hope this will help.

/*
    Name:      Program to count no of words and spaces in a given string.
    Author:    Rahul Mali
    Date:      23-01-14 18:29
    Notes:     1) ASCII value of a space is 32.
*/

#include<stdio.h>

int main(){
    char str[100];
    int i,spaces=0,words=0;
    printf("Enter your string: ");
    gets(str);  
    if(str[0]!=32){      // Checked if starting of string is blank.
        words++;
    }
    for (i=0; str[i]!='\0'; i++){     // Check for spaces.
        if(str[i]==32){      
            spaces++;
            if(str[i+1]!=32&&str[i+1]!='\0'){    // Check for a character after space and if it is end of string. 
                words++;
                }
            }
    }
    printf("No of words are %d.\nNo of spaces are %d",words,spaces);
    getch();
    return 0;
}

Rahul's code is completed and it works as expected, I think it is easy to convert it into c++ yourself

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.