[C/C++]反转句子但不反转单词


#include <string.h>
#include <stdlib.h>

// 反转整个字符串
void _Reverse(char*s, int b, int e){
	while(b<e){
		char t = s[b];
		s[b] = s[e];
		s[e]= t;
		b++, e--;
	}
}
void ReverseSentence(char* s){
	int len = strlen(s);
	_Reverse(s, 0, len-1);
	int b=0, e=0;
	while(b<len){
		// 找到一个单词的起始和结束位置
		while(e<len&&s[e]!=' ')e++;

		// 对每个单词在反转一次
		_Reverse(s, b, e-1);
		e++;
		b = e;
	}
}


int main(int argc, char* argv[])
{
	char s[100]= "Test this sentence!";
	printf("%s\n",s);
	ReverseSentence(s);
	printf("%s\n",s);
	system("pause");
	return 0;
}


你可能感兴趣的:(反转句子)