#include <stdio.h>
#include <string.h>
char *reverse(char *str, int len) //字符串倒序
{
if (str == NULL)
{
return NULL;
}
char *pBegin = str;
char *pLast = str;
pLast = str + len - 1;
while (pBegin < pLast)
{
char temp;
temp = *pBegin;
*pBegin = *pLast;
*pLast = temp;
pBegin++;
pLast--;
}
return str;
}
char *ReverseSentence(char *str) //倒序句子
{
if (str == NULL)
{
return NULL;
}
reverse(str, strlen(str)); //逆转句子
char *pBegin = str;
char *pEnd = str;
while (*pEnd != '\0')
{
while (*pEnd != '\0' && *pEnd != ' ')
{
pEnd++;
}
reverse(pBegin, pEnd - pBegin); //逆转每个单词
if (*pEnd == '\0')
{
break;
}
pEnd++;
pBegin = pEnd;
}
return str;
}
int main()
{
char ch[] = "Hello World";
printf ("ch = %s\n", ch);
printf ("ch_now = %s\n", ReverseSentence(ch));
}
#include <string.h>
char *reverse(char *str, int len) //字符串倒序
{
if (str == NULL)
{
return NULL;
}
char *pBegin = str;
char *pLast = str;
pLast = str + len - 1;
while (pBegin < pLast)
{
char temp;
temp = *pBegin;
*pBegin = *pLast;
*pLast = temp;
pBegin++;
pLast--;
}
return str;
}
char *ReverseSentence(char *str) //倒序句子
{
if (str == NULL)
{
return NULL;
}
reverse(str, strlen(str)); //逆转句子
char *pBegin = str;
char *pEnd = str;
while (*pEnd != '\0')
{
while (*pEnd != '\0' && *pEnd != ' ')
{
pEnd++;
}
reverse(pBegin, pEnd - pBegin); //逆转每个单词
if (*pEnd == '\0')
{
break;
}
pEnd++;
pBegin = pEnd;
}
return str;
}
int main()
{
char ch[] = "Hello World";
printf ("ch = %s\n", ch);
printf ("ch_now = %s\n", ReverseSentence(ch));
}