--- title: 『プログラミング言語C 第2版 ANSI規格準拠』の勉強 author: kazu634 date: 2009-05-30 wordtwit_post_info: - 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4623;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}' categories: - C ---

ようやく関数の項目まで入ってきました。とりあえず打ち込んだサンプルだけ、貼り付けておきますね。

今回は簡単な grep の実装だそうです:

#include <stdio.h>
#define MAXLINE 1000
/* prototype declarations: */
int getline(char line[], int max);
int strindex(char source[], char searchfor[]);
/* Declaration */
char pattern[] = "ould";
/* program begins here: */
int main(int argc, char *argv[]) {
char line[MAXLINE];
int found = ;
while (getline(line, MAXLINE) > ){
if (strindex(line, pattern) >= ){
printf("%s", line);
found++;
}
}
printf("%d", found);
return ;
}
/* getline: sに行を入れて、長さを返す*/
int getline(char s[], int lim)
{
int c, i;
i = ;
while (--lim >  && (c = getchar()) != EOF && c != '\n') {
s[i++] = c;
}
if (c == '\n'){
s[i++] = c;
}
s[i] = '\0';
return i;
}
/* strindex: sにおけるtのインデックスを返す。どこにもなければ-1を返す */
int strindex(char s[], char t[])
{
int i, j, k;
for (i = ; s[i] != '\0'; i++) {
for (j = i, k = ; t[k] != '\0' && s[j] == t[k]; j++, k++)
;
if (k >  && t[k] == '\0') {
return i;
}
}
return -1;
}
プログラミング言語C 第2版 ANSI規格準拠

プログラミング言語C 第2版 ANSI規格準拠