C
#include <stddef.h>
char *find_needle(const char **haystack, size_t count) {
int idx = -1;
for(int i=0; i<count; i++) {
if(strcmp(haystack[i], "needle")==0) {
idx=i;
break;
}
}
char* ret = "";
asprintf(&ret, "found the needle at position %d", idx);
return ret;
}
#include <stddef.h>
char *find_needle(const char **haystack, size_t count) {
for(int i=0; i<count;++i) {
if(!strcmp(haystack[i], "needle")) {
char* ret;
asprintf(&ret, "found the needle at position %d", i);
return ret;
}
}
}
#include <stddef.h>
char *find_needle(const char **haystack, size_t count) {
while (strcmp(haystack[--count], "needle"));
char *ret = malloc(128);
sprintf(ret, "found the needle at position %d", count);
return ret;
}
PHP
function findNeedle($haystack) {
return 'found the needle at position ' . array_search("needle", $haystack);
}
function findNeedle($haystack) {
foreach($haystack as $k => $t) {
if( $t == 'needle' ) return "found the needle at position $k";
}
}
function findNeedle($haystack) {
return in_array('needle',$haystack) ? 'found the needle at position ' . array_search('needle',$haystack) : false;
}