题目:字符串反转,如将字符串 "www.runoob.com" 反转为 "moc.boonur.www"。
程序分析:无。
代码1:
#include<iostream>
using namespace std;
int main()
{
string str;
cin >> str;
int n = str.length();
for (int i = n; i >= 0; i--)
{
cout << str[i];
}
return 0;
}
代码2:指针交换
#include<iostream>
#include<string>
using namespace std;
void Reverse(char *str)
{
int len;
char temp;
char* begin, * end;
len = strlen(str);
begin = str;
end = str + len - 1;
while (begin <= end)
{
temp = *begin;
*begin = *end;
*end = temp;
begin++;
end--;
}
}
int main()
{
char string[] = "www.runoob.com";
Reverse(string);
puts(string);
return 0;
}