C examples for String:String Function
returns index of string s2 in string s1 and returns -1 if no substring s2 is found in s1.
int strindex(char s1[], char s2[]) { int j, k, m; for(j = 0; s1[j] != '\0'; j++){ for(k = j, m = 0; s2[m] != '\0' && s1[k] == s1[m]; k++, m++) ; /* null statement */ if(m > 0 && s2[m] == '\0') return j; } return -1; }
#include <stdio.h> int strindex(char s1[], char s2[]) { int j, k, m;//w ww.j a v a2s. co m for(j = 0; s1[j] != '\0'; j++){ for(k = j, m = 0; s2[m] != '\0' && s1[k] == s1[m]; k++, m++) ; /* null statement */ if(m > 0 && s2[m] == '\0') return j; } return -1; } int main(void) { char *str = " this is a test "; char *s = "this"; int index = strindex(str, s); printf("%d", index); return 0; }