Created
January 20, 2013 06:59
-
-
Save taishi41228/4577075 to your computer and use it in GitHub Desktop.
文字列探索のサンプルプログラムstring_search.c
strlen,strncmp関数を再現した自作の関数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int mystrncmp(char str[], char str2[], int n) { | |
int i; | |
for (i = 0; i < n; i++) { | |
if (str[i] != str2[i]) { | |
return 1; | |
} | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int mystrlen(char str[]) { | |
int i; | |
for (i = 0; str[i] != '\0'; i++); | |
return i; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
int main(int argc, char *argv[]) { | |
int i; | |
char text[] = "hello world"; | |
char pattern[] = "world"; | |
int textLength = strlen(text); | |
int patternLength = strlen(pattern); | |
for (i = 0; i < textLength - patternLength + 1; i++) { | |
if (!strncmp(&text[i], pattern, patternLength)) { | |
printf("%d文字目で発見しました。", i); | |
break; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment