我正在準備面試,面臨著任務的問題。情況是我們有一個字串: test12pop90java989python
我需要回傳新字串,其中單詞將被反轉并且數字將保持在同一位置:
test12pop90java989python ==> tset12pop90avaj989nohtyp
我開始的:
將字串傳輸到 char 陣列
使用 for 回圈 Char.IsNumber
??
var charArray = test.ToCharArray(); for (int i = 0; i < charArray.Length; i ) { if (!Char.IsNumber(charArray[i])) { .... } }
但目前我被卡住了,不知道如何進行,有什么提示可以做到嗎?
uj5u.com熱心網友回復:
嘿,您可以執行以下操作:
string test = "test12pop90java989python", tempStr = "", finalstr = "";
var charArray = test.ToCharArray();
for (int i = 0; i < charArray.Length; i )
{
if (!Char.IsNumber(charArray[i]))
{
tempStr = charArray[i];
}
else
{
char[] ReverseString = tempStr.Reverse().ToArray();
foreach (char charItem in ReverseString)
{
finalstr = charItem;
}
tempStr = "";
finalstr = charArray[i];
}
}
if(tempStr != "" && tempStr != null)
{
char[] ReverseString = tempStr.Reverse().ToArray();
foreach (char charItem in ReverseString)
{
finalstr = charItem;
}
tempStr = "";
}
我希望這有幫助
uj5u.com熱心網友回復:
在觀察整個程序之前,您無法反轉一連串的字母;在此之前,您需要跟蹤待處理的字母,以便在遇到數字或字串結尾時反轉并附加到最??終輸出。通過將這些掛起的字符存盤在 a 中Stack<>,它們自然會以添加它們的相反順序回傳。
static string Transform(string input)
{
StringBuilder outputBuilder = new StringBuilder(input.Length);
Stack<char> pending = new Stack<char>();
foreach (char c in input)
if (char.IsNumber(c))
{
// In the reverse order of which they were added, consume
// and append pending characters as long as they are available
while (pending.Count > 0)
outputBuilder.Append(pending.Pop());
// Alternatively...
//foreach (char p in pending)
// outputBuilder.Append(p);
//pending.Clear();
outputBuilder.Append(c);
}
else
pending.Push(c);
// Handle pending characters when input does not end with a number
while (pending.Count > 0)
outputBuilder.Append(pending.Pop());
return outputBuilder.ToString();
}
一種類似但無緩沖區的方法是存盤當前字母運行開始的索引,然后在找到數字時回傳并附加每個字符......
static string Transform(string input)
{
StringBuilder outputBuilder = new StringBuilder(input.Length);
int lettersStartIndex = -1;
for (int i = 0; i < input.Length; i )
{
char c = input[i];
if (char.IsNumber(c))
{
if (lettersStartIndex >= 0)
{
// Iterate backwards from the previous character to the start of the run
for (int j = i - 1; j >= lettersStartIndex; j--)
outputBuilder.Append(input[j]);
lettersStartIndex = -1;
}
outputBuilder.Append(c);
}
else if (lettersStartIndex < 0)
lettersStartIndex = i;
}
// Handle remaining characters when input does not end with a number
if (lettersStartIndex >= 0)
for (int j = input.Length - 1; j >= lettersStartIndex; j--)
outputBuilder.Append(input[j]);
return outputBuilder.ToString();
}
對于這兩種實作,呼叫Transform()...
string[] inputs = new string[] {
"test12pop90java989python",
"123test12pop90java989python321",
"This text contains no numbers",
"1a2b3c"
};
for (int i = 0; i < inputs.Length; i )
{
string input = inputs[i];
string output = Transform(input);
Console.WriteLine($" Input[{i}]: \"{input }\"");
Console.WriteLine($"Output[{i}]: \"{output}\"");
Console.WriteLine();
}
...產生這個輸出...
輸入[0]:“test12pop90java989python” 輸出[0]:“tset12pop90avaj989nohtyp” 輸入[1]:“123test12pop90java989python321” 輸出[1]:“123tset12pop90avaj989nohtyp321” 輸入[2]:“此文本不包含數字” 輸出[2]:“srebmun on sniatnoc txet sihT” 輸入[3]:“1a2b3c” 輸出[3]:“1a2b3c”
uj5u.com熱心網友回復:
使用 Regex 和 Linq 的可能解決方案:
using System;
using System.Text.RegularExpressions;
using System.Linq;
public class Program
{
public static void Main()
{
var result = "";
var matchList = Regex.Matches("test12pop90java989python", "([a-zA-Z]*)(\\d*)");
var list = matchList.Cast<Match>().SelectMany(o =>o.Groups.Cast<Capture>().Skip(1).Select(c => c.Value));
foreach (var el in list)
{
if (el.All(char.IsDigit))
{
result = el;
}
else
{
result = new string(el.Reverse().ToArray());
}
}
Console.WriteLine(result);
}
}
我使用來自stackoverflow.com/a/21123574/1037948的代碼在第 11 行創建了一個正則運算式匹配串列:
var list = matchList.Cast<Match>().SelectMany(o =>o.Groups.Cast<Capture>().Skip(1).Select(c => c.Value));
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/509938.html
標籤:C#。网细绳撤销文本操作
