C Programming. Problem searching for name stored in variable

Status
Not open for further replies.

spitso

Member
Hi all

Just trying to search for a variable (case in-sensitive) and then display the variable that matches, but cant quite get it too work.

attached is my code.

tested with a .txt file and searched for an exact match but nothing displayed

any ideas?
Thanks
 

Attachments

  • searching.png
    32 KB · Views: 152
1. Are you sure you want to pre-increment your index "p" (++p)? Probably should be p++ instead.
2. You misspelled "field". That's why it doesn't work. (Jes' kidding!)
3. Horrible colors. I can't see the comments in blue at all.

So you don't have a strcmpi() (case-insensitive) function in your library? Would save you the trouble of going through strings and tolower()-ing them.
 
searchField is a char and you're assigning the title to it
tolower takes a char and not a char*
also returns a char and not char*
even if you passed char into tolower, it would not change the character to a lower case. You'd have to do someting funky like this

string[X]=tolower(string[x])

But you sholdn't need to convert the case for a case-insensitive search anyway. There are better methods.

PS: On second thought, strcmpi() might not be an ANSI C function, so conversion might be necessary.
 
Last edited:
searchFeild should really be declared as
Code:
char searchFeild[100];
as it's a string (char array), not a single character.

Converting searchFeild to lowercase must be done on all characters in the string, thus
Code:
for(p = searchFeild; *p; p++)
*p = tolower(*p);
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…