반전 고시
3911 단어 반전
class Program
{
static void Main(string[] args)
{
string input = " , 。 , 。";
IStringInverter inverter1 = new StringInverter1();
string str1 = inverter1.PiecewiseInvert(input);
Console.WriteLine(str1);
IStringInverter inverter2 = new StringInverter2();
string str2 = inverter2.PiecewiseInvert(input);
Console.WriteLine(str2);
Console.Read();
}
}
/// <summary>
///
/// </summary>
class StringInverter1 : IStringInverter
{
#region IStringInverter
public string PiecewiseInvert(string input)
{
StringBuilder sb = new StringBuilder();
int spindex = input.IndexOf('。');
for (int i = 0; i < input.Length; i++)
{
if (spindex > -1)
{
if (input[i] != '。')
{
sb.Append(input[spindex - i - 1]);
}
else
{
i -= (spindex + 1);
input = input.Remove(0, spindex + 1);
spindex = input.IndexOf('。');
sb.Append('。');
}
}
else
{
sb.Append(input[input.Length - i - 1]);
}//
}
string[] two = sb.ToString().Split('。', ',');
if (two.Length > 3)
return two[1] + "," + two[0] + "。" + two[3] + "," + two[2] + "。";
else
throw new ArgumentException(" **,**。 ");
}
#endregion
}
/// <summary>
/// ,3
/// </summary>
class StringInverter2 : IStringInverter
{
#region IStringInverter
public string PiecewiseInvert(string input)
{
Regex punctuationReg = new Regex(@"[,。]");
MatchCollection matchs = punctuationReg.Matches(input);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < matchs.Count; i++)//
{
int index = input.IndexOf(matchs[i].Value);
string section = input.Remove(index);
input = input.Remove(0, index + 1);
#region ,
var tempchars = section.ToCharArray();
int templen = tempchars.Length;
for (int c = 0; c < templen / 2; c++)
{
char temp = tempchars[c];
tempchars[c] = tempchars[templen - c - 1];
tempchars[templen - c - 1] = temp;
}
section = string.Join("", tempchars);
sb.Append(section);
#endregion
#region
//for (int c = 0; c < section.Length; c++)
//{
// sb.Append(section[section.Length - c - 1]);//
//}
#endregion
#region array
//var chars = section.ToCharArray();
//Array.Reverse(chars);
//section = string.Join("", chars);
//sb.Append(section);
#endregion
sb.Append(matchs[i].Value);//
}
return sb.ToString();
}
#endregion
}
interface IStringInverter
{
string PiecewiseInvert(string input);
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Python reversed 반전 시퀀스 및 교체 가능 객체 생성영문 문서: reversed(seq) Return a reverse iterator. seq must be an object which has a __reversed__() method or supports the ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.