new files
This commit is contained in:
parent
3af7344581
commit
86c034bb8b
1364 changed files with 21352 additions and 0 deletions
40
Task/Search-a-list/C/search-a-list.c
Normal file
40
Task/Search-a-list/C/search-a-list.c
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
const char *haystack[] = {
|
||||
"Zig", "Zag", "Wally", "Ronald", "Bush", "Krusty", "Charlie",
|
||||
"Bush", "Boz", "Zag", NULL
|
||||
};
|
||||
|
||||
int search_needle(const char *needle, const char **hs)
|
||||
{
|
||||
int i = 0;
|
||||
while( hs[i] != NULL ) {
|
||||
if ( strcmp(hs[i], needle) == 0 ) return i;
|
||||
i++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int search_last_needle(const char *needle, const char **hs)
|
||||
{
|
||||
int i, last=0;
|
||||
i = last = search_needle(needle, hs);
|
||||
if ( last < 0 ) return -1;
|
||||
while( hs[++i] != NULL ) {
|
||||
if ( strcmp(needle, hs[i]) == 0 ) {
|
||||
last = i;
|
||||
}
|
||||
}
|
||||
return last;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("Bush is at %d\n", search_needle("Bush", haystack));
|
||||
if ( search_needle("Washington", haystack) == -1 )
|
||||
printf("Washington is not in the haystack\n");
|
||||
printf("First index for Zag: %d\n", search_needle("Zag", haystack));
|
||||
printf("Last index for Zag: %d\n", search_last_needle("Zag", haystack));
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue