您的位置:首页 > 编程语言

编程之美的2.17,数组循环移位 & 字符串逆转(反转) Hello world Welcome => Welcome world Hello

2013-12-17 12:28 387 查看
代码如下:(类似于编程之美的2.17,数组循环移位)



staticvoidMain(string[]args)
{

stringinput="HelloWorldWelcome";
char[]tempArray=input.ToCharArray();

stringresult=RightShift(tempArray,0,tempArray.Length-1);
}

publicstaticstringRightShift(char[]arrary,intstartIndex,intendIndex)
{
Reverse(arrary,0,endIndex);

startIndex=endIndex=0;

while(startIndex<arrary.Length-1)
{
if(arrary[startIndex]=='')
{
startIndex++;
endIndex++;
continue;

}
elseif(arrary[endIndex]=='')
{
intcurrentIndex=endIndex;
Reverse(arrary,startIndex,endIndex-1);
startIndex=endIndex=currentIndex;
}
elseif(endIndex==arrary.Length-1)
{
intcurrentIndex=endIndex;
Reverse(arrary,startIndex,endIndex);
startIndex=endIndex=currentIndex;
}
else
{
endIndex++;
}
}

StringBuilderbuilder=newStringBuilder();
foreach(chariteminarrary)
{
builder.Append(item);
}

returnbuilder.ToString();

}

publicstaticvoidReverse(char[]arrary,intstart,intend)
{
while(start<end)
{
chartemp=arrary[start];
arrary[start]=arrary[end];
arrary[end]=temp;

start++;
end--;
}
}





                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: