反转句子

编写程序将句子反转, 例如将nice to meet you 反转成为 you meet to nice

void reverse(char *p, char *q){
	while(p < q){
		char tmp = *p;
		*p++ = *q;
		*q-- = tmp;
	}
}

char* reverseSentence(char *s){
	char *p = s; //指向单词的首字符
	char *q = s; //指向空格或者'\0'
	while(*q){
		if(*q == ' '){
			reverse(p, q-1);
			q++;
			p = q;
		}
		else
			q++;
	}
	reverse(p, q-1);
	reverse(s, q-1);
	return s;
}


展开阅读全文

没有更多推荐了,返回首页