0

I want to pass a char string as a pointer reference and then count the words in this string ... but somehow I never get to count the right number of words... here my code:

#include <iostream>
#include <stdio.h>
using namespace std;

int charCount(const char* pPtr);

int main() {

    char wort[] = "Ein Neger mit Gazelle zagt im Regen nie ";
    int count(0);

    count = charCount(wort);
    cout <<count <<endl;

}

int charCount(const char* pPtr) {
    int wordCount(0);

    while(*pPtr != '\0') {      

        //Falls EOF Erreicht und vorheriger Buchstabe war kein Blank oder newline dann Wortzaehler erhoehen
        if ((*pPtr == '\0') && (*(pPtr-1) !=' ' || *(pPtr-1) != '\n')) {
            wordCount++;

        }

        //Falls Blank oder Newline, und vorheriger Buchstabe war kein Blank oder Newline, Wortzaehler erhoehen
        if (((*(pPtr+1) == ' ' || *(pPtr+1) == '\n')) && ((*(pPtr) != ' ' || *(pPtr) != '\n' ))) {
                wordCount++;                
        }       
        pPtr++;

    }
    return wordCount;
}
2
  • What result do you actually get? Commented Nov 15, 2013 at 17:11
  • I got 7, but when I add blanks at the end, he counts them as words... Commented Nov 15, 2013 at 17:36

3 Answers 3

1

Looks like the while(*pPtr != EOF) should actually be while(*pPtr != NULL).

EOF on some systems is 0 (like NULL) and on some it might be -1 or any other value.

Also, looks like a better approach to your problem is having some kind of 'state-machine', i.e:

int in_word = 0;
while (*pPtr != NULL){
    if ((*pPtr >= 'a' && *pPtr <= 'z') || <same for uppercase>){
        in_word = 1;
    }
    else if (in_word == 1){
        wordCount++;
        in_word = 0;
    }

Not sure if this covers everything.. but I hope you get the general idea.

Sign up to request clarification or add additional context in comments.

4 Comments

I don't believe I've ever seen a system where EOF == 0. I will admit, though, that I have not perused every available system...
I believe that in anything ANSI / Modern etc you will in fact never find it. Here you go: stackoverflow.com/a/1624150/1778249
this is not very helpful, it doesnt even execute correctly... has nobody an idea how to realise a wordcount with pointers please?
EDIT: Actually , it was very helpful ;) How can I expand it for alphanumeric characters , also with numbers and other characters, just not for blanks and newline characters please?
1

I think the easiest way to count words in a string is using stringstreamming!

#include <iostream>
#include <sstream>
using namespace std;

int charCount(const char* pPtr);

int main() {
    char wort[] = "Ein Neger mit Gazelle zagt im Regen nie ";
    int count(0);
    count = charCount(wort);
    cout <<count <<endl;
}

int charCount(const char* pPtr) {
    int wordCount(0);
    stringstream ss;
    string temp;
    ss<<string(pPtr);
    while(ss>>temp)
        wordCount++;
    return wordCount;
}

http://www.cplusplus.com/reference/sstream/stringstream/

Comments

1

I think it should be while(*pPtr != '\0')

Note that '\0' is the end of a char array, and generally, EOF is -1, but there is no -1 in your char array so the loop will go above your char array until it finds -1

int charCount(const char* pPtr) {
int wordCount(0);
int track;
while(*pPtr != NULL) {       
    if ((*pPtr == ' ' || *pPtr == '\n' || *pPtr == '\r')&& track != 0){
        //cout << *pPtr << endl;
        wordCount++;
        track = 0;
    }else if ((*pPtr != ' ' && *pPtr != '\n' && *pPtr != '\r')){
        track++;
    }
    pPtr++;
}
return wordCount;
}

Try this!

1 Comment

ok I added '\0' but I had this before and he still counts wrong , blanks at the end = words...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.