6、以下程序的输出结果为。 #include "stdio.h" int fun(char *p) { char *s=p; while (*s!= E' )s++; return s-p; } int main(void) { int i: char all=" ABCDEFG" ¡=fun(a); printf("%2d" i); return 0; A
时间: 2024-03-11 16:48:23 浏览: 36
这个程序存在一些语法错误,也无法通过编译。其中:
1. 变量 `i` 没有声明类型,应该改为 `int i;`。
2. 字符串 `a` 没有加上双引号,应该改为 `char a[] = "ABCDEFG";`。
3. 函数 `fun` 中的字符串结束符应该是单引号,而不是大写字母 E,应该改为 `'\0'`。
在修正这些错误之后,程序的输出结果应该是 7,表示字符串 "ABCDEFG" 的长度(不包括字符串结束符)。
正确的代码如下:
```c
#include <stdio.h>
int fun(char *p) {
char *s = p;
while (*s != '\0') s++;
return s - p;
}
int main(void) {
int i;
char a[] = "ABCDEFG";
i = fun(a);
printf("%2d", i);
return 0;
}
```
相关问题
#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()` 函数。
#include <stdio.h> #include <string.h> void fun(char *w,int n) { char t,*s1,*s2; sl=w;s2=w+n-1 while(s]<s2) {t=*sl++:*s1=*s2- *s2=t; int main() { char a[10]="1234567"; char *p=a; fun(p,strlen(P)); puts(p); return 0;
这是一段 C 语言的代码。该代码定义了一个函数 `fun`,该函数的作用是将字符串翻转。具体来说,该函数的输入参数为一个字符串指针 `w` 和字符串的长度 `n`,函数会将字符串中的字符顺序翻转。该函数内部定义了三个变量,分别是字符变量 `t` 和字符指针变量 `s1` 和 `s2`。接着是一个 while 循环,该循环的条件是 `s1 < s2`,循环内部将 `s1` 和 `s2` 所指向的字符交换,直到 `s1` 和 `s2` 相遇。最后,该代码定义了一个 `main` 函数,该函数声明了一个字符串数组 `a` 并初始化,然后调用 `fun` 函数翻转字符串,并输出翻转后的结果。
需要注意的是,该代码有一些语法错误。第一处是在 `fun` 函数内部,`sl` 变量应该是 `s1`;第二处是在 `while` 循环内部,应该使用 `++s1` 和 `--s2` 分别使 `s1` 和 `s2` 指向下一个字符。此外,`main` 函数中的 `strlen(P)` 应该是 `strlen(p)`。修改后的代码如下:
```
#include <stdio.h>
#include <string.h>
void fun(char *w, int n) {
char t, *s1, *s2;
s1 = w;
s2 = w + n - 1;
while (s1 < s2) {
t = *s1;
*s1 = *s2;
*s2 = t;
++s1;
--s2;
}
}
int main() {
char a[10] = "1234567";
char *p = a;
fun(p, strlen(p));
puts(p);
return 0;
}
```
该程序的输出结果为 `7654321`,即将原字符串 `1234567` 翻转后的结果。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.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_column_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)