编辑代码

/* mod_str.c -- 修改字符串 */
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define LIMIT 81

void ToUpper(char *);
int PunctCount(const char *);

int main(void)
{
    char line[LIMIT];
    char * find;

    puts("Please enter a line:");
    fgets(line, LIMIT, stdin);//输入,到line字符数组,长度limit,stdin标准输入

    find = strchr(line, '\n'); // 查找换行符
    if (find) // 如果地址不是 NULL,
        *find = '\0'; // 用空字符替换

    ToUpper(line);//修改字符串
    puts(line);//输出

    printf("That line has %d punctuation characters.\n",
    PunctCount(line));//统计标点符号个数。
    return 0;
}


void ToUpper(char * str)
{
    while (*str)//*str的值为0(空字符的编码值为0),空字符跳出循环
    {
        *str = toupper(*str);//转大写
        str++;//字符指针跳到下一个字符
    }
}
int PunctCount(const char * str)
{
    int ct = 0;
    while (*str)
    {
        if (ispunct(*str))//统计字符串中的标点符号个数。
            ct++;
        str++;
    }
    return ct;
}