using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ReverseWord {
class Program
{
static string Revert(string target)
{
if (target == null || target == string.Empty)
{
throw new ArgumentException "Error:Can not revert empty string! Please enter input like this:How are you?";
}
// clear blank both end!
char blank = ' ';
if (target.StartsWith(blank.ToString()))
target = target.Trim();
StringBuilder builder = new StringBuilder();
int i = target.Length - 1;
int j = target.Length - 1;
if (char.IsPunctuation(target[target.Length - 1]))
{
i = target.Length - 2;
j = target.Length - 2;
}
while (j > 0)
{
if (target[j] == blank)
{
builder.Append(target.Substring(j + 1, i - j));
builder.Append(blank.ToString());
i = j - 1;
}
j--;
}
builder.Append(target.Substring(j, i + 1));
if (char.IsPunctuation(target[target.Length - 1]))
builder.Append(target[target.Length - 1].ToString());
return builder.ToString();
}
}
}
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。