演習3-3 K&R プログラミング言語C

演習3-3

#include <stdio.h>

#define MAX 1024

void expand(char s1[], char s2[]);

int main(int argc, char *argv[])
{
    char *str1 = "-c-h-r-b, a-z0-9";
    char str2[MAX];

    expand(str1, str2);
    printf("%s => %s\n", str1, str2);

    return 0;
}

void expand(char s1[], char s2[])
{
    int i, j, from, to;

    for (i = 0, j = 0; s1[i] != '
#include <stdio.h>

#define MAX 1024

void expand(char s1[], char s2[]);

int main(int argc, char *argv[])
{
    char *str1 = "-c-h-r-b, a-z0-9";
    char str2[MAX];

    expand(str1, str2);
    printf("%s => %s\n", str1, str2);

    return 0;
}

void expand(char s1[], char s2[])
{
    int i, j, from, to;

    for (i = 0, j = 0; s1[i] != '\0'; i++) {
        if (s1[i] == '-') {
            from = s1[i-1];
            to = s1[i+1];
            if (i > 1 && to != '\0' && from < to) {
                while (from < to) {
                    s2[j++] = ++from;
                }
                i++;
            } else {
                s2[j++] = s1[i];
            }
        } else {
            s2[j++] = s1[i];
        }
    }
    s2[j] = s1[i];
    s2[++j] = '\0';
}
'
; i++) { if (s1[i] == '-') { from = s1[i-1]; to = s1[i+1]; if (i > 1 && to != '
#include <stdio.h>

#define MAX 1024

void expand(char s1[], char s2[]);

int main(int argc, char *argv[])
{
    char *str1 = "-c-h-r-b, a-z0-9";
    char str2[MAX];

    expand(str1, str2);
    printf("%s => %s\n", str1, str2);

    return 0;
}

void expand(char s1[], char s2[])
{
    int i, j, from, to;

    for (i = 0, j = 0; s1[i] != '\0'; i++) {
        if (s1[i] == '-') {
            from = s1[i-1];
            to = s1[i+1];
            if (i > 1 && to != '\0' && from < to) {
                while (from < to) {
                    s2[j++] = ++from;
                }
                i++;
            } else {
                s2[j++] = s1[i];
            }
        } else {
            s2[j++] = s1[i];
        }
    }
    s2[j] = s1[i];
    s2[++j] = '\0';
}
'
&& from < to) { while (from < to) { s2[j++] = ++from; } i++; } else { s2[j++] = s1[i]; } } else { s2[j++] = s1[i]; } } s2[j] = s1[i]; s2[++j] = '
#include <stdio.h>

#define MAX 1024

void expand(char s1[], char s2[]);

int main(int argc, char *argv[])
{
    char *str1 = "-c-h-r-b, a-z0-9";
    char str2[MAX];

    expand(str1, str2);
    printf("%s => %s\n", str1, str2);

    return 0;
}

void expand(char s1[], char s2[])
{
    int i, j, from, to;

    for (i = 0, j = 0; s1[i] != '\0'; i++) {
        if (s1[i] == '-') {
            from = s1[i-1];
            to = s1[i+1];
            if (i > 1 && to != '\0' && from < to) {
                while (from < to) {
                    s2[j++] = ++from;
                }
                i++;
            } else {
                s2[j++] = s1[i];
            }
        } else {
            s2[j++] = s1[i];
        }
    }
    s2[j] = s1[i];
    s2[++j] = '\0';
}
'
; }

実行結果

$ ./ex3-3
-c-h-r-b, a-z0-9 => -cdefghijklmnopqr-b, abcdefghijklmnopqrstuvwxyz0123456789
プログラミング言語C 第2版 ANSI規格準拠
B.W. カーニハン D.M. リッチー
共立出版
売り上げランキング: 9726
«
»