#include <stdio.h> void fun(char *s) { int i, j; for (i=j=0; s[i]!='\0'; i++) if (s[i] != 'c') /********found********/ s[j] = s[i]; /********found********/ s[i] = '/0'; } void main() { char s[80]; gets(s); fun(s); puts(s); }
时间: 2023-07-23 11:38:46 浏览: 36
这段代码的功能是将字符串中所有的字符 'c' 删除,并输出修改后的字符串。但是代码有两处错误:
1. 第一个错误是在第 10 行,字符串结束符应为 '\0' 而不是 '/0'。
2. 第二个错误是在第 7 行,当字符不是 'c' 时,应该将该字符拷贝到新的字符串中,即应该是 s[j] = s[i] 而不是 s[j] = 'c'。
正确的代码应该是:
```c
#include <stdio.h>
void fun(char *s) {
int i, j;
for (i = j = 0; s[i] != '\0'; i++) {
if (s[i] != 'c') {
s[j] = s[i];
j++;
}
}
s[j] = '\0';
}
int main() {
char s[80];
gets(s);
fun(s);
puts(s);
return 0;
}
```
相关问题
请使用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 <stdio.h> #include <string.h> void fun(char *s,int num) { } void main() { char s[10]; gets(s); fun(s,7); printf("%s", s); }
这是一道需要填空的题目,题目思路是将字符串 `s` 中的前 `num` 个字符反转。可以使用两个指针分别指向字符串的首位和末位,依次交换两个指针所指向的字符,直到两个指针相遇为止。具体实现可以这样写:
```c
#include <stdio.h>
#include <string.h>
void fun(char *s, int num) {
int i = 0, j = num - 1;
while (i < j) {
char temp = s[i];
s[i] = s[j];
s[j] = temp;
i++;
j--;
}
}
void main() {
char s[10];
gets(s);
fun(s, 7);
printf("%s", s);
}
```
其中,`i` 和 `j` 分别表示指向字符串的首位和末位的指针,`temp` 是一个临时变量,用于交换两个指针所指向的字符。在循环中,每次将指针向中间移动一位,并交换两个指针所指向的字符,直到两个指针相遇为止。最终输出交换后的字符串即可。
相关推荐
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)