#include <stdio.h> #include <ctype.h> void fun(char *s) { /********found********/ while (*s *= '@') { if (*s>='A'&&*s<='Z' || *s>='a'&&*s<='z') { if (*s == 'Z') *s = 'A'; else if(*s == 'z') *s = 'a'; else *s += 1; } /********found********/ *(s++); } } void main() { char s[80]; gets(s); fun(s); puts(s); }
时间: 2023-07-19 07:55:56 浏览: 126
这段代码的功能是将输入的字符串中的字母全部转换成下一个字母,并将其他字符保持不变,然后输出转换后的字符串。
具体实现方式是通过指针遍历字符串中的每一个字符,如果该字符是字母,则将其转换成下一个字母,如果是大写字母中的 Z 或小写字母中的 z,则将其转换成 A 或 a,其他字符保持不变。最后输出转换后的字符串。
需要注意的是,原始代码中的代码行 `*(s++);` 应该改为 `(*s++);`,否则会产生编译错误。
相关问题
请使用while函数将fun函数的内容补充完整,编写出一个C语言程序,对字符串,除首、尾字符外,将其字符按ASCII编码数值降序排列的操作:#include<stdio.h> #include<ctype.h> #include<string.h> #include<conio.h> void wwjt(); void fun(char *a,int len) { int i,j; char t; } main(){ char s[100]; int l; printf("输入字符串:"); gets(s); l=strlen(s); fun(s,l); printf("\n%s\n",s); wwjt(); }
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>
void wwjt();
void fun(char *a, int len) {
int i, j;
char t;
i = 1;
j = len - 2; //除去首尾字符后的最后一个字符的下标
while (i < j) {
while (i < j && a[i] > a[j]) {
t = a[i];
a[i] = a[j];
a[j] = t;
i++;
j--;
}
i++;
j--;
}
}
int main() {
char s[100];
int l;
printf("输入字符串:");
gets(s);
l = strlen(s);
fun(s, l);
printf("\n%s\n", s);
wwjt();
return 0;
}
void wwjt() {
printf("我爱计算机网络课程设计!\n");
}
#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;
}
```