C语言编程(练习5:字符串与字符串函数)
来源:互联网 发布:mobiscroll.custom.js 编辑:程序博客网 时间:2024/05/12 09:43
题目:编写一个程序。功能是读取输入,直到遇到EOF,并报告单词数、大写字母数、小写字母数、标点符号数和数字字符数。使用ctype.h系列的函数
代码实现:
/**< 编写一个程序。功能是读取输入,直到遇到EOF,并报告单词数、大写字母数、小写字母数、标点符号数和数字字符数。使用ctype.h系列的函数 */#include <stdio.h>#include <stdlib.h>#include <ctype.h>#define MAX 100int main(){ int word = 0; int upper = 0; int lower = 0; int punct = 0; int digit = 0; int flag = 0; //进入单词标志位 int pflag = 0; char str[MAX]; char *pstr = str; printf("输入字符\n"); while((*pstr++ = getchar()) != EOF) continue; *pstr = '\0'; for(pstr=str; *pstr!='\0'; pstr++) { if(isupper(*pstr)) upper++; else if(islower(*pstr)) lower++; else if(ispunct(*pstr)) punct++; else if(isdigit(*pstr)) digit++; } for(pstr=str; *pstr!='\0'; pstr++) //判断字符串中的单词数 用两个标志位来判断单词 { if(isalpha(*pstr)) flag = 1; else { if(pflag==1) word++; flag = 0; } pflag = flag; } printf("word = %d\n", word); printf("upper = %d\n", upper); printf("lower = %d\n", lower); printf("punct = %d\n", punct); printf("digit = %d\n", digit); return 0;}运行结果:
0 0
- C语言编程(练习5:字符串与字符串函数)
- C语言编程(练习5:字符串与字符串函数)
- C语言编程(练习3:字符串与字符串函数)
- C语言编程(练习4:字符串与字符串函数)
- C语言编程(练习1:字符串和字符串函数)
- C语言编程(练习2:字符串与字符串数组)
- GPS数据处理——字符串编程练习(C语言)
- C语言 字符串与字符串函数
- C语言函数与字符串
- C语言整数转字符串(itoa()函数)小练习
- C语言字符串练习
- 字符串C语言练习
- C语言字符串函数与C++string的字符串函数
- C语言练习-指针(数组,字符串)
- 【编程语言】java---字符串练习
- C语言字符串练习1
- C语言字符串输入与输出函数
- c语言编程实例1(字符串与数组)
- 基于IMX6 RT3070的USB接口wifi模块移植
- 百分百财富管理软件V1.0
- opencv2.4.9+VS2013环境搭建
- Gauges2控件
- linux ssh工具 file 访问工具
- C语言编程(练习5:字符串与字符串函数)
- 关于记帐怎么坚持呢?
- 【c++笔记三】类型转换
- UVA Don't Get Rooked (DFS回溯)
- 【java】itoo项目实战之常见问题三
- hal testapp and file open and close, and ES and PES dump. HAL doc position. hal compile
- [LeetCode] 20.Valid Parentheses
- 4W家庭理财 V2.6
- linux 下面的 pdf 阅读工具