s[i] = (char) getchar();
时间: 2024-05-24 16:10:43 浏览: 16
This line of code reads a single character from the standard input and assigns it to the ith index of the character array s.
The function getchar() reads a single character from the standard input and returns its ASCII value as an integer. The (char) before the function call casts the integer value to a character before assigning it to the array.
This line of code is often used in a loop to read multiple characters from the input and store them in an array.
相关问题
源程序(有错误的程序)如下: #include <stdio.h> #include <string.h> int main(void) { int i, s; char str[80]; i=0; while((str[i]=getchar())!='\n') i++; str[i]='\0'; for(i=0; i<80; i++) if(str[i]<='0' || str[i]>='9') s=s*10+str[i]; printf("%d", s); return 0; }
该程序有一个明显的错误,即在for循环中使用了未初始化的变量s。因此,程序会产生不可预知的结果或崩溃。在使用变量s之前,应该将其初始化为0,如下所示:
```
#include <stdio.h>
#include <string.h>
int main(void)
{
int i, s = 0; // 初始化变量s为0
char str[80];
i = 0;
while ((str[i] = getchar()) != '\n')
i++;
str[i] = '\0';
for (i = 0; i < 80; i++)
if (str[i] >= '0' && str[i] <= '9') // 判断字符是否为数字
s = s * 10 + (str[i] - '0'); // 将字符转换为数字并累加到s
printf("%d", s);
return 0;
}
```
修改后的程序可以正确地将输入的字符串转换为数字并输出。
#include <string.h> #include <stdio.h> #include <ctype.h> void fun(char *p) { int i, t; char c[80]; /********found********/ For (i=0, t=0; p[i]; i++) if (!isspace(*(p+i))) c[t++] = p[i]; /********found********/ c[t] = "\0"; strcpy(p, c); } void main() { char c, s[80]; int i = 0; c = getchar(); while (c != '#') { s[i] = c; i++; c = getchar(); } s[i] = '\0'; fun(s); puts(s); }
这段代码的功能是将输入的字符串中的空格删除,并输出删除后的字符串。但是代码有两处错误:
1. 第一个错误是在第 10 行,`For` 应该改为小写字母 `for`。
2. 第二个错误是在第 13 行,字符串结束符应为 `'\0'` 而不是 `"\0"`。
正确的代码应该是:
```c
#include <string.h>
#include <stdio.h>
#include <ctype.h>
void fun(char *p) {
int i, t;
char c[80];
for (i = 0, t = 0; p[i]; i++) {
if (!isspace(*(p+i))) {
c[t++] = p[i];
}
}
c[t] = '\0';
strcpy(p, c);
}
int main() {
char c, s[80];
int i = 0;
c = getchar();
while (c != '#') {
s[i] = c;
i++;
c = getchar();
}
s[i] = '\0';
fun(s);
puts(s);
return 0;
}
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)