例如如下:
I am from nanjing. 变成
I am morf gnijnan.
源代码如下:
#include <iostream>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int is_split(char *c) {
if ((*c == '!')||(*c == '?') || (*c == '.') || (*c == ',') || (*c == ' ')) {
return 1;
}
return 0;
}
void _rever_str(char *low, char *high) {
char *plow= low, *phigh = high;
while (plow < phigh ) {
*plow = *plow^*phigh;
*phigh = *plow^*phigh;
*plow = *plow^*phigh;
plow++;
phigh--;
}
}
void rever_str(char *str) {
char* temp = NULL;
char* pstart = str, *pcur = str,*pnext = pcur +1;
if (NULL == str) { return ;}
while ((*pcur != '\0')&&(*pnext != '\0'))
{
if (pstart == NULL) {
pstart = pnext;
}
if (is_split(pnext)) {
if ((pcur - pstart) >= 1) {
_rever_str(pstart,pcur);
}
pstart = NULL;
}
pcur++;
pnext++;
}
return;
}
int main(int argc, char**argv){
char str1[] = {"I am from nanjing."};
char str2[] = {"I ma morf gnijnan."};
cout<<str1<<endl;
rever_str(str1);
cout<<str1<<endl;
cout<<str2<<endl;
assert(!strcmp(str2,str1));
return 0;
}
#include <iostream>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int is_split(char *c) {
if ((*c == '!')||(*c == '?') || (*c == '.') || (*c == ',') || (*c == ' ')) {
return 1;
}
return 0;
}
void _rever_str(char *low, char *high) {
char *plow= low, *phigh = high;
while (plow < phigh ) {
*plow = *plow^*phigh;
*phigh = *plow^*phigh;
*plow = *plow^*phigh;
plow++;
phigh--;
}
}
void rever_str(char *str) {
char* temp = NULL;
char* pstart = str, *pcur = str,*pnext = pcur +1;
if (NULL == str) { return ;}
while ((*pcur != '\0')&&(*pnext != '\0'))
{
if (pstart == NULL) {
pstart = pnext;
}
if (is_split(pnext)) {
if ((pcur - pstart) >= 1) {
_rever_str(pstart,pcur);
}
pstart = NULL;
}
pcur++;
pnext++;
}
return;
}
int main(int argc, char**argv){
char str1[] = {"I am from nanjing."};
char str2[] = {"I ma morf gnijnan."};
cout<<str1<<endl;
rever_str(str1);
cout<<str1<<endl;
cout<<str2<<endl;
assert(!strcmp(str2,str1));
return 0;
}