输入一个英文句子,对句子中的单词逆序。其中"-"作为单词连接符,"--"作为单词分隔符
比如,I am a student, 逆序之后为student a am I。
-
#include <stdio.h>
-
#include <stdlib.h>
-
-
int reverse(char* strOut, const char* strIn)
-
{
-
if (NULL == strOut || NULL == strIn)
-
{
-
return -1;
-
}
-
-
const char* pTraIn = strIn;
-
char strTmp[40] = {'\0'};
-
char* pTmp = strTmp;
-
-
while (*pTraIn != '\0')
-
{
-
pTraIn++;
-
}
-
pTraIn--;
-
-
while (pTraIn != strIn - 1)
-
{
-
if ((*pTraIn >= 'a' && *pTraIn <= 'z') ||
-
(*pTraIn >= 'A' && *pTraIn <= 'Z') ||
-
(*pTraIn == '-' && ((*(pTraIn+1) >= 'a' && *(pTraIn+1) <= 'z') ||
-
(*(pTraIn+1) >= 'A' && *(pTraIn+1) <= 'Z')
-
)
-
&& ((*(pTraIn-1) >= 'a' && *(pTraIn-1) <= 'z') ||
-
(*(pTraIn-1) >= 'A' && *(pTraIn-1) <= 'Z')
-
)
-
) ||
-
('\'' == *pTraIn)
-
)
-
{
-
*pTmp++ = *pTraIn--;
-
}
-
else
-
{
-
if (pTmp != strTmp)
-
{
-
pTmp--;
-
while (pTmp != strTmp)
-
{
-
*strOut++ = *pTmp--;
-
}
-
*strOut++ = *pTmp;
-
*strOut++ = ' ';
-
}
-
pTraIn--;
-
}
-
}
-
if (pTmp != strTmp)
-
{
-
pTmp--;
-
while (pTmp != strTmp)
-
{
-
*strOut++ = *pTmp--;
-
}
-
*strOut++ = *pTmp;
-
}
-
*strOut = '\0';
-
-
return 0;
-
}
-
-
int main()
-
{
-
char str1[] = "I am a student";
-
char str2[] = "he-llo, may--be I'm . a student";
-
char str3[] = ". hello, i am a student.. *";
-
char strRet[200];
-
-
reverse(strRet, str1);
-
puts(strRet);
-
reverse(strRet, str2);
-
puts(strRet);
-
reverse(strRet, str3);
-
puts(strRet);
-
-
return 0;
-
}
阅读(571) | 评论(0) | 转发(0) |