当我使用这段代码,输入CEAedca时,希望它输出的结果是CedcEAa,我应该如何去修改这段代码#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"); }
时间: 2024-03-12 21:46:01 浏览: 77
要修改这段代码,使得输入CEAedca时输出CedcEAa,可以将fun函数中的交换操作修改为只交换两个字符,而不是整个子串,具体的修改如下:
```
void fun(char *a, int len) {
int i = 1, j = len - 2;
while (i < j) {
if (a[i] > a[j]) {
char t = a[j];
for (int k = j; k > i; k--) {
a[k] = a[k - 1];
}
a[i] = t;
}
i++;
j--;
}
}
```
这样,就能够满足要求,输出CedcEAa。
完整的代码如下所示:
```
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>
void wwjt();
void fun(char *a, int len) {
int i = 1, j = len - 2;
while (i < j) {
if (a[i] > a[j]) {
char t = a[j];
for (int k = j; k > i; k--) {
a[k] = a[k - 1];
}
a[i] = t;
}
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");
}
```
阅读全文
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)