#include <stdio.h> int count(char str[]); int main(void) { char s[80]; int n; printf("请输入一个字符串:"); getchar (s); n = count (s); printf("字符串\"%s\"中共有大写字母%d个。\n",s,n); return 0; } int count (char str[]) { int i,c=0; for(i=0;str[i] != '\0';i++) if(str[i] >= 'A' && str[i] <= 'Z') c++; return c; }
时间: 2024-01-14 10:03:05 浏览: 23
这是一个计算字符串中大写字母个数的程序。用户需要输入一个字符串,然后程序会调用 `count` 函数来计算其中的大写字母个数,并将结果输出。
然而,程序中的 `getchar(s)` 函数调用是错误的,应该使用 `gets(s)` 或者 `fgets(s, 80, stdin)` 来获取用户输入的字符串。正确的代码应该像这样:
```
#include <stdio.h>
int count(char str[]);
int main(void) {
char s[80];
int n;
printf("请输入一个字符串:");
fgets(s, 80, stdin);
n = count(s);
printf("字符串\"%s\"中共有大写字母%d个。\n", s, n);
return 0;
}
int count(char str[]) {
int i, c = 0;
for(i = 0; str[i] != '\0'; i++)
if(str[i] >= 'A' && str[i] <= 'Z')
c++;
return c;
}
```
请注意,`gets` 函数是不安全的,因为它没有限制输入的大小,可能会导致缓冲区溢出。因此,使用 `fgets` 函数更为安全。
相关问题
#include <stdio.h> int fcap(char str[]); int main(void) { char str[80]; gets(str); printf("%d",fcap(str)); return 0; }C语言输入一个字符串,统计字符串中大写辅音字母个数并输出。大写辅音字母是除AE、IO、U以外的大写字母。
这段代码有误,函数 `fcap()` 的实现并未给出。以下是完整的代码实现。
```c
#include <stdio.h>
#include <string.h>
int count_fcap(char str[]);
int main(void) {
char str[80];
gets(str);
printf("%d", count_fcap(str));
return 0;
}
int count_fcap(char str[]) {
int i, count = 0;
for(i = 0; i < strlen(str); i++) {
if(str[i] >= 'A' && str[i] <= 'Z') { // 判断是否为大写字母
if(str[i] != 'A' && str[i] != 'E' && str[i] != 'I' && str[i] != 'O' && str[i] != 'U') { // 判断是否为AE、IO、U
count++;
}
}
}
return count;
}
```
#include <stdio.h> #include <stdlib.h> void PutRes(void); int fun(char *); int fun (char *p) 接着这个编写C语言:通过函数统计字符串中数字字符的个数,返回结果。在主函数中输入一串字符
代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
int countDigits(char*);
int main() {
char str[100];
printf("Enter a string: ");
gets(str);
int count = countDigits(str);
printf("The number of digits in the string is: %d\n", count);
return 0;
}
int countDigits(char *str) {
int count = 0;
while (*str != '\0') {
if (*str >= '0' && *str <= '9') {
count++;
}
str++;
}
return count;
}
```
注意:使用 `gets()` 函数时要小心,因为它没有限制输入的长度,可能会导致缓冲区溢出。更好的选择是使用 `fgets()` 函数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)