Skip to content

Instantly share code, notes, and snippets.

@taishi41228
Created January 20, 2013 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taishi41228/4577075 to your computer and use it in GitHub Desktop.
Save taishi41228/4577075 to your computer and use it in GitHub Desktop.
文字列探索のサンプルプログラムstring_search.c strlen,strncmp関数を再現した自作の関数
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;
}
int mystrlen(char str[]) {
int i;
for (i = 0; str[i] != '\0'; i++);
return i;
}
#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