Home > Software engineering >  Decode user input by index position
Decode user input by index position

Time:10-20

public static void Main(string[] args)
{
    string text = Console.ReadLine();
    int number = 0;
    int powerOfFive = 1;

    const string coders = " 0Oo1l";
    const int five = 5;

    for (int i = text.Length - 1; i >= 0; --i)
    {
        int decodedDigit = coders.IndexOf(text[i]);
        number  = powerOfFive * decodedDigit;
        powerOfFive *= five;
    }

    Console.Write(number);
}

For input data:

11 ll 00 O OO oO o 10

The console will display:

24 30 6 2 12 17 3 21

In my code I can only take one pair of characters (except the case when there are single character) separated by a space at one input at a time.

How can I take the entire string in one input?

CodePudding user response:

Looks like you are supposed to split the input string on space and then process each substring individually.

string text = Console.ReadLine();

const string coders = " 0Oo1l";
const int five = 5;

foreach (var code in text.Split(' '))
{
    int number = 0;
    int powerOfFive = 1;
    for (int i = code.Length - 1; i >= 0; --i)
    {
        int decodedDigit = coders.IndexOf(code[i]);
        number  = powerOfFive * decodedDigit;
        powerOfFive *= five;
    }
    Console.Write(number   " ");
}

Sample input/output:

11 ll 00 O OO oO o 10
24 30 6 2 12 17 3 21
  • Related