Swap neighbor character in string in c#:
string str = "TAPAN";
Console.WriteLine("Before Swaping :"+str);
string str1 = "";
Console.Write("After Swaping :");
for (int i = 0; i < str.Length; i = i + 2)
{
int j = 0;
if (str.Length % 2 != 0 && i == str.Length - 1) { j = 1; }
str1 += j == 0 ? str[i + 1].ToString() : "";
str1 += str[i].ToString();
}
Console.WriteLine(str1);
Console.WriteLine("Before Swaping :"+str);
string str1 = "";
Console.Write("After Swaping :");
for (int i = 0; i < str.Length; i = i + 2)
{
int j = 0;
if (str.Length % 2 != 0 && i == str.Length - 1) { j = 1; }
str1 += j == 0 ? str[i + 1].ToString() : "";
str1 += str[i].ToString();
}
Console.WriteLine(str1);
No comments:
Post a Comment