// WordReverse.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdlib.h>
#include <string.h>
int main(int argc, char* argv[])
{
char inWords[]="I love China";
printf("%s\n", inWords);
//char* newWords=(char*)malloc(sizeof(inWords)+1);
char* curPoint, *recoPoint;
recoPoint=inWords;
int length=strlen(inWords);
char temp;
int before=0,last=length-1;
//sentence reverse
while (before<last)
{
temp=*(inWords+before);
*(inWords+before)=*(inWords+last);
*(inWords+last)=temp;
before++;
last--;
}
printf("%s\n", inWords);
//word reverse
for(int i=0; i<length; i++)
{
if (inWords[i+1]==' ')
{
curPoint=inWords+i;
while (recoPoint<curPoint)
{
//reverse
temp=*(recoPoint);
*(recoPoint)=*(curPoint);
*(curPoint)=temp;
recoPoint++;
curPoint--;
}
recoPoint=inWords+i+2; //the beginning of next word
}
}
printf("%s", inWords);
system("pause");
}
思想:先反转句子,再反转单词。
单词反转问题
最新推荐文章于 2020-11-02 15:01:33 发布