將一句話中的單詞進行倒換,標點符號不倒換,要求不使用輔助空間

比如一個字符串“this is a test.”,將其反轉後爲"test. a is this"。

#include <stdio.h>
#include <string.h>

void ReverseString(char str[],int start,int end)
{
	if (NULL == str || start > end)
	{
		return;
	}

	char c = ' ';
	while (start < end)
	{
		c = str[start];
		str[start] = str[end];
		str[end] = c;
		start++;
		end--;
	}
}

int main()
{
	char str[] = "this is a test.";
	int i = 0;
	int j = 0;
	int iLen = strlen(str);
	ReverseString(str,0,iLen-1);

	printf("%s\n",str);

	for (; i <= iLen; i++)
	{
		if (str[i] == ' ' || str[i] == '\0')
		{
			ReverseString(str,j,i-1);
			j = i+1;
		}
	}

	printf("%s\n",str);
}

發佈了20 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章