- class Program
- {
- static void Main(string[] args)
- {
- string s = null;
- char[] charArray;
- char temp = ' ';
- string str = "hello c sharp";
- string[] strArray = str.Split(' ');//把str这个字符串以空格为分隔符分开,strArray={hello,c,sharp}
- for (int i = 0; i < strArray.Length; i++)
- {
- charArray = strArray[i].ToCharArray();//把strArray的每一个元素变为char数组,{h,e,l,l,o},{c},{s,h,a,r,p}
- for (int j = 0; j < charArray.Length / 2; j++)//交换charArray[j]里的位置,交换次数为小于长度的一半
- {
- temp = charArray[j];
- charArray[j] = charArray[charArray.Length - 1 - j];
- charArray[charArray.Length - 1 - j] = temp;
- }
- foreach (char c in charArray)
- {
- s += c;
- }
- Console.Write(s+" ");
- s = null;//将s清空,否则输出olleh ollehc ollehcprahs
- }
- Console.ReadKey();
- }
- }
复制代码 |