原题:
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Example 1:
Input: "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc"字符反转
代码如下:
char* reverseWords(char* s) {
void reverse(char *s,int front,int last);
int len=strlen(s);
int s1=0;
int s2=0;
for(int n=0;n<len;n++)
{
if(*(s+n)==' ')
{
s2=n;
reverse(s,s1,s2-1);
s1=s2+1;
}
}
s2=len-1;
reverse(s,s1,s2);
return s;
}
void reverse(char *s,int front,int last)
{
char *temp;
for(int n=0;front+n<last-n;n++)
{
temp=*(s+front+n);
*(s+front+n)=*(s+last-n);
*(s+last-n)=temp;
}
}