题解 | #倒置字符串#

倒置字符串

https://www.nowcoder.com/practice/ee5de2e7c45a46a090c1ced2fdc62355

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

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

int main()
{	
	char str[101] = { 0 };
	gets(str);
	int len = strlen(str) - 1;
	reverse(str, str+len);
	char* start = str;
	while (*start)
	{
		char* end = start;
		while (*end != ' ' && *end != '\0')
		{
			end++;
		}
		reverse(start, end - 1);
		if (*end == ' ')
		{
			start = end+1;
		}
		else
		{
			start = end;
		}
	}
	printf("%s\n", str);
	return 0;
}

全部评论

相关推荐

喜欢走神的孤勇者练习时长两年半:爱华,信华,等华,黑华
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务