以下程序运行后,输出结果是 #include<stdio.h> int main(void) { char ch[2][5]={"693","825"},*p[2]; int i,j,s=0; for(i=0;i<2;i++) p[i]=ch[i]; for(i=0;i<2;i++) for(j=0;p[i][j]>='0'&&p[i][j]<='9';j+=2) s=10*s+(p[i][j]-'0'); printf("%d\n",s); return 0; } A.693825 B.22 C.6385 D.33
时间: 2024-03-18 21:45:14 浏览: 760
答案是 B.22。程序首先定义了一个二维字符数组 ch,然后将其每一行的地址分别存储到一个指针数组 p 中。接下来,程序使用双重循环遍历 p 数组中的每个字符,如果该字符是数字,则将其转换为整数并加入到变量 s 中。由于 ch 数组中只包含数字字符,因此循环体中的条件 p[i][j]>='0'&&p[i][j]<='9' 永远为真,并且 j 的增量为 2,因此循环体内每次处理两个字符。由于 ch 数组中的字符顺序是 "693825",因此最终 s 的值为 10*6+9+10*3+8=66+22=88。最后,程序将 s 的值输出到屏幕上。
相关问题
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`函数,并打印出读取到的单词。
#include<stdio.h> #include<string.h> void main() { char str[10]="Ch\nina"; printf("%d",strlen(str)); }
这段代码的作用是输出字符串 "Ch\nina" 的长度,即 6。但是可以看出,在数组定义时,只分配了 10 个字符的空间,而 "Ch\nina" 长度为 7,因此存在字符串溢出的问题。建议将数组大小定义为至少 8 个字符。同时,main 函数的返回值应该为 int 而不是 void。修改后的代码如下所示:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[8] = "Ch\nina";
printf("%d", strlen(str));
return 0;
}
```