将一个句子中的单词逆序



例如如下:

I am from nanjing. 变成
I am morf gnijnan.

源代码如下:

#include
#include
#include
#include
#include

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< rever_str(str1);
cout< cout< assert(!strcmp(str2,str1));
return 0;
}

#include
#include
#include
#include
#include

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<     rever_str(str1);
    cout<     cout<     assert(!strcmp(str2,str1));
    return 0;
}

你可能感兴趣的:(将一个句子中的单词逆序)