#region 求倒置 输入字符串 "Hello world",要求输出 dlrow olleh
/// <summary> /// string resource = "Hello world"; /// </summary> /// <param name="resource"></param> /// <returns></returns> public static char[] revert(string resource) { char[] a = resource.ToArray(); char b;
for (int i = 0; i <= a.Length / 2; i++) // for(int i=0;i<(a.leng-1)/2) { b = a[i]; a[i] = a[a.Length - 1 - i]; a[a.Length - 1 - i] = b; }
return a.ToArray(); // the drawback of this method is the result type is a array, the same time this is a advantage of it.
}
public static string reverTostring(string resource) { char[] a = resource.ToArray(); char b; StringBuilder sb = new StringBuilder();
for (int i = 0; i < a.Length - 1 / 2;i++ ) { b = a[i]; a[i] = a[a.Length - 1 - i]; a[a.Length - 1 - i] = b;
} foreach(char achar in a) { sb.Append(achar); } return sb.ToString(); }
/// <summary> /// this method use the statck /// </summary> /// <param name="resource"></param> /// <returns></returns> public static string StackRevertMethod(string resource) { string result = string.Empty; Stack<string> a = new Stack<string>(); foreach (char achar in resource) { a.Push(achar.ToString()); }
while (a.Count != 0) // c# only have this method to count the number of the value { result = result + a.Pop().ToString(); }
return result;
} /// <summary> /// using the stringbuilder /// </summary> /// <param name="resource"></param> /// <returns></returns>
public static string rollRevert(string resource) {
char[] a = resource.ToCharArray(); StringBuilder sb = new StringBuilder();
for (int i = a.Length - 1; i >= 0; i--) { sb.Append(a[i]); } return sb.ToString(); }
#endregion 求倒置完
#region 求倒置 输入字符串 "Hello world",要求输出olleh dlrow /// <summary> /// this method only use the double rotate /// </summary> /// <param name="resource"></param> /// <returns></returns> public static string rollRevert2(string resource) {
char[] a = resource.ToCharArray(); StringBuilder sb = new StringBuilder();
int j = 0; for (int i = 1; i < a.Length; i++) { if (a[i].ToString() == " ") { j = i; } }
for (int i = j - 1; i >= 0; i--) { sb.Append(a[i]); } sb.Append(" "); for (int i = a.Length - 1; i > j; i--) { sb.Append(a[i]); } return sb.ToString(); }
/// <summary> /// return the arry type /// </summary> /// <param name="resource"></param> /// <returns></returns> public static char[] revert2(string resource) { int j = 0; char[] a = resource.ToArray(); char b; for (int i = 0; i < a.Length; i++) { if (a[i].ToString() == " ") { j = i; } } for (int k = 0; k <= (j - 1)/ 2; k++) { b = a[k]; a[k] = a[j - 1 - k]; a[j - 1 - k] = b; }
for (int k = j + 1; k <= (a.Length - j) / 2+j; k++) { b = a[k]; a[k] = a[a.Length-k+j]; a[a.Length-k+j] = b; } return a.ToArray();
} #endregion
翻转句子中单词的顺序。
题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。
为简单起见,标点符号和普通字母一样处理。
例如输入“I am a student.”,则输出“student. a am I”。 或者是hello world,要求输出world hello.
/// <summary>
/// 求倒置 输入字符串 "Hello world",要求输出 world hello
/// 或是i am a student,输出的内容是:student a am i.
/// </summary>
/// <param name="resource"></param>
/// <returns></returns>
public static string revertString(string resource)
{
StringBuilder sb = new StringBuilder(string.Empty);
int len = resource.Length;
char[] resourceToAarry = resource.trim().ToArray();
// added a List<int> to save the position of ""
List<int> position = new List<int>();
for (int i = 0; i < len; i++)
{
if (resourceToAarry[i].ToString()==" ")
{
position.Add(i);
}
}
if (position==null)
{
sb.Append(resource);
return sb.ToString();
}
else
{
for (int i = position.Count; i >0;i-- )
{
for (int k = position[i-1]+1; k < len;k++ )
{
// 去掉我们可能得到的空格是因为len的控制问题 精简的代码要去掉这个东西
if (resourceToAarry[k].ToString() != "")
{
sb.Append(resourceToAarry[k]);
}
}
len = position[i-1];
sb.Append(" ");
}
for (int p = 0; p < len;p++ )
{
sb.Append(resourceToAarry[p]);
}
return sb.ToString();
}
}
}