1

How can I reverse part of user input string? just number should not reverse all other part must be reversed.

ABC123DEF   --> CBA123FED
DISK0123CAR --> KSID0123RAC
596ABCDEF   --> 596FEDCBA

Thank you in advance Here is my code:

public static string ReverseStr(string sStrRev)
{
        string output = "";
        Dictionary<int, char> SChar = new Dictionary<int, char>();

        int Cposition = 0;

        for (int i = sStrRev.Length - 1; i >= 0; i--)
        {
            if (sStrRev[i] != '1' && sStrRev[i] != '2' && sStrRev[i] != '3' 
                && sStrRev[i] != '4' && sStrRev[i] != '5' 
                && sStrRev[i] != '6' && sStrRev[i] != '7' 
                && sStrRev[i] != '8' && sStrRev[i] != '9' 
                && sStrRev[i] != '0')
                output += sStrRev[i];
            else
            {
                SChar.Add(Cposition, sStrRev[i]);
            }
            Cposition++;
        }

        for (int i = 0;i<sStrRev.Length ; i++)
        {
            if (SChar.ContainsKey(i))
                output.Insert(i, SChar[i].ToString());
        }
            return output;
 }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
jaleel
  • 373
  • 8
  • 13

1 Answers1

5

I suggest using regular expression to match all the parts to be reversed and Linq to reverse them:

  using System.Linq;
  using System.Text.RegularExpressions; 

  ...

  string source = "DISK0123CAR"; 

  // KSID0123RAC 
  string result = Regex.Replace(source, 
    "[^0-9]+",                                        // all except numbers ...
     match => string.Concat(match.Value.Reverse()));  // ... should be reversed

If you want to wrap it into a method:

  public static string ReverseStr(string sStrRev) {
    // when implementing public methods do not forget to validate the input
    if (string.IsNullOrEmpty(sStrRev))
      return sStrRev;

    return Regex.Replace(sStrRev, 
      "[^0-9]+",                                        
       match => string.Concat(match.Value.Reverse())); 
  }

Edit: Please, notice that the solution doesn't swap chunks:

  ABC123DEF   --> CBA123FED    // <- CBA and FED are not swapped
  DISK0123CAR --> KSID0123RAC
  596ABCDEF   --> 596FEDCBA

In case you want to reverse the chunks' order as well

  string result = string.Concat(Regex
    .Split(source, "([^0-9]+)")
    .Reverse()
    .Select(chunk => chunk.All(c => c >= '0' && c <= '9') 
       ? chunk 
       : string.Concat(chunk.Reverse())));

The outcome will be

    ABC123DEF   --> FED123CBA    // chunks are swapped
    DISK0123CAR --> RAC0123KSID
    596ABCDEF   --> FEDCBA596
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • How about the first input `ABC123DEF --> FED123CBA` – sujith karivelil Feb 28 '17 at 06:50
  • @un-lucky: `FED123CBA -> DEF123CBA`: each non-numberic part of the string, i.e. `FED`, `CBA` will be reversed independently. – Dmitry Bychenko Feb 28 '17 at 06:55
  • Take a look into OP's first input `ABC123DEF` and he wanted that to be converted as `FED123CBA`. (this is what i understand from `ABC123DEF --> FED123CBA`) but your code gives `CBA123FED`. may be OP's specification is wrong.. lol – sujith karivelil Feb 28 '17 at 07:01
  • 1
    @un-lucky: I've mentioned the problem in the answer; the 1st example does swap chunks when the 2nd one doesn't and I can't see any way to distinguish them. – Dmitry Bychenko Feb 28 '17 at 07:04
  • sorry for my mistake the first example is wrong! it should not change the position. but your solution output :80System.Linq.Enumerable+d__99`1[System.Char]38 – jaleel Feb 28 '17 at 07:15
  • @jaleel: please, edit the question then – Dmitry Bychenko Feb 28 '17 at 07:17
  • @jaleel: it seems that you've done some errors when coping the answer. I've implemented `ReverseStr` for you – Dmitry Bychenko Feb 28 '17 at 07:31
  • @Dmitry Bychenko; According to this post http://stackoverflow.com/questions/1537528/how-to-convert-system-linq-enumerable-wherelistiteratorint-to-listint you should change your code. Thank You, sometimes it dose not return reverse string but something like : 12System.Linq.Enumerable+d__99`1[System.Char]34System.Linq.Enumerable+d__99`1[System.Char] – jaleel Mar 04 '17 at 06:36
  • @jaleel: I don't see any use of *materialization* (i.e. `ToList()` or `.ToArray()`); however, you can sefely add it: `match.Value.Reverse().ToList()` if you want. If my code doesn't work, please, provide a test case (*counter example*) – Dmitry Bychenko Mar 04 '17 at 12:27