c# - 如何从结尾 c# 反转字符串 2 个字符和 2 个字符
问题描述
样品string
:
A3148579
预期结果:
798514A3
我试过这段代码:
public static string Reverse(string s)
{
char[] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
实际结果是9758413A
但我想要798514A3
感谢大家
解决方案
You can try below code. This is just to give you a idea and you can update based on the test cases and requirement. Below code works fine for your input which you have mentioned. I have not considered if the length is odd. You can do your research and update logic which will help you to learn and know more.
string input = "A3148579";
Stack stack = new Stack();
int count = 0;
string output = "";
for (int i = 0; i < input.Length/2; i++)
{
stack.Push(input.Substring(count, 2));
count = count + 2;
}
while (stack.Count > 0)
{
output += stack.Pop().ToString();
}
推荐阅读
- android - react-native-vector-icons 中的错误,图标是日语
- python - 如何从 pandas sum / is in function 中获取一个数字
- javascript - 如何将普通文本和嵌入在同一条消息中?不和谐.js
- python - 提高将数据附加到文件的效率
- algorithm - 我如何找到这个递归算法的复杂性?用二进制数替换字符串中的模式
- javascript - 音频静音-取消静音图标
- stripe-payments - 类条带没有结帐
- javascript - Javascript删除特定屏幕尺寸的类不起作用?
- python - 如何防止我的 Python 网络爬虫停止?
- r - 无法弄清楚如何使用 if 语句格式化 for 循环