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.

2
  • 1
    Here is duplicate Commented Sep 27, 2013 at 8: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. Commented Sep 27, 2013 at 9:04

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

There is a built-in function that searches for a string in an array of strings? Which one?
That sounds like Java or C++, there is no such function in C.
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);

1 Comment

The adminName[] array does not have a NULL entry - your while loop will go past the end of the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.