#include<stdio.h> int main(void) { char ch; int n,m,j; while((ch = getchar() != '#') { if( ch == ' ') { n++; } else if(ch == '\n') { m++; } else j++; } printf("%d,%d,%d",n,m,j); return 0; }
时间: 2024-02-10 10:30:18 浏览: 40
你的代码是一个简单的统计输入字符中空格、换行和其他字符数量的程序。其中,输入字符通过 `getchar()` 逐个读入,当读到 `#` 字符时程序停止。
具体来说,程序定义了三个变量 `n`、`m` 和 `j`,分别用于记录空格、换行和其他字符的数量。然后程序进入一个 `while` 循环,不断读入输入字符并进行判断。如果读入的字符是空格,则将 `n` 的值加 1;如果是换行符,则将 `m` 的值加 1;否则将 `j` 的值加 1。最后程序输出 `n`、`m` 和 `j` 的值,用逗号分隔。
需要注意的是,程序中的括号不完整,应该将 `getchar()` 和 `!=` 之间的括号补上。
相关问题
#include<stdio.h> int main(void) { char ch; while((ch=getchar ()) != '#');{ printf("%c",ch); printf("%d",ch); } return 0; }有什么问题吗
这段代码的问题在于花括号的位置。while循环的花括号应该放在读取字符的语句后面,而不是在读取字符的语句后面。因为当前的花括号会导致while循环变成一个无限循环,因为它没有终止条件。正确的代码如下所示:
```
#include <stdio.h>
int main(void) {
char ch;
while ((ch = getchar()) != '#') {
printf("%c", ch);
printf("%d", ch);
}
return 0;
}
```
另外,代码中应该加上注释,以便更好地理解代码的逻辑和目的。
C++编制一个读单词过程,以#include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <string.h> #define NULL 0为开头
C++编写一个读取单词的过程可以以下代码:
```cpp
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define NULL 0
void readWord(char* word) {
int index = 0;
char ch;
// 跳过空白字符
while ((ch = getchar()) != EOF && isspace(ch));
// 读取单词字符
while (ch != EOF && !isspace(ch)) {
word[index++] = ch;
ch = getchar();
}
// 添加字符串结束符
word[index] = '\0';
}
int main() {
char word[100];
readWord(word);
printf("读取的单词是:%s\n", word);
return 0;
}
```
上述代码中,`readWord`函数用于读取一个单词,它会跳过输入中的空白字符,然后读取连续的非空白字符作为一个单词。`main`函数中调用了`readWord`函数,并打印出读取到的单词。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)