//你的代码没什么逻辑问题,只是书写规范有错:看我注释
#include
#include
#define N 15
int Exchange(char *a, char x, char y)
{
int i = 0, j;
bool isFind = false;
while (a[i] != '\0') //这里是\0,不是/0
{
if (a[i] == x)
{
a[i] = y;
isFind = true;
}
i++;
}
return isFind;
}
void main()
{
int i;
char x, y, a[N];
gets(a);
scanf("%c,%c", &x, &y); //%c和%c之前有逗号,隔开
if( Exchange(a, x, y) ){
puts(a);
}
else
printf("没有被替换\n");
system("pause");
}
#include
int Exchange(char *a, char x, char y)
{
char ch;
int i, j;
for (j=i=0; ; ++i,++a)
{
ch = *a;
if (0==ch) break;
if (ch!=x) continue;
*a = y;
j = 1;
}
return j;
}
void main()
{
char x, y, str[1000];
printf("请输入一个字符串:");
gets(str);
printf("请输入待查找字符和替换字符:");
scanf("%c%c", &x, &y);
if ( Exchange(str, x, y))
printf("替换成功。替换后的字符串为:\n%s\n",str);
else
printf("没有在输入字符串内发现字符%c.\n", x);
}
#include
#include
#define N 15
int Exchange(char *in, char x, char y, char *out)
{
int i = 0, changed = 0;
while (in[i] != '\0')
{
if (in[i] == x)
{
out[i] = y;
changed = 1;
}
else
out[i] = in[i];
i++;
}
return changed;
}
void main()
{
int i;
char x, y, a[N], b[N];
memset(a, 0, N);
memset(b, 0, N);
printf("string:\n");
gets(a);//取出字符串,并且把换行符取出来,但不存入a
printf("two char:\n");
scanf("%c %c", &x, &y);
printf("[%s] [%c] [%c]\n", a, x, y);
i = Exchange(a, x, y, b);
printf("i: %d, b:%s\n", i, b);
system("pause");
}
这样就解决了。
输入的时候要注意换行符的处理。请采纳,谢谢
while (a[i] != '/0') --》 while (a[i] != '\0')