--- title: 文字列の扱い author: kazu634 date: 2009-05-17 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:4599;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}' categories: - C ---
『プログラミング言語C 第2版 ANSI規格準拠』の練習問題を解いていました:
文字列s2中の任意の文字に等しい文字をslから除去するような形のsqueeze(sl, s2)を書け。
#include <stdio.h> /* prototype declarations */ void squeeze(char source[], char target[], int num); int count_char(char target[]); /* program begins here */ int main(int argc, char *argv[]) { char source[] = "12345123451234567890"; char test[] = "1278"; squeeze(source, test, count_char(test)); printf("%s\n", source); return ; } void squeeze(char source[], char target[], int num) { int i; /* sourceの添え字 (before update) */ int j; /* sourceの添え字 (after update) */ int k; /* index of target */ int flag; /* flag */ for (i = j = ; source[i] != '\0'; i++) { flag = ; for (k = ; k < num; k++) { if (source[i] == target[k]){ flag = 1; break; } else { flag = ; } } if (flag == ){ source[j++] = source[i]; } } source[j] ='\0'; } int count_char(char target[]) { int i; /* working variable */ int num = ; /* Number of chars */ for (i = ; target[i] != '\0'; i++) { num++; } return num; }