ji1812062521 于 2016.02.20 23:56 提问

C++小程序，调试，在我这个基础上调试 3C

using namespace std;
void swap(char a[], char b[], int n) {
char ch; char k=0;
for (int k = 0; k < n; k++)
ch = a[k]; a[k] = b[k]; b[k] = ch;
}
int main(void)
{
char a[100], b[100];
cin >> a >> b;
cout << "a=" << a << ",b=" << b << '\n';
int c = strlen(a), d = strlen(b);
if (c > d) swap(a, b, d);
cout << "a=" << a << ",b=" << b << '\n';

2个回答

caozhy      2016.02.21 00:17
`````` #include<iostream>
using namespace std;
void swap(char a[], char b[], int n, int m) {

char ch; int k;
for (k = 0; k < m; k++) //这里少括号，并且复制不全
{
ch = a[k]; a[k] = b[k]; b[k] = ch;
}
b[m] = '\0';
}
int main(void)
{
char a[100], b[100];
cin >> a >> b;
cout << "a=" << a << ",b=" << b << '\n';
int c = strlen(a), d = strlen(b);
if (c > d) swap(a, b, d, c);
cout << "a=" << a << ",b=" << b << '\n';
return 0;
}
``````
caozhy      2016.02.21 00:17

123
abcdef
a=123,b=abcdef
a=123,b=abcdef
Press any key to continue

abcdef
123
a=abcdef,b=123
a=123,b=abcdef
Press any key to continue

caozhy 回复ji1812062521: 没有啊b[m] = '\0';这个你写了吧，

ji1812062521 你先输入长字符串，再输入短字符串〒_〒为什么结果会输出莫名其妙的东西