将"i am a student"转换成" student a am i"

整体思路:1.先将整个字符串逆序

                  2.再将整个单词逆序

void Reverse_word(char *left,char *right)
{
	assert(left);
	assert(right);
	while (left < right)
	{
		char tmp = *left;
		*left = *right;
		*right = tmp;
		left++;
		right--;
	}
}
int my_strlen(char *str)
{
	assert(str);
	int count = 0;
	while (*str)
	{
		count++;
		str++;
	}
	return count;
}
void Reverse_str( char *str)
{
	char*tmp = NULL;
	char *left = str;
	char *right = str + my_strlen(str) - 1;
	Reverse_word(left, right);//逆序整个字符串
	while (*str)
	{   
		tmp = str;
		while ((*str != ' ') &&(*str != '\0'))
		{
			str++;
		}
		char *end = str - 1;
		Reverse_word(tmp, end);
		if (*str == ' ')
		{
			str++;
		}
	}


}
#include
int main()
{
	char arr[] = "i am a student";
	Reverse_str(arr);
	printf("%s", arr);
	system("pause");
	return 0;
}

将

你可能感兴趣的:(每日一练)