字符串翻转

/* 将一句话里面的单词进行倒置,标点符号不倒换*/

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

void RevStr(char *str)
{
	printf("原字符串:\n%s\n", str);

	int i = 0, j = 0, temp, begin, end;
	j = strlen(str) - 1;
	while(j > i)
	{
		 temp = str[i];
		 str[i] = str[j];
		 str[j] = temp;
		 i++;
		 j--;
	}
	printf("全部翻转后:\n%s\n", str);  

	//第二步进行部分翻转,不是空格就翻转
	i = 0;
	while(str[i])
	{
		if(str[i] != ' ')
		{
			begin = i;
			while(str[i] && str[i] != ' ')
			{
				i++;
			}
			i = i-1;  //回到空格前一个字符
			end = i;
		}
		while(end > begin)
		{
			temp = str[begin];
			str[begin] = str[end];
			str[end] = temp;
			end--;
			begin++;
		}
		i++;
	}

	printf("部分翻转后:\n%s\n", str);
}

int main()
{
	char p[] = "I come from Bengbu.";
	RevStr(p);
	return 0;
}


 

你可能感兴趣的:(字符串翻转)