Hi, im trying to write a code to find whether one string is inside another string, without using the strstr() library. What I got so far is as follows:
Although, it seems to work with the current values of char arrays, when I change things around, it doesnt always produce the desired outcome.Code:#include <stdio.h> int getlength(char[]); int main() { char str1[] = "This is a string"; char str2[] = "str"; int len1 = getlength(str1); int len2 = getlength(str2); int x, i, j; for (i = 0; i < (len1 - len2); i++) { j = 0; if (str2[j] == str1[i]) { for (j = 1; j < len2; j++) { if (str2[j] == str1[i + j]) x = 0; else x = 1; } if (x == 0) { printf("True!"); return 0; } } } printf("False!"); return 1; } /* Get String Length function */ int getlength(char str[]) { int len = 0; while(str[len] != '\0') len++; return len; }
Can anyone please point me to the place that needs to be reviewed?
Thanx in advance![]()



LinkBack URL
About LinkBacks




