在不用第三方参数的情况下,交换两个参数的值
//在不用第三方参数的情况下,交换两个参数的值
#include <stdio.h>
#include <iostream>
using namespace std;
int main(void)
{
int a = 50;
int b = 60;
cout << "a: " << a << " b: " << b << endl;
a = a + b; //a=110
b = a - b; //b = 110 - 60 = 50
a = a - b; //a = 110 - 50 = 60
cout <<"swap..... "<<endl;
cout << "a: " << a << " b:" << b << endl;
system("pause");
return 0;
}
//a: 50 b: 60
//swap.....
//a: 60 b:50
//请按任意键继续. . .
输入一个字符串,将其逆序后输出。(使用C++)
//输入一个字符串,将其逆序后输出。(使用C++,不建议用伪码)
#include <stdio.h>
#include <iostream>
using namespace std;
char *reverse_str(char *src)
{
char str[1024];
cout << sizeof(str) << endl;
memset(str, 0, sizeof(str));
strcpy(str,src);
int i = 0, j = 0;
char c_temp = '0';
//abcde
//01234
for (i = 0, j = strlen(str) - 1; i < strlen(str)/2; i++, j--)
{
c_temp = str[i];
str[i] = str[j];
str[j] = c_temp;
}
return str;
}
int main(void)
{
char string[1024];
cin.getline(string,1024,'d'); //输入的字符串遇到字符d,结束。
//cin.getline(string,1024);
cout << string << endl;
cout << reverse_str(string) << endl;
system("pause");
return 0;
}
/*
aabbccddee
aabbcc
1024
ccbbaa
请按任意键继续. . .
*/