Ahmad Khateeb
Ahmad Khateeb

Reputation: 73

counting words of a string

As the title states, few things I must add to explain: " "/tab/","/"." are things that divide words in my situation, another note that there can be more the one space or dot coming one after the other

this is what I have

int countWords(char * str, int length){
       int counter = 0;

        for( int i = 0; i < length; i++){
            if(( str[i] == " ") || ( str[i] == ".") || ( str[i] == ",")){
                if(( str[i+1] != " ") || ( str[i+1] != ".") || (str[i+1] != ",")){
                    if(( str[i-1] != " ") || ( str[i-1] != ".") || (str[i-1] != ","))
                        counter++;
                }
            }
        }
        return counter;
    }

I get an error saying that I can not compare int and a pointer, I do understand where that is coming from, but how can I get it to actually work?

Note* I can't use string.h

Upvotes: 1

Views: 135

Answers (2)

Vishwajeet Vishu
Vishwajeet Vishu

Reputation: 492

If you want to count word there is one fine example in K&R ANSI C. Try to write program like this.

#include <stdio.h>
#define IN  1    /* inside a word */
#define OUT 0    /* outside a word */
/* count lines, words, and characters in input */
main()
{
   int c, nl, nw, nc, state;
   state = OUT;
   nl = nw = nc = 0;
   while ((c = getchar()) != EOF)
 {
    ++nc;
    if (c == '\n')
        ++nl;
    if (c == ' ' || c == '\n' || c = '\t')
        state = OUT;
    else if (state == OUT) 
    {
        state = IN;
        ++nw;
     }
   }
printf("%d %d %d\n", nl, nw, nc);
}

Hope this helps. :) You can find the pdf of this book here.

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310920

In expressions like this

str[i] == " "
          ^^^

you are trying to compare an object of type char with a string literal that is implicitly converted to the type char *.

You have to use a character constant instead of the string literal

str[i] == ' '
          ^^^

The enclosing if statements do not make sense. For example for i equal to 0 the expression str[i-1] tries to access memory beyond the string.

The first parameter of the function should be declared with qualifier const.

The function can look the following way as it is shown in the demonstrative program.

#include <stdio.h>

size_t countWords( const char *s ) 
{
    size_t n = 0;

    for (size_t i = 0; s[i] != '\0'; )
    {
        while (s[i] == ' '  || 
               s[i] == '\t' || 
               s[i] == '.'  || 
               s[i] == ',') ++i;

        if (s[i] != '\0')
        {
            ++n;
            while ( s[i] != '\0' &&
                    ! ( s[i] == ' '  ||
                        s[i] == '\t' ||
                        s[i] == '.'  ||
                        s[i] == ',')) ++i;

        }
    }

    return n;
}

int main( void )
{
    char *s = "  Hello\t, World...";

    printf("%zu\n", countWords(s));
}

The program output is

2

Or the function's implementation can look like

size_t countWords(const char *s)
{
    size_t n = 0;

    while ( *s )
    {
        while (*s == ' '  ||
               *s == '\t' ||
               *s == '.'  ||
               *s == ',') ++s;

        if ( *s )
        {
            ++n;
            while ( *s &&
                    !( *s == ' '  ||
                       *s == '\t' ||
                       *s == '.'  ||
                       *s == ',' ) ) ++s;

        }
    }

    return n;
}

A more general approach of declaring the function is the following

size_t countWords( const char *s1, const char *s2 );

where the string s2 specifies a set of word separators.

Upvotes: 1

Related Questions