将字符串中的单词倒置

前端之家收集整理的这篇文章主要介绍了将字符串中的单词倒置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

例如:

输入:i came from china.

输出:china. from came i

while (j>i)
	{
		temp=str[j];
		str[j]=str[i];
		str[i]=temp;
		j--;
		i++;
	}
	cout<<str<<endl;//字符串逆转了
	i=0;
	while(str[i]!='\0')
		{
			if(str[i]!=' ')
			{
				begin=i;//begin 是第一个单词开始的地方
				while (str[i]&&str[i]!=' ')
				i++;
				i--;
				end=i;//end是第一个单词结束的地方
			}

			while(end>begin)
			{
				temp=str[begin];
				str[begin]=str[end];
				str[end]=temp;
				end--;
				begin++;
			}
			i++;
		}
原文链接:https://www.f2er.com/javaschema/285328.html

猜你在找的设计模式相关文章