您的位置:首页 > 其它

字符串反转 "you are beautiful" 转为 "beautiful are you"

2014-08-28 13:33 281 查看
//自己想的,从数学角度考虑每一个字符的位置,注意边界位置和空格的位置

void string_revert()
{
/*
8.27 2014
成功
要点: 数学 位置 精密 
最后第一位要特别处理
算法 strDes[position+i-start]=strSrc[i];
*/
char *strSrc="You are beautiful a girl Rocky afdafasf";

int len=strlen(strSrc);
char *strDes=(char*)malloc(sizeof(char)*(len+1));
int position,start,end,single_len,i;
start=end=0;
position=len;
cout<<"Source string: "<<strSrc<<endl;
cout<<len<<endl;

while(end<=len)
{
	if(' '==strSrc[end] || '\0'==strSrc[end]) //end 到了空格就进入 
	{ 	
		single_len=end-start;
		
		position=position-single_len;
		
		if('\0'==strSrc[end])
		{
		cout<<position<<endl;
		for(i=start;i<end;i++)
		{
		strDes[position+i-start]=strSrc[i];
		cout<<strSrc[i];
		}
		break;
		}
		cout<<" space"<<end<<endl;
		cout<<" position"<<position<<endl;
		cout<<" word len"<<single_len<<endl;
		for(i=start;i<end;i++)
		{
		strDes[position+i-start]=strSrc[i];
		cout<<strSrc[i];
		}
		position=position-1;
		strDes[position]=' '; //排头第一个排除
		
		start=end+1;
	}
	
	end++;
	
}
strDes[len]='\0';
cout<<"Destination String:"<<strDes<<endl;
cout<<"lens of new string "<<strlen(strDes)<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐