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, '\n');
17     if(find)
18         *find ='\0';
19     ToUpper(line);
20     puts(line);
21     printf("That line has %d punctuation characters.\n",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 }

猜你喜欢

转载自www.cnblogs.com/grooovvve/p/9938774.html
今日推荐