C Primer Plus 第六版 第11章 编程答案

C Primer Plus 第六版 第11章 编程答案

1.第1题

#include 

#define SIZE 10

char * getnchar(char *, int);

int main(void)
{
     
    char st[SIZE];
    char * sts;
    printf("请输入:");
    sts = getnchar(st, SIZE - 1);
    if (sts == NULL)
        puts("输入失败");
    else
        puts(st);

    return 0;
}

char * getnchar(char * st, int n)
{
     
    int i;
    int ch;

    for (i = 0 ; i < n; i++)
    {
     
        ch = getchar();
        if (ch == EOF)
            break;
        else
            st[i] = ch;
    }
    if (ch == EOF)
        return NULL;
    else
    {
     
        st[i] = '\0';
        return st;
    }
}

2.第2题

#include 

#define SIZE 10

char * getnchar(char * st, int n);

int main(void)
{
     
    char st[SIZE];
    char * sts;

    printf("请输入:");
    sts = getnchar(st, SIZE-1);

    if (sts == NULL)
        puts("输入失败");
    else
        puts(st);

    return 0;
}

char * getnchar(char * st, int n)
{
     
    int i;
    int ch;

    for (i = 0; i < n; i++)
    {
     
        ch = getchar();
        if (ch == EOF || ch == '\n' || ch == '  ' || ch ==' ')
            break;
        else
            st[i] = ch;
    }
    if (ch == EOF)
        return NULL;
    else
    {
     
        st[i] = '\0';
        return st;
    }
}

3.第3题

#include 

#define SIZE 10

char * getnchar(char * st, int n);

int main(void)
{
     
    char st[SIZE];
    char * sts;

    printf("请输入");
    sts = getnchar(st, SIZE - 1);
    if (sts == NULL)
        puts("输入错误");
    else
        puts(sts);

    return 0;
}

char * getnchar(char * st, int n)
{
     
    int i;
    int ch;

    while ((ch = getchar()) == ' ' || ch == '   ' || ch == '\n')
        continue;
    for (i = 0; i < n; i++)
    {
     
        if (ch == EOF || ch == ' ' || ch == '   ' || ch == '\n')
            break;
        else
            st[i] = ch;
        ch = getchar();
    }
    if (ch == EOF)
        return NULL;
    else
    {
     
        st[i] = '\0';
        return st;
    }
}

4.第4题

#include 

#define SIZE 10

char * getnchar(char * st, int n);

int main(void)
{
     
    char st[SIZE];
    char * sts;

    printf("请输入");
    sts = getnchar(st, SIZE - 1);
    if (sts == NULL)
        puts("输入错误");
    else
        puts(sts);

    return 0;
}

char * getnchar(char * st, int n)
{
     
    int i;
    int ch;

    while ((ch = getchar()) == ' ' || ch == '   ' || ch == '\n')
        continue;
    for (i = 0; i < n; i++)
    {
     
        if (ch == EOF || ch == ' ' || ch == '   ' || ch == '\n')
            break;
        else
            st[i] = ch;
        ch = getchar();
    }
    if (ch == EOF)
        return NULL;
    else
    {
     
        st[i] = '\0';
        return st;
    }
}

5.第5题

#include 

char * strchrs(char * s, int c);

int main(void)
{
     
    char * sts;
    char st[] = "abcdefg\n\0";
    if (sts = strchrs(st, '\n'));
        *sts = 'A';
    puts(st);

    return 0;
}

char * strchrs(char * s, int c)
{
     

    while (*s)
    {
     
        if (*s == c)
            return s;
        else
            s++;
    }
    if (*s == c)
        return s;
    else
        return NULL;
}

6.第6题

#include 
#include 

#define SIZE 100

char * s_gets(char * st, int n);
int is_within(char * s, int c);

int main(void)
{
     
    char st[SIZE];
    int ch;

    puts("请输入字符串");
    while (s_gets(st, SIZE) && st[0] != '\0')
    {
     
        puts("请输入字符");
        ch = getchar();
        while (getchar() != '\n');

        if (is_within(st, ch))
            puts("存在");
        else
            puts("不存在");

        puts("请输入字符串");
    }
    return 0;
}

int is_within(char * s, int c)
{
     
    if (strchr(s, c))
        return 1;
    else
        return 0;
}

char * s_gets(char * st, int n)
{
     
    char * ret_val;
    char * find;

    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
     
        if (find = strchr(st, '\n'))
            *find = '\0';
        else
            while (getchar() != '\n');
    }
    return ret_val;
}

7.第7题

#include 
#include 

#define SIZE 10

char * mystrncpy(char * s1, char * s2, int n);
char * s_gets(char * st, int n);

int main(void)
{
     
    char st[SIZE];
    char sts[SIZE];
    int ch;

    puts("请输入字符串");
    while (s_gets(st, SIZE) && st[0] != '\0')
    {
     
        puts("请输入n");
        scanf("%d",&ch);
        while (getchar() !='\n');

        mystrncpy(sts, st, ch);
        puts(sts);

        puts("请输入字符串");
    }
    return 0;
}

char * mystrncpy(char * s1, char * s2, int n)
{
     
    int i;
    int len;
    len = strlen(s1);
    for ( i = 0; i < len; i++)
    {
     
        s1[i] = '\0';
    }

    for (i = 0; i < n-1; i++)
    {
     
        s1[i] = s2[i];
    }
    if (strlen(s2) < n)
        s1[i] = '\0';
    else
        s1[i] = s2[i];

    return s1;
}

char * s_gets(char * st, int n)
{
     
    char * ret_val;
    char * find;

    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
     
        if (find = strchr(st, '\n'))
            *find = '\0';
        else
            while (getchar() != '\n');
    }
    return ret_val;
}

8.第8题

#include 
#include 

char * string_in(const char * s1, const char * s2);

int main(void)
{
     
    char s1[100];
    char s2[100];

    printf("请输入s1:");
    while (scanf("%s",&s1) == 1)
    {
     
        printf("请输入s2:");
        scanf("%s",&s2);

        if (string_in(s1,s2) != NULL)
            puts("包含");
        else
            puts("不包含");

        printf("请输入s1:");
    }
    return 0;
}

char * string_in(const char * s1, const char * s2)
{
     
    int l2;
    int j = 1;
    int i;

    l2 = strlen(s2);
    i = strlen(s1) + 1 - l2;
    if (i > 0)
        while ((j = strncmp(s1,s2,l2)) && i--)
            s1++;
    if (j)
        return NULL;
    else
        return (char *)s1;
}

9.第9题

#include 
#include 

void fan(char *);
int main(void)
{
     
    char s1[100];

    printf("请输入s1:");
    while (scanf("%s",&s1) == 1)
    {
     
        fan(s1);
        puts(s1);

        printf("请输入s1:");
    }
    return 0;
}

void fan(char * s1)
{
     
    int i = 0;
    int j = strlen(s1) - 1;
    char s2[j+1];

    strcpy(s2, s1);

    while (j > -1)
    {
     
        s1[j--] = s2[i++];
    }
}

10.第10题

#include 
#include 
#define SIZE 10

void sc(char * s1);

int main(void)
{
     
    char s1[SIZE];
    char * find;

    printf("请输入s1:");
    while (fgets(s1, SIZE, stdin) != NULL)
    {
     
        if (find = strchr(s1, '\n'))
            *find = '\0';
        else
            while (getchar() != '\n');
        sc(s1);

        puts(s1);

        printf("请输入s1:");
    }
    return 0;
}

void sc(char * s1)
{
     
    int i, ii;
    int j = strlen(s1);

    for (i = 0; i < j; i++)
    {
     
        if (s1[i] == ' ')
        {
     
            for (ii = i; ii < j; ii++)
                s1[ii] = s1[ii+1];
            i--;
            j--;
        }
    }
}

11.第11题

#include 
#include 

#define SIZE 100
#define NUM 10

char * s_gets(char *st, int n);
void s1(char *st [], int num);
void s2(char *st [], int num);
void s3(char *st [], int num);
void s4(char *st [], int num);

int menu(void);

int main(void)
{
     
    char st[NUM][SIZE];
    char *sts[NUM];
    int ch;
    int num = 0;

    while (num < NUM)
    {
     
        printf("输入第%d字符串:",num+1);
        if (s_gets(st[num], SIZE) == NULL)
            break;
        sts[num] = st[num];
        num++;
    }
    putchar('\n');

    while ((ch = menu()) != 5)
    {
     
        switch (ch)
        {
     
            case 1: s1(sts, num);
                break;
            case 2: s2(sts, num);
                    s1(sts, num);
                break;
            case 3: s3(sts, num);
                    s1(sts, num);
                break;
            case 4: s4(sts, num);
                break;
        }
    }
    return 0;
}

char * s_gets(char *st, int n)
{
     
    char * ret_val;
    char * find;

    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
     
        if (find = strchr(st, '\n'))
            *find = '\n';
        else
            while (getchar() != '\n');
    }
    return ret_val;
}

int menu(void)
{
     
    int ch;
    puts("|=========================|");
    puts("|1) 打印源字符串列表      |");
    puts("|2) 以码表的顺序打印      |");
    puts("|3) 按长度递增顺序打印    |");
    puts("|4) 第一个单词的长度打印  |");
    puts("|5) 退出                  |");
    puts("|=========================|");
    puts("请输入<1-5>:");
    while (scanf("%d",&ch) != 1 || ch > 5 || ch < 1)
    {
     
        while (getchar() != '\n');
        puts("请输入<1-5>:");
    }
    return ch;
}

void s1(char *st[NUM], int num)
{
     
    int i;

    for (i = 0; i < num; i++)
        puts(st[i]);
    putchar('\n');
}

void s1(char *st[NUM], int num)
{
     
    int i;

    for (i = 0; i < num; i++)
        puts(st[i]);
    putchar('\n');
}

void s2(char *st [], int num)
{
     
    int i,j;
    char *temp;

    for (i = 0; i < num - 1; i++)
        for (j = i + 1; j < num; j++)
            if (strcmp(st[i], st[j]) > 0)
            {
     
                temp = st[i];
                st[i] = st[j];
                st[j] = temp;
            }
}

void s3(char *st [], int num)
{
     
    int i, j;
    char *temp;

    for (i = 0; i < num - 1; i++)
        for (j = i + 1; j < num; j++)
            if (strlen(st[i]) > strlen(st[j]))
            {
     
                temp = st[i];
                st[i] = st[j];
                st[j] = temp;
            }
}

void s4(char *st [], int num)
{
     
    int i, j, k;
    int sum;
    char *temp;

    for (i = 0; i < num; i++)
    {
     
        j = 0;
        sum = 0;

        while (st[i][j])
        {
     
            if (st[i][j] == ' ' || st[i][j] == '        ')
                j++;
            else
                break;
        }
        while (st[i][j])
        {
     
            if (st[i][j] == ' ' || st[i][j] == '        ')
                break;
            else
            {
     
                j++;
                sum++;
            }
        }
        for (k = 0; k < sum; k++)
            putchar(st[i][k]);
        putchar('\n');
    }
}

12.第12题

#include 
#include 

int main(void)
{
     
    int ch;
    int words = 0;
    int word = 0;
    int up = 0;  //isupper();
    int lo = 0;  //islower();
    int pu = 0;  //ispunct();
    int num = 0; //isdigit();

    while ((ch = getchar()) != EOF)
    {
     
        if (!isspace(ch) && !words)
        {
     
            words = 1;
            word++;
        }
        if (isupper(ch))
            up++;
        if (islower(ch))
            lo++;
        if (ispunct(ch))
            pu++;
        if (isdigit(ch))
            num++;
        if (isspace(ch) && words)
            words = 0;
    }

    printf("word = %d, up = %d\n", word, up);
    printf("lo = %d, pu = %d\n", lo, pu);
    printf("num = %d\n", num);

    return 0;
}

13.第13题

#include 

int main(int argc, char *argv [])
{
     
    int i, j;

    for (j = argc - 1, i = 1; i < argc; i++, j--)
    {
     
        printf("%s\n", argv[j]);
    }

    return 0;
}

14.第14题

#include 
#include 

int main(int argc, char *argv [])
{
     
    double d;
    double i;
    double ds;
    int j,k;

    if (argc != 3)
        puts("输入错误");
    else
    {
     
        d = atof(argv[1]);
        i = atof(argv[2]);
        k = atoi(argv[2]);
        ds = 1;

        for (j = 1; j <= k; j++)
        {
     
            ds = ds * d;
        }
        printf("%.2lf的%d次幂为:%.2lf\n", d, k, ds);
    }
    return 0;
}

15.第15题

#include 
#include 
#include 

#define SIZE 100

int atois(char *s1);

int main(void)
{
     
    char st[SIZE];
    char * find;

    while (fgets(st, SIZE, stdin) != NULL)
    {
     
        if (find = strchr(st, '\n'))
            *find = '\0';
        else
            while (getchar() != '\n');

        printf("%d\n",atois(st));
    }
    return 0;
}

int atois(char *s1)
{
     
    int len;
    int num;
    int i, j, k;

    len = strlen(s1);

    for (i = 0; i < len; i++)
        if (!isdigit(s1[i]))
            return 0;

    for (k = len - 1, i = 0, j = 1, num = 0; i < len; i++, k--)
    {
     
        num += (s1[k] - '0') * j;
        j *= 10;
    }

    return num;
}

16.第16题

#include 
#include 

#define SIZE 100

int main(int argc, char *argv [])
{
     
    char st[SIZE];
    char *find;
    int i = 0;

    while (fgets(st, SIZE, stdin) != NULL)
    {
     
        if (find = strchr(st, '\n'))
            *find = '\0';
        else
            while (getchar() != '\n');
    }

    if (argc != 2 || strcmp(argv[1], "-p") == 0)
        puts(st);

    else if (strcmp(argv[1], "-u") == 0)
    {
     
        while (st[i])
        {
     
            if (islower(st[i]))
                st[i] -= 32;
            i++;
        }
       puts(st);
    }
    else if (strcmp(argv[1], "-l") == 0)
    {
     
        while (st[i])
        {
     
            if (isupper(st[i]))
                st[i] += 32;
            i++;
        }
        puts(st);
    }
    return 0;
}

你可能感兴趣的:(C,Primer,Plus)