• C语言ctype.h字符函数和字符串


    ctype.h存的是与字符相关的函数;

    这些函数虽然不能处理整个字符串,但是可以处理字符串中的字符;

    ToUpper()函数,利用toupper()函数处理字符串中的每个字符,转换成大写;

    PunctCount()函数,利用ispunct()统计字符串中的标点符号个数;

    使用strchr()处理fgets()读入字符串的换行符;这样处理没有把缓冲区的剩余字符清空,所以仅适合只有一条输入语句的情况。s_gets()适合处理多条输入语句的情况

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <ctype.h>
     4 #define LIMIT 81
     5 
     6 void ToUpper(char *);
     7 int PunctCount(const char *);
     8 
     9 int main(void)
    10 {
    11     char line[LIMIT];
    12     char * find;
    13 
    14     puts("Please enter a line:");
    15     fgets(line,LIMIT,stdin);
    16     find = strchr(line, '
    ');
    17     if(find)
    18         *find ='';
    19     ToUpper(line);
    20     puts(line);
    21     printf("That line has %d punctuation characters.
    ",PunctCount(line));
    22 
    23     return 0;
    24 }
    25 
    26 void ToUpper(char * str)
    27 {
    28     while(*str)
    29     {
    30         *str =toupper(*str);
    31         str++;
    32     }
    33 }
    34 
    35 int PunctCount(const char * str)
    36 {
    37     int ct =0;
    38     while(*str)
    39     {
    40         if(ispunct(*str))
    41             ct++;
    42         str++;
    43     }
    44 
    45     return ct;
    46 }
  • 相关阅读:
    redis-LinkedList
    Jedis(java操作redis数据库技术)
    jquery判断表单内容是否为空
    jQuery单选框的回显
    使用jQuery重置(reset)表单的方法
    BootstrapValidator 解决多属性被同时校验问题
    模态框被遮罩层遮挡
    python 高阶函数
    python 函数式编程
    python 生成器
  • 原文地址:https://www.cnblogs.com/grooovvve/p/9938774.html
Copyright © 2020-2023  润新知