19,469
社区成员
发帖
与我相关
我的任务
分享
typedef struct ST_REVERSE
{
ST_REVERSE* pPre;
char *psWord;
int nCount;
}*PST_REVERSE;
int _tmain(int argc, _TCHAR* argv[])
{
int nIndex = 0;
int nCharacterInWordCount = 0;
char *a = "Why is that our corporation won`t give us half a day off on May 4th!!";
char *pszFinal;
PST_REVERSE pstWord = NULL;
PST_REVERSE pstWordPre = NULL;
//将原字符串中每个单词首字母的指针和单词中的字母数放入单向链表
while (1)
{
if (*a == ' ' || *a == '\0')
{
pstWord = new ST_REVERSE;
pstWord->pPre = pstWordPre;
//首个单词前没有空格
if (pstWord->pPre != NULL)
{
--nCharacterInWordCount;
}
pstWord->psWord = a - nCharacterInWordCount;
pstWord->nCount = nCharacterInWordCount;
pstWordPre = pstWord;
nCharacterInWordCount = 0;
}
if (*a == '\0')
{
break;
}
++nIndex;
++nCharacterInWordCount;
++a;
}
//从单向链表的最后一项开始往前,将前一个单词拷入新字符串中
pszFinal = new char[nIndex + 1];
nIndex = 0;
while (1)
{
for (int i = 0; i != pstWord->nCount; ++i)
{
pszFinal[nIndex] = pstWord->psWord[i];
++nIndex;
}
pszFinal[nIndex] = ' ';
++nIndex;
pstWord = pstWord->pPre;
if (pstWord == NULL)
{
pszFinal[nIndex] = '\0';
break;
}
}
printf("%s\n", pszFinal);
return 0;
}