0

How to search for a record using only the first name.

User inputs Name(Consists of first name and last name) and is stored using dynamic memory allocation. When i try to search for the name in the records using the first name the program goes back to my menu, but when i search for the contact using both names, the record is found. Can someone please advise me on how i could search a record using only the first name. This is the part where the search happens Eg: name - Homer Simpson tempName - Homer if(strcmp(currentPtr->name,tempName)==0) { printf(details) } Thank you.

9th Apr 2020, 5:42 AM
FS00047
2 ответов
+ 3
strcmp compares for equality, it does not check for occurrence. Use strstr instead, and see if the returned value was non null. #include <string.h> #include <stdio.h> int main() { char* haystack = "Homer Simpson"; char* needle = "Homer "; // http://www.cplusplus.com/reference/cstring/strstr/ char* res = strstr(haystack, needle); if(res != NULL) { printf("'%s' was found in '%s'\nFound at index: %lu\n", needle, haystack, res - haystack); return 0; } }
9th Apr 2020, 6:48 AM
Ipang
+ 1
Can you show how you did?
9th Apr 2020, 6:10 AM
A͢J
A͢J - avatar