写一个Reserve函数处理字符串,比如输入”I am a boy”,输出”boy a am I”

C# codes as below:

public static string MyReserve(string str)

{

if (str == string.Empty)

{

return str;

}

char[] myChar = str.ToCharArray();

char[] myChar2 = new char[myChar.Length];

bool mark;

int count = 0;

if (myChar[0] == ' ')

{

mark = true;

}

else

{

mark = false;

}

for (int i = 0; i < myChar.Length; i++)

{

if (myChar[i] == ' ')

{

if (mark == true)

{

count++;

}

else

{

for (int j = 0; j < count; j++)

{

myChar2[myChar.Length -i +j ] = myChar[i - j - 1];

}

count = 0;

mark = true;

i--;

}

}

else

{

if (mark == false)

{

count++;

}

else

{

for (int j = 0; j < count; j++)

{

myChar2[myChar.Length - i + j] = myChar[i - j - 1];

}

count = 0;

mark = false;

i--;

}

}

}

for (int j = 0; j < count; j++)

{

myChar2[j] = myChar[myChar.Length-1-j];

}

return new string(myChar2);

}

你可能感兴趣的:(写一个Reserve函数处理字符串,比如输入”I am a boy”,输出”boy a am I”)