#include <stdio.h> int main() { char str[100]; int count = 0; int i=0; while((str[i]=getchar())!='\n'){ i=i+1; } str[i]='\n'; for (int i = 0; str[i] != '\n'; i++) { if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') { count++; } } printf("%d", count); return 0; }不改变while的情况下的优化
时间: 2023-10-25 11:09:56 浏览: 70
可以使用 fgets() 函数代替 while 循环读入字符串,同时可以使用一个变量记录字符串的长度,避免每次循环都要判断是否为 '\n'。另外,在计算元音字母数量时,可以使用 switch 语句代替多个 if 语句,使代码更加简洁。优化后的代码如下:
```
#include <stdio.h>
int main() {
char str[100];
int count = 0;
int len = 0;
fgets(str, 100, stdin);
while (str[len] != '\n') {
len++;
}
for (int i = 0; i < len; i++) {
switch (str[i]) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
count++;
break;
default:
break;
}
}
printf("%d", count);
return 0;
}
```
相关问题
#include<stdio.h> #include<string.h> int main() { FILE *fp; fp=fopen("in.txt","r"); if(fp==NULL) { perror("failed"); return 1; } char str[81]; int i=0; while(fgets(str,81,fp)!=NULL) { i++; fgets(str,81,fp); } printf("%d",i); fclose(fp); return 0; }为什么这样写不对
这个程序的问题在于 `fgets()` 函数调用了两次,在循环中,每次读取文件都应该调用 `fgets()` 函数一次,否则会出现漏读或重复读的情况。因此,正确的做法是将第二个 `fgets()` 函数调用去掉即可。
以下是修改后的程序:
```c
#include <stdio.h>
#include <string.h>
int main() {
FILE *fp;
fp = fopen("in.txt", "r");
if (fp == NULL) {
perror("failed");
return 1;
}
char str[81]; // 多开一个字符用于存储 '\0'
int count = 0;
while (fgets(str, 81, fp) != NULL) {
count++;
}
printf("%d\n", count);
fclose(fp);
return 0;
}
```
这个程序只调用了一次 `fgets()` 函数,每次循环都只读取一行,并将行数计数器加一。
#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)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)