c语言练习字符数组作为函数参数: 1编写函数UCount统计字符串s中的大写字母个数并作为函数的返回值 2编写函数LCount统计字符串s中的小写字母个数并作为函数的返回值 3编写函数WordCount统计字符串s中的单词个数并作为函数的返回值 4编写函数Reverse将字符串s逆序。 5编写函数main输入字符串s输出s中的大写字母、小写字母和单词的个数以及s的逆序。
代码如下:
#include <stdio.h> #include <string.h> #include <ctype.h>
int UCount(char s[]) { int count = 0; for (int i = 0; i < strlen(s); i++) { if (isupper(s[i])) { count++; } } return count; }
int LCount(char s[]) { int count = 0; for (int i = 0; i < strlen(s); i++) { if (islower(s[i])) { count++; } } return count; }
int WordCount(char s[]) { int count = 0; int flag = 0; for (int i = 0; i < strlen(s); i++) { if (isalpha(s[i])) { if (flag == 0) { count++; flag = 1; } } else { flag = 0; } } return count; }
void Reverse(char s[]) { int len = strlen(s); for (int i = 0; i < len / 2; i++) { char temp = s[i]; s[i] = s[len - 1 - i]; s[len - 1 - i] = temp; } }
int main() { char s[100]; printf("请输入字符串:"); scanf("%s", s); printf("大写字母个数:%d\n", UCount(s)); printf("小写字母个数:%d\n", LCount(s)); printf("单词个数:%d\n", WordCount(s)); printf("逆序字符串:%s\n", s); Reverse(s); printf("逆序字符串:%s\n", s); return 0;
原文地址: https://www.cveoy.top/t/topic/epR8 著作权归作者所有。请勿转载和采集!