C Prime Plus编程练习(第十一章)

1. 设计并测试一个函数,从输入中获取下n个字符(包括空白、制表符、换行符),把结果储存在一个数组里,它的地址被传递作为一个参数

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

#define STRLEN 20

int main(void) {
    char words[STRLEN];

    int spaceNum = 0, tabNum = 0, lineNum = 0, charactorNum = 0;
    puts("Enter strings (empty line to quit):");
    while(fgets(words, STRLEN, stdin) != NULL && words[0] != '\n') {
        fputs(words, stdout);
        int index = 0;
        for(index = 0; index < strlen(words); index++) {
            if (' ' == words[index]) {
                spaceNum++;
            } else if ('\t' == words[index]) {
                tabNum++;
            } else if ('\n' == words[index]) {
                lineNum++;
            } else if (words[index] >= 'A' && words[index] <= 'z') {
                charactorNum++;
            }
        }
    }
    printf("\nspace : %d, tab : %d, newline : %d, charactors : %d\n", spaceNum, tabNum, lineNum, charactorNum);
    return 0;
}


2. 修改并编程练习1的函数,在n个字符后停止,或在读到第1个空白、制表符或换行符时停止,哪个先遇到哪个停止。不能只使用scanf()

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

#define STRLEN 20

int main(void) {
    char words[STRLEN];

    puts("Enter strings (empty line to quit):");
    int index = 0;
    char ch;
    while((ch = getchar()) != EOF) {
        if (' ' == ch) {
            words[index] = '\0';
            break;
        } else if ('\t' == ch) {
            words[index] = '\0';
            break;
        } else if ('\n' == ch) {
            words[index] = '\0';
            break;
        }
        if(index == STRLEN - 1) {
            words[index] = '\0';
            break;
        }
        words[index++] = ch;
    }
    puts(words);
    return 0;
}


3. 设计并测试一个函数,从一行输入中吧一个单词读入一个数组中,并丢弃输入行中的其余字符。该函数应该跳过第一个非空白字符前面的所有空白。将一个单词定义为没有空白、制表符或换行符的字符序列

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

#define STRLEN 20

int is_space(char);
int is_tabs(char);
int is_line_break(char);
void print_s();

int main(void) {
    print_s();
    return 0;
}

void print_s() {
    char words[STRLEN];
    puts("Enter strings (empty line to quit):");
    int index = 0;
    char ch;
    while (' ' == (ch = getchar())) {
        continue;
    }
    words[index] = ch;
    for (index = 1; index < STRLEN - 1; index++) {
        words[index] = getchar();
        if(is_space(words[index]) || is_tabs(words[index]) || is_line_break(words[index])) {
            words[index] = '\0';
            break;
        }
    }
    if(index == STRLEN - 1) {
        words[index] = '\0';
    }
    puts(words);
}

int is_space(char ch) {
    if(' ' == ch) {
        return 1;
    }
    return 0;
}

int is_tabs(char ch) {
    if('\t' == ch) {
        return 1;
    }
    return 0;
}

int is_line_break(char ch) {
    if('\n' == ch) {
        return 1;
    }
    return 0;
}


4. 设计并测试一个函数,它类似编程练习3的描述,只不过它接受第2个参数知名可读取的最大字符数。

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

#define STRLEN 20
#define INPUTLEN 10

int is_space(char);
int is_tabs(char);
int is_line_break(char);
void print_s(int);

int main(void) {
    print_s(INPUTLEN);
    return 0;
}

void print_s(int max) {
    char words[STRLEN];
    int index = 0;
    while(is_space(words[index] = getchar())) {
        continue;
    }
    int max_len = (max > STRLEN) ? STRLEN : max;
    for(index = 1;index < max_len - 1; index++) {
        words[index] = getchar();
        if(is_space(words[index]) || is_tabs(words[index]) || is_line_break(words[index])) {
            words[index] = '\0';
            break;
        }
    }
    if (index == max_len - 1) {
        words[index] = '\0';
    }
    puts(words);
}

int is_space(char ch) {
    if(' ' == ch) {
        return 1;
    }
    return 0;
}

int is_tabs(char ch) {
    if('\t' == ch) {
        return 1;
    }
    return 0;
}

int is_line_break(char ch) {
    if('\n' == ch) {
        return 1;
    }
    return 0;
}


5. 设计并测试一个函数,搜索第1个函数形参制定的字符串,在其中查找第2个函数形参制定的字符首次出现的位置。如果成功,该函数返回指向该字符的指针,如果在字符串中未找到指定字符,则返回空指针(该函数的功能与strchr()函数相同)。在一个完整的程序中测试该函数,使用一个循环给函数提供输入值。

#include <stdio.h>

#define STRLEN 20

char* search_ch(char*, char);

int main(void) {
    char words[STRLEN];
    char ch;
    int index = 0;
    puts("Please Input A String (Exit For Line Break Input):");
    while((words[index] = getchar()) != '\n' && index < STRLEN - 1) {
        index++;
    }
    words[index] = '\0';
    puts(words);
    while(getchar() != '\n') {
        continue;
    }
    pu
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值