Hussain Khawaja
How to reverse a string without changing order of special characters (e.g. commas)?
By Hussain Khawaja in Algorithms in C# on Jul 07 2016
  • lavan KUMAR
    Mar, 2018 14

    If The string is Like "ABC@@" i nedd "CBA@@" public static string reverse(string str){int r = str.Length - 1, l = 0;while (l < r){if (!char.IsLetterOrDigit(str[l]))l++;else if (!char.IsLetterOrDigit(str[r]))r--;else // Both str[l] and str[r] are not spacial{char[] array = str.ToCharArray(); char temp = array[l]; array[l] = array[r]; array[r] = temp; str = new string(array);l++;r--;}}return str;#endregion}

    • 1
  • Adarsh Gowda
    Aug, 2018 16

    Hi this is my first attempt for this kind of QA replying,if I'm wrong please pardon me . you can write a regex to find all the special characters and split by special characters it will give u a array then reverse the index of array and join again with the special characters

    • 0
  • lavan KUMAR
    Mar, 2018 14

    If The string is Like "ABC@@" i need "CBA@@" public static string reverse(string str){int r = str.Length - 1, l = 0;while (l < r){if (!char.IsLetterOrDigit(str[l]))l++;else if (!char.IsLetterOrDigit(str[r]))r--;else // Both str[l] and str[r] are not spacial{char[] array = str.ToCharArray(); char temp = array[l]; array[l] = array[r]; array[r] = temp; str = new string(array);l++;r--;}}return str;#endregion}

    • 0
  • khushi shah
    Jun, 2017 25

    public static string ReverseString(string inputString){ char[] inputCharArray = inputString.ToCharArray();char[] reverseCharArray = new char[inputCharArray.Length];inputCharArray.CopyTo(reverseCharArray, 0);Array.Reverse(reverseCharArray);for(int i= 0; i< inputCharArray.Length; i ){if(!char.IsLetterOrDigit(reverseCharArray[i])){reverseCharArray[i] = inputCharArray[i];}}return new string(reverseCharArray);}

    • 0

Most Popular Job Functions


MOST LIKED QUESTIONS