翻转单词顺序(两次旋转)

//字符串翻转:I am a student. 变换为 student. a am I

void Reverse(char *pBegin, char *pEnd)
{
	if (pBegin == NULL || pEnd == NULL)
		return;
	while (pBegin < pEnd)
	{
		char temp = *pBegin;
		*pBegin = *pEnd;
		*pEnd = temp;
		pBegin++;
		pEnd--;
	}
}

char *ReverseSentence(char *pData)
{
	if (pData == NULL)
		return NULL;
	char *pBegin = pData;
	char *pEnd = pData;
	while (*pEnd != '\0')
		pEnd++;
	pEnd--;

	Reverse(pBegin, pEnd);
	pBegin = pEnd = pData;
	
	while (*pBegin != '\0')
	{
		if (*pBegin == ' ')
		{
			pBegin++;
			pEnd++;
		}
		else if (*pEnd == ' ' || *pEnd == '\0')
		{
			Reverse(pBegin, --pEnd);
			pBegin = ++pEnd;
		}
		else
			pEnd++;
	}
	return pData;
}


你可能感兴趣的:(翻转单词顺序(两次旋转))