#include <stdio.h> /********found********/ char fun(char *s, char *t) { int s1 = 0, t1 = 0; char *ss, *tt; ss = s; tt = t; while (*ss) { s1++; /********found********/ (*ss)++; } while (*tt) { t1++; /********found********/ (*tt)++; } if (t1 > s1) return t; else return s; } void main() { char a[80], b[80]; gets(a); gets(b); printf("%s\n", fun(a, b)); }
时间: 2023-07-18 13:41:20 浏览: 179
这段代码的功能是接受两个字符串作为参数,并返回长度更长的那个字符串。同时,它还将每个字符串中的每个字符的ASCII码增加1。具体实现见下:
```c
#include <stdio.h>
char fun(char *s, char *t) {
int s1 = 0, t1 = 0;
char *ss, *tt;
ss = s;
tt = t;
while (*ss) {
s1++;
(*ss)++; // 将s中的每个字符的ASCII码增加1
}
while (*tt) {
t1++;
(*tt)++; // 将t中的每个字符的ASCII码增加1
}
if (t1 > s1)
return *t; // 如果t长度更长,则返回t
else
return *s; // 如果s长度更长或两者长度相等,则返回s
}
int main() {
char a[80], b[80];
gets(a);
gets(b);
printf("%c\n", fun(a, b)); // 返回的是字符,用%c输出即可
return 0;
}
```
相关问题
#include <stdio.h> #include <string.h> char *fun(char tt[]) { int i; for (i=0; tt[i]; i++) /********found********/ if ((tt[i] >= 'a') || (tt[i]<='z')) /********found********/ tt[i] += 32; return (tt); } void main() { char tt[81]; gets(tt); printf("\n%s", fun(tt)); }
这段代码的功能是将输入的字符串中所有的大写字母转换成小写字母,并返回修改后的字符串。但是代码中存在两处错误:
1. 逻辑运算符应该是 `&&` 而不是 `||`,因为 `||` 表示或者的关系,应该是大于等于 'a' 且小于等于 'z' 才需要执行 `tt[i] += 32` 的操作。
2. 函数 fun 的返回值类型应该是 char*,即返回一个指向字符数组的指针。
下面是修改后的代码:
```c
#include <stdio.h>
#include <string.h>
char* fun(char tt[]) {
int i;
for (i = 0; tt[i]; i++)
if ((tt[i] >= 'A') && (tt[i] <= 'Z'))
tt[i] += 32;
return tt;
}
void main() {
char tt[81];
gets(tt);
printf("\n%s", fun(tt));
}
```
#include <stdio.h> /********found********/ fun(char a) { if (*a) { fun(a+1); /********found********/ printf("%c". *a); } } void main() { char s[10]; scanf("%s",s); fun(s); printf("\n"); }
这段代码的功能是将输入的字符串倒序输出。但是代码中存在两处错误:
1. 函数 fun 的返回值类型应该是 void,即无返回值。
2. printf 函数中应该使用逗号 `,` 分隔两个参数,而不是点号 `.`。
下面是修改后的代码:
```c
#include <stdio.h>
void fun(char *a) {
if (*a) {
fun(a+1);
printf("%c", *a);
}
}
void main() {
char s[10];
scanf("%s", s);
fun(s);
printf("\n");
}
```