Q. checking single character in string (char array)

Started by darrenf, January 13, 2010, 07:25:50 AM

Previous topic - Next topic

darrenf

Hi,

Wonder if someone can help here.

I am trying to set up the condition....   if char
  • in stringArray is ',' then do something

    a sample code is below.

    Peles C gives me the error....

    >  C:/progs> cc /c chardebug.c
    >  charDebug.c
    >  charDebug.c(29): error #2168: Operands of '==' have incompatible types 'char' and 'char *'.

    some simple code in context of how I want to use it (solve this, solve problem) is....

    ------------ charDebug.c -----------------

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>


    int countChar(char *);


    int main(void)
    {
      char lstr[255];

      sprintf(lstr,"10,45,67,89,ABC");

      printf("\n\n the string has %d data items", countChar(lstr));

    }


    int countChar(char lstr[])
    {
         int  fByte, wpos, numItems;

         numItems=0;
         fByte = strlen(lstr);
             wpos = 1;
             while (wpos<fByte)
              {
               if(lstr[wpos]==" ")
                 numItems++;
              wpos++;
              }

    return(numItems);
    }

    ----------------------------------------------------

    thanks for your help.   :)





AlexN

Quote from: darrenf on January 13, 2010, 07:25:50 AM
>  charDebug.c(29): error #2168: Operands of '==' have incompatible types 'char' and 'char *'.
Try this:
if(lstr[wpos]==' ')
If you use " ", you define a string. if you use ' ', you define a charater.
best regards
Alex ;)

darrenf

thanks Alex,  sitting here now wondering why I had not picked this up.
Your help is appreciated.