天天看點

【字元串操作】13.将一句話單詞進行倒置

/**将一句話的單詞進行倒置,标點符号不倒換。
*比如:“I am a boy.”,輸出“boy. a am I”。
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define SIZE 100
void func(char* input)
{
	int i;
	char temp;
	int begin,end;
	int n=strlen(input);
	//第一次進行倒序輸出
	for(i=0;i<n/2;i++)
	{
		temp=input[i];
		input[i]=input[n-i-1];
		input[n-i-1]=temp;
	}
	printf("Reverse string is:%s\n",input);
	i=0;
	while(input[i]!='\0')
	{

		//單詞内部調整順序
		if(input[i]!=' ')
		{
			begin=i;
			while(input[i]!=' ' && input[i]!='\0')
				i++;
			i=i-1;
			end=i;
			while(begin<end)
			{
				temp=input[begin];
				input[begin]=input[end];
				input[end]=temp;
				end--;
				begin++;
			}
		}
		i++;//繼續循環
	}
}
int main(void)
{
	char input[SIZE];
	printf("Please enter a string:");
	gets(input);
	func(input);
	printf("The result string is:%s\n",input);
	system("pause");//如果不加這一句的話,運作exe檔案的時候隻會一閃而過!!!
	return 0;	
}