Reverse a string with characters being intact.
using System;
using System.Linq;
namespace RetrieveData
{
internal class Program3
{
static void Main(string[] args)
{
var myString = "acbd#FGHIJKLa-34!azdd-e33fass3-1";
Console.WriteLine(myString);
char[] chars = myString.ToCharArray();
string original = string.Empty;
string toBeReversed = string.Empty;
foreach (char c in chars)
{
if (Char.IsLetter(c))
{
toBeReversed += c.ToString();
original += " ";
}
else
{
original += c.ToString();
}
}
var stringArray = toBeReversed.ToCharArray();
var reverseArray = stringArray.Reverse().ToArray();
var reversedString = new String(reverseArray);
var newString = string.Empty;
int k = 0;
for (int i = 0; i < original.Length; i++)
{
if (original.Substring(i, 1) == " ")
{
newString += reversedString.Substring(k, 1);
k++;
}
else
{
newString += original.Substring(i, 1);
}
}
Console.WriteLine(newString);
Console.ReadLine();
}
}
}
No comments:
Post a Comment