#include <iostream>
#include <cstring>
#include <cstdio>
#include <iomanip>
using namespace std;
namespace Myspace{
void reverse();
}
void Myspace::reverse()
{
int i = 0;
int j = 0;
int flag = 0;
int temp;
char str[1024] = "hello world";
while (str[flag])
{
i = flag;
while (str[i] == ' ')
i++;
j=i;
while (str[j]!=' ' &&str[j]!='\0')
j++;
flag = j;
j--;
while(j>i)
{
temp = str[j];
str[j] = str[i];
str[i] = temp;
j--;
i++;
}
}
for(int b =0;b<strlen(str);b++)
cout<<str[b];
cout<<endl;
}
using namespace Myspace;
int main()
{
reverse();
return 0;
}
输入字符串为“Hello World“,输出结果为“olleH dlroW“,并在主函数内测试该函数。
最新推荐文章于 2023-08-22 00:44:59 发布