user1594906
user1594906

Reputation: 35

Check if char is existing in array

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.

Upvotes: 1

Views: 4753

Answers (3)

leykan
leykan

Reputation: 389

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);

Upvotes: 0

sms
sms

Reputation: 426

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

Upvotes: 0

user4815162342
user4815162342

Reputation: 155600

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

Upvotes: 3

Related Questions