NO

Author Topic: Q. checking single character in string (char array)  (Read 3044 times)

darrenf

  • Guest
Q. checking single character in string (char array)
« on: January 13, 2010, 07:25:50 AM »
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.   :)





Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: Q. checking single character in string (char array)
« Reply #1 on: January 13, 2010, 01:48:48 PM »
>  charDebug.c(29): error #2168: Operands of '==' have incompatible types 'char' and 'char *'.
Try this:
Code: [Select]
if(lstr[wpos]==' ')If you use " ", you define a string. if you use ' ', you define a charater.
best regards
 Alex ;)

darrenf

  • Guest
Re: Q. checking single character in string (char array)
« Reply #2 on: January 14, 2010, 02:51:29 AM »
thanks Alex,  sitting here now wondering why I had not picked this up.
Your help is appreciated.