c primer plus 第六版 第十一章编程练习

(编译环境 Microsoft Visual Studio 2019)

(命令行参数的题用MinGW的gcc编译)

1.

/*1.*/
#include<stdio.h>
#define LENGTH 10
void get_char(char* ar, char *end);
int main(void)
{
    char arr[LENGTH];
    char* p;

    printf("Please enter %d characters.\n", LENGTH);
    get_char(arr, arr + LENGTH);
    printf("\ncharacter array:\n");
    for (p = arr; p < arr + LENGTH; p++)
        putchar(*p);
    putchar('\n');

    return 0;
}
//函数返回后,函数内局部变量的内存会被释放,这时局部变量的地址会指向不可预料的内容
//返回局部变量的地址会在程序运行时出现未知错误
void get_char(char* ar, char *end)
{
    for (; ar < end; ar++)
        * ar = getchar();
}

2.

/*2.*/
#include<stdio.h>
#include<ctype.h>
#define LENGTH 10
int get_char(char* ar, char* end);
int main(void)
{
    char arr[LENGTH];
    char* p;
    int leng;

    printf("Please enter some text.\n");
    leng = get_char(arr, arr + LENGTH);
    printf("\ncharacter array:\n");
    for (p = arr; p < arr + leng; p++)
        putchar(*p);
    putchar('\n');

    return 0;
}

int get_char(char* ar, char* end)
{
    int length = 0;

    for (; ar < end; ar++, length++)
    {
        *ar = getchar();
        if (isspace(*ar))
            break;
    }

    return length;
}

3.

/*3.*/
#include<stdio.h>
#include<ctype.h>
#define LENGTH 20
int get_word(char* ar);
int main(void)
{
    char arr[LENGTH];
    char* p;
    int leng;

    printf("Please enter some text.\n");
    leng = get_word(arr);
    printf("\nWord:\n");
    for (p = arr; p < arr + leng; p++)
        putchar(*p);
    putchar('\n');

    return 0;
}

int get_word(char* ar)
{
    int length = 0;
    _Bool inword = 0;

    while (*ar=getchar())
    {
        if (isspace(*ar) && !inword)
            continue;
        else if (!isspace(*ar) && !inword)
            inword = 1;
        else if (isspace(*ar) && inword)
            break;
        length++;
        ar++;
    }

    return length;
}

4.

/*4.*/
#include<stdio.h>
#include<ctype.h>
#define LENGTH 10
int get_word(char* ar, int n);
int main(void)
{
    char arr[LENGTH];
    char* p;
    int leng;

    printf("Please enter some text."
        "(Less than %d characters per word)\n",LENGTH);
    leng = get_word(arr, LENGTH);
    printf("\nWord:\n");
    for (p = arr; p < arr + leng; p++)
        putchar(*p);
    putchar('\n');

    return 0;
}

int get_word(char* ar, int n)
{
    char* p = ar;
    int length = 0;
    _Bool inword = 0;

    while (p < ar + n)
    {
        *p = getchar();
        if (isspace(*p) && !inword)
            continue;
        else if (!isspace(*p) && !inword)
            inword = 1;
        else if (isspace(*p) && inword)
            break;
        length++;
        p++;
    }

    return length;
}

5.

/*5.*/
#include<stdio.h>
#define LENGTH 20
char* s_gets(char* st, int n);
char* search(char* st, char ch);
int main(void)
{
    char string[LENGTH];
    char tar;
    char* p_tar;

    printf("Please enter some text.(less than %d character)\n",LENGTH);
    if (s_gets(string, LENGTH))
    {
        printf("Your String: ");
        puts(string);
        printf("\nPlease enter the target character.(empty line to quit)\n");
        while ((tar = getchar()) != '\n')
        {
            p_tar = search(string, tar);
            if (p_tar)
                printf("The rest of string: \"%s\"", p_tar);
            else
                printf("No such character.");
            printf("\nEnter another character.(empty line to quit)\n");
            while (getchar() != '\n')
                continue;
        }
    }
    else
        printf("error\n");
    printf("bye\n");

    return 0;
}

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

    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        while (*st != '\n' && *st != '\0')
            st++;
        if (*st == '\n')
            * st = '\0';
        else
            while (getchar() != '\n')
                continue;
    }

    return ret_val;
}

char* search(char* st, char ch)
{
    char* target;

    while (*st != '\0' && *st != ch)
        st++;
    if (*st == ch)
        target = st;
    else
        target = NULL;

    return target;
}

6.

/*6.*/
#include<stdio.h>
#define LENGTH 20
char* s_gets(char* st, int n);
int is_within(char ch, char* ar);
int main(void)
{
    char string[LENGTH];
    char target;

    printf("Please enter some text.(less than %d character)\n", LENGTH);
    if (s_gets(string, LENGTH))
    {
        printf("Your input: ");
        puts(string);
        printf("\nPlease enter target character.\n");
        while ((target = getchar()) != '\n')
        {
            if (is_within(target, string))
                printf("Character in this string.\n");
            else
                printf("Not found.\n");
            printf("Enter another character.\n");
            while (getchar() != '\n')
                continue;
        }
    }
    else
        printf("error\n");
    printf("bye\n");

    return 0;
}

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

    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        while (*st != '\n' && *st != '\0')
            st++;
        if (*st == '\n')
            * st = '\0';
        else
            while (getchar() != '\n')
                continue;
    }

    return ret_val;
}

int is_within(char ch, char* ar)
{
    while (*ar != '\0')
    {
        if (*ar == ch)
            return 1;
        else
            ar++;
    }

    return 0;
}

7.

/*7.*/
#include<stdio.h>
#define S_LENG 30
#define T_LENG 10
char* mystrncpy(char* s1, char* s2, int n);
char* s_gets(char* st, int n);
int main(void)
{
    char source[S_LENG];
    char target[T_LENG];
    char* tar;

    printf("Please enter some text.(empty line to quit)\n");
    while (s_gets(source, S_LENG) && *source != '\0')
    {
        tar = mystrncpy(target, source, T_LENG - 1);
        tar[T_LENG - 1] = '\0';
        printf("Target: \n");
        puts(tar);
        printf("\nEnter other text.(empty line to quit)\n");
    }
    printf("bye\n");

    return 0;
}

char* mystrncpy(char* s1, char* s2, int n)
{
    int count = 0;
    char* ret_val = s1;

    while (count < n)
    {
        *s1 = *s2;
        if (*s2 == '\0')
            break;
        s2++;
        s1++;
        count++;
    }
    return ret_val;
}

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

    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        while (*st != '\n' && *st != '\0')
            st++;
        if (*st == '\n')
            * st = '\0';
        else
            while (getchar() != '\n')
                continue;
    }

    return ret_val;
}

8.

/*8.*/
#include<stdio.h>
#define F_LENG 30
#define S_LENG 30
char* s_gets(char* st, int n);
char* string_in(char* fst, char* sec);
int main(void)
{
    char first[F_LENG];
    char second[S_LENG];
    char* ret;

    printf("Please enter the first string.\n");
    if (s_gets(first, F_LENG))
    {
        printf("Your input: \"%s\"\n", first);
        printf("\nPlease enter the second string.(empty line to quit)\n");
        while (s_gets(second, S_LENG) && *second != '\0')
        {
            ret = string_in(first, second);
            if (ret)
            {
                printf("The rest:\n");
                puts(ret);
            }
            else
                printf("No such string,\n");
            printf("\nEnter other string.(empty line to quit)\n");
        }
    }
    printf("bye\n");

    return 0;
}

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

    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        while (*st != '\n' && *st != '\0')
            st++;
        if (*st == '\n')
            * st = '\0';
        else
            while (getchar() != '\n')
                continue;
    }

    return ret_val;
}

char* string_in(char* fst, char* sec)
{
    char* ret_val = NULL;

    while (*fst != '\0')
    {
        if (*fst == *sec)
            ret_val = fst;
        while (*fst == *sec)
        {
            fst++;
            sec++;
            if (*sec == '\0')
                return ret_val;
        }
        fst++;
    }

    return NULL;
}

9.

/*9.*/
#include<stdio.h>
#include<string.h>
#define LENGTH 20
char* s_gets(char* st, int n);
void re_order(char* st);
int main(void)
{
    char string[LENGTH];

    printf("Please enter some text(empty line to quit).\n");
    while (s_gets(string, LENGTH) && *string != '\0')
    {
        printf("Your input: \"%s\"\n", string);
        re_order(string);
        printf("Reverse Order:\n");
        puts(string);
        printf("\nEnter other string(empty line to quit).\n");
    }
    printf("bye\n");

    return 0;
}

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

    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        while (*st != '\n' && *st != '\0')
            st++;
        if (*st == '\n')
            * st = '\0';
        else
            while (getchar() != '\n')
                continue;
    }

    return ret_val;
}

void re_order(char* st)
{
    int i;
    int n;
    int temp;

    n = strlen(st);
    for (i = 0; i < n / 2; i++)
    {
        temp = st[i];
        st[i] = st[n - i - 1];
        st[n - i - 1] = temp;
    }
}

10.

/*10.*/
#include<stdio.h>
#define LENGTH 20
char* s_gets(char* st, int n);
void d_blank(char* st);
int main(void)
{
    char string[LENGTH];

    printf("Please enter some text(empty line to quit).\n");
    while (s_gets(string,LENGTH) && *string != '\0')
    {
        printf("Original String: \n\"%s\"\n", string);
        d_blank(string);
        printf("Remove Space: \n\"%s\"\n", string);
        printf("\nEnter other string(empty line to quit).\n");
    }
    printf("bye\n");

    return 0;
}

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

    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        while (*st != '\n' && *st != '\0')
            st++;
        if (*st == '\n')
            * st = '\0';
        else
            while (getchar() != '\n')
                continue;
    }

    return ret_val;
}

void d_blank(char* st)
{
    char* p;

    while (*st != '\0')
    {
        p = st;
        if (*p == ' ')
        {
            while (*p != '\0')
            {
                *p = *(p + 1);
                p++;
            }
        }
        st++;
    }
}

11.

/*11.*/
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define ROWS 10
#define COLUMNS 30
int get_list(char st[][COLUMNS], char* str[ROWS], int n);
char* s_gets(char* st, int n);
char menu(void);
char get_first(void);
void put_list(char st[][COLUMNS], int n);
void a_order(char *st[ROWS], int n);
void l_order(char *st[ROWS], int n);
void fl_order(char *st[ROWS], int n);
int fw_length(char* st);
int main(void)
{
    char string[ROWS][COLUMNS];
    char* str[ROWS];
    char ch;
    int row;

    printf("Please enter %d strings:\n", ROWS);
    if (row = get_list(string, str, ROWS))
    {
        while ((ch = menu()) != 'q')
        {
            printf("Print List:\n");
            switch (ch)
            {
            case 'a': put_list(string, row); break;
            case 'b': a_order(str, row); break;
            case 'c': l_order(str, row); break;
            case 'd': fl_order(str, row); break;
            default: printf("Program Error.\n"); break;
            }
        }
    }
    printf("bye\n");

    return 0;
}

int get_list(char st[][COLUMNS],char *str[ROWS], int n)
{
    char (*p)[COLUMNS];
    int count = 0;

    for (p = st; p < st + n; p++)
    {
        if (s_gets(*p, COLUMNS))
        {
            str[count] = *p;
            count++;
        }
        else
            break;
    }

    return count;
}

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

    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        while (*st != '\n' && *st != '\0')
            st++;
        if (*st == '\n')
            * st = '\0';
        else
            while (getchar() != '\n')
                continue;
    }

    return ret_val;
}

char menu(void)
{
    char ch;

    puts("***********************************************");
    printf("\nMenu:\n");
    printf("a. Print source string list\n"
        "b. Print strings in ASCII order\n"
        "c. Print string by length\n"
        "d. Print a string by the length of the first "
        "word in the string\n"
        "q. Quit.\n");
    printf("Please enter your choice: ");
    ch = get_first();
    while ((ch > 'd' || ch < 'a' )&& ch != 'q')
    {
        printf("Please enter correct choice: ");
        ch = get_first();
    }
    printf("\n");

    return ch;
}

char get_first(void)
{
    char ch;

    ch = getchar();
    if (ch != '\n')
    {
        while (getchar() != '\n')
            continue;
    }
    return ch;
}

void put_list(char st[][COLUMNS], int n)
{
    int i;

    for (i = 0; i < n; i++, st++)
        puts(*st);
}

void a_order(char *st[ROWS], int n)
{
    char* temp;
    int i, j;

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

void l_order(char* st[ROWS], int n)
{
    char* temp;
    int i, j;

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

void fl_order(char* st[ROWS], int n)
{
    int i, j;
    char* temp;

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

int fw_length(char* st)
{
    int count = 0;
    _Bool inword = 0;

    while (*st != '\0')
    {
        if (!isspace(*st) && !inword)
        {
            inword = 1;
            count++;
        }
        else if (!isspace(*st) && inword)
            count++;
        else if (isspace(*st) && inword)
            break;
        st++;
    }

    return count;
}

12.

/*12.*/
#include<stdio.h>
#include<ctype.h>
#define LENGTH 50
int main(void)
{
    char ch;
    _Bool inword = 0;
    int words = 0;
    int upper = 0;
    int lower = 0;
    int digit = 0;
    int punct = 0;

    printf("Please enter some text(end with EOF).\n");
    while ((ch=getchar())!=EOF)
    {
        if (isdigit(ch))
            digit++;
        else if (ispunct(ch))
            punct++;
        else if (islower(ch))
            lower++;
        else if (isupper(ch))
            upper++;
        if (isalpha(ch) && !inword)
        {
            words++;
            inword = 1;
        }
        else if (!isalpha(ch) && inword)
            inword = 0;
    }
    printf("\nNumber of words: %d\n", words);
    printf("Uppercase letters: %d\n", upper);
    printf("Lowercase letters: %d\n", lower);
    printf("Punctuation number: %d\n", punct);
    printf("Number of digits: %d\n", digit);

    return 0;
}

13.

/*13.*/
#include<stdio.h>
int main(int argc, char* argv[])
{
    char** st;
    for (st = argv + argc -1; st > argv; st--)
        printf("%s ",*st);
    putchar('\n');

    return 0;
}

14.

/*14.*/
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
    double result = 1.0;
    double base;
    int pow;

    if(argc==3)
    {
        base=atof(argv[1]);
        pow=atoi(argv[2]);
        for (; pow > 0; pow--)
            result *= base;
        printf("Result= %f\n", result);
    }
    else
        printf("The parameter is incorrect.\n");

    return 0;
}

15.

/*15.*/
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#define LENGTH 10
int atoi(char* st);
int pow_i(int base, int n);
int main(void)
{
    char integer[LENGTH];
    int value;

    printf("Please enter an integer.\n");
    scanf_s("%s", integer, 10);
    value = atoi(integer);
    printf("\nInteger:\n%d\n", value);

    return 0;
}

int atoi(char* st)
{
    int value = 0;
    int len;
    int i = 0;

    len = strlen(st);
    while (*st != '\0')
    {
        if (!isdigit(*st))
        {
            value=0;
            break;
        }
        else
        {
            value += (*st - '0') * pow_i(10, len - 1 - i);
        }
        st++;
        i++;
    }

    return value;
}

int pow_i(int base, int n)
{
    int pow = 1;

    for (; n > 0; n--)
        pow *= base;
    return pow;
}

16.

/*16.
这里用DOS重定向将words.txt文件与stdin流相关联,把words.txt中的内容导入test程序
*/
#include<stdio.h>
#include<ctype.h>
#include<string.h>
void print(void);
void to_up(void);
void to_low(void);
int main(int argc, char* argv[])
{
    if (argc > 2)
        printf("The parameter is incorrect.\n");
    else if (argc == 1 || !strcmp(argv[1], "-p"))
        print();
    else if (!strcmp(argv[1], "-u"))
        to_up();
    else if (!strcmp(argv[1], "-l"))
        to_low();
    putchar('\n');

    return 0;
}

void print(void)
{
    char ch;

    while((ch=getchar())!=EOF)
        putchar(ch);
}

void to_up(void)
{
    char ch;

    while ((ch = getchar()) != EOF)
    {
        ch = toupper(ch);
        putchar(ch);
    }
}

void to_low(void)
{
    char ch;

    while ((ch = getchar()) != EOF)
    {
        ch = tolower(ch);
        putchar(ch);
    }
}

  • 13
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值