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

演習5-5

strncpystrncat の返り値のポインタは最初に退避させておく。

strncpy

#include <stdio.h>
#include <string.h>

#define MAX 100

char *my_strncpy(char*, char*, int);

int main(void)
{
    char *s = "hello";
    char s1[MAX] = "hello";
    char s2[MAX] = "hello";
    char s3[MAX] = "hello";
    char s4[MAX] = "hello";
    char s5[MAX] = "hello";
    char s6[MAX] = "hello";
    char *ct1 = "world";
    char *ct2 = "world";
    char *ct3 = "world";
    char *ct4 = "world";
    char *ct5 = "world";
    char *ct6 = "world";

    printf("strncpy(\"%s\", \"%s\", %d) => %s\n", s, ct1, 2, strncpy(s1, ct1, 2));
    printf("strncpy(\"%s\", \"%s\", %d) => %s\n", s, ct2, 5, strncpy(s2, ct2, 5));
    printf("strncpy(\"%s\", \"%s\", %d) => %s\n", s, ct3, 9, strncpy(s3, ct3, 9));

    printf("my_strncpy(\"%s\", \"%s\", %d) => %s\n", s, ct4, 2, my_strncpy(s4, ct4, 2));
    printf("my_strncpy(\"%s\", \"%s\", %d) => %s\n", s, ct5, 5, my_strncpy(s5, ct5, 5));
    printf("my_strncpy(\"%s\", \"%s\", %d) => %s\n", s, ct6, 9, my_strncpy(s6, ct6, 9));

    return 0;
}

char *my_strncpy(char *s, char *ct, int n)
{
    char *rt;

    rt = s;
    while (*ct && n) {
        *s++ = *ct++;
        n--;
    }
    if (n > 0)
        *s = '\0';

    return rt;
}

実行結果

$ ./strncpy
strncpy("hello", "world", 2) => wollo
strncpy("hello", "world", 5) => world
strncpy("hello", "world", 9) => world
my_strncpy("hello", "world", 2) => wollo
my_strncpy("hello", "world", 5) => world
my_strncpy("hello", "world", 9) => world

strncat

#include <stdio.h>
#include <string.h>

#define MAX 100

char *my_strncat(char*, char*, int);

int main(void)
{
    char *s = "hello";
    char s1[MAX] = "hello";
    char s2[MAX] = "hello";
    char s3[MAX] = "hello";
    char s4[MAX] = "hello";
    char s5[MAX] = "hello";
    char s6[MAX] = "hello";
    char *ct1 = "world";
    char *ct2 = "world";
    char *ct3 = "world";
    char *ct4 = "world";
    char *ct5 = "world";
    char *ct6 = "world";

    printf("strncat(\"%s\", \"%s\", %d) => %s\n", s, ct1, 2, strncat(s1, ct1, 2));
    printf("strncat(\"%s\", \"%s\", %d) => %s\n", s, ct2, 5, strncat(s2, ct2, 5));
    printf("strncat(\"%s\", \"%s\", %d) => %s\n", s, ct3, 9, strncat(s3, ct3, 9));

    printf("my_strncat(\"%s\", \"%s\", %d) => %s\n", s, ct4, 2, my_strncat(s4, ct4, 2));
    printf("my_strncat(\"%s\", \"%s\", %d) => %s\n", s, ct5, 5, my_strncat(s5, ct5, 5));
    printf("my_strncat(\"%s\", \"%s\", %d) => %s\n", s, ct6, 9, my_strncat(s6, ct6, 9));

    return 0;
}

char *my_strncat(char *s, char *ct, int n)
{
    char *rt;

    rt = s;
    while (*s)
        s++;
    while (*ct && n) {
        *s++ = *ct++;
        n--;
    }
    if (n > 0)
        *s = '\0';

    return rt;
}

実行結果

$ ./strncat
strncat("hello", "world", 2) => hellowo
strncat("hello", "world", 5) => helloworld
strncat("hello", "worlds", 9) => helloworlds
my_strncat("hello", "world", 2) => hellowo
my_strncat("hello", "world", 5) => helloworld
my_strncat("hello", "worlds", 9) => helloworlds

strncmp

#include <stdio.h>
#include <string.h>

int my_strncmp(char*, char*, int);

int main(void)
{
    char *ct1 = "world";
    char *ct2 = "worlds";

    printf("strncmp(\"%s\", \"%s\", %d) => %d\n", ct1, ct2, 2, strncmp(ct1, ct2, 2));
    printf("strncmp(\"%s\", \"%s\", %d) => %d\n", ct1, ct2, 5, strncmp(ct1, ct2, 5));
    printf("strncmp(\"%s\", \"%s\", %d) => %d\n", ct1, ct2, 6, strncmp(ct1, ct2, 6));
    printf("strncmp(\"%s\", \"%s\", %d) => %d\n", ct2, ct1, 6, strncmp(ct2, ct1, 6));

    printf("my_strncmp(\"%s\", \"%s\", %d) => %d\n", ct1, ct2, 2, my_strncmp(ct1, ct2, 2));
    printf("my_strncmp(\"%s\", \"%s\", %d) => %d\n", ct1, ct2, 5, my_strncmp(ct1, ct2, 5));
    printf("my_strncmp(\"%s\", \"%s\", %d) => %d\n", ct1, ct2, 6, my_strncmp(ct1, ct2, 6));
    printf("my_strncmp(\"%s\", \"%s\", %d) => %d\n", ct2, ct1, 6, my_strncmp(ct2, ct1, 6));

    return 0;
}

int my_strncmp(char *cs, char *ct, int n)
{
    while (n && (*cs && *ct) && *cs++ == *ct++)
        n--;
    if (n == 0 || (!*cs && !*ct)) /* n が 0, または, *cs と *ct が共に '\0' */
        return 0;

    return *cs - *ct;
}

実行結果

$ ./strncmp
strncmp("world", "worlds", 2) => 0
strncmp("world", "worlds", 5) => 0
strncmp("world", "worlds", 6) => -115
strncmp("worlds", "world", 6) => 115
my_strncmp("world", "worlds", 2) => 0
my_strncmp("world", "worlds", 5) => 0
my_strncmp("world", "worlds", 6) => -115
my_strncmp("worlds", "world", 6) => 115
プログラミング言語C 第2版 ANSI規格準拠
B.W. カーニハン D.M. リッチー
共立出版
売り上げランキング: 9726
«
»