1

I am creating project in arduino. In C. How can I check if return char is existing in my array?

This is how I want it.

char n[20];
char *adminName[] = {"Jane", "Joe", "James"};

I want to return true if (n) is in my list.

alk
  • 69,737
  • 10
  • 105
  • 255
user1594906
  • 35
  • 1
  • 6
  • 1
    Here is [duplicate](http://stackoverflow.com/questions/1071542/in-c-check-if-a-char-exists-in-a-char-array) – Deepak Ingole Sep 27 '13 at 08:35
  • @captain Not really a duplicate of that post, since that post requests a blacklist of characters. There are likely a bunch of others that ask for this, though. – user4815162342 Sep 27 '13 at 09:04

3 Answers3

3

Loop over array indices and use strcmp(n, adminName[i]) == 0 to test whether the string n is part of the array.

user4815162342
  • 141,790
  • 18
  • 296
  • 355
0

you have to use the strcmp() that check the diff between 2 char *

char n[20];
char *adminName[] = {"Jane", "Joe", "James"};
int  i;

i = 0;
while (admminName[i])
 {
    if (strcmp(n, adminName[i]) == 0)
      return (true); 
    i++;
 }
return (false);
leykan
  • 389
  • 2
  • 4
  • 24
0

There are many built in functions are there for this. why cant you use those functions rather than checking manually by loops?

sms
  • 426
  • 1
  • 8
  • 23