0

I am trying to do string manipulation. Here's my C# code :

 static void Main(string[] args)
 {
    string input;
    string output;
    int length;

    Console.WriteLine("input = ");
    input = Console.ReadLine();
    length = input.Length;

    if ((input != "") || (length != 0))
    {
        Random randem = new Random();
        int i = -1; //because I do not want the first number to be replaced by the random number 
        char[] characters = input.ToCharArray();
        while (i < length)
        {
            int num = randem.Next(0, 9);
            char num1 = Convert.ToChar(num);
            i = i + 2; //so that every next character will be replaced by random number.. :D
            characters[i] = num1; //*error* here
        }
        output = new string(characters);
        Console.WriteLine(output);
     }

For example:

User input : "i_love_to_eat_fish"

Desired output : "i2l4v1_9o5e8t7f8s2"

notice that the only unchanged character in the char[] characters is : "i l v _ o e t f s". (desired output from the program)

I've already tried using this code, but still, keep getting error at characters[i] = num1;

Am I on the right track?

2
  • What's the error? Is it ArgumentOutOfRange? Commented Jul 2, 2015 at 14:58
  • IndexOutOfTheRangeException Commented Jul 2, 2015 at 15:04

5 Answers 5

2

I'm guessing the error you get is IndexOutOfRangeException this is because of the i = i + 2;. The while makes sure that i is less than length, but then adding 2 could result in it being more. Just add a check that it isn't beyond the length.

i = i + 2; 
if(i < length)
    characters[i] = num1;

Or just change to a for loop.

Random randem = new Random();
char[] characters = input.ToCharArray();
for(int i = 1; i < length; i += 2)
{
    int num = randem.Next(1, 10); // max value is exclusive
    char num1 = num.ToString()[0];
    characters[i] = num1;
}
output = new string(characters);
Console.WriteLine(output);

Also as Shar1er80 points out you're currently converting the digit to the char that has the same ASCII value, and not the the actual characters that represent the digit. The digits 0-9 are represented by the the values 48-57. You can change the call to Random.Next to be:

int num = randem.Next(48, 58); // The upper bound is exclusive, not inclusive
char num1 = (char)num;

Or as Shar1er80 does it

int num = randem.Next(0,10) // Assumming you want digits 0-9
char num1 = num.ToString[0];

Also note that the max value for Random.Next is exclusive, so if you want to include the possibility of using a 9 you have to use an upper bound that is 1 greater than the greatest value you want.

Sign up to request clarification or add additional context in comments.

Comments

2

Whenever you reach i = 17 you add 2 to i . That makes i = 19 with length of input equal to 18 that causes out of range exception.

Comments

1

The error you are getting is IndexOutOfTheRangeException, which explains everthing in itself.

It means that index you are feeding to array in the loop is going beyond its length-1 (as arrays have 0-based indexing)

So when you do i+2, you need to check if i+2 is not exceeding i.length-1 at any point of time; which does in your loop.

In general just check if you are supplying indexes between 0 and Array.Length-1

Comments

1

its because you start at index -1, and characters doesn't contain an index of -1.

EDIT: Sorry no the corrct answer is it must be while(i < length - 2)

1 Comment

But in the loop the OP does i = i + 2;, so the first index hit will be 1.
1

Change this line

char num1 = Convert.ToChar(num);

To

char num1 = num.ToString()[0];

Then... Put

characters[i] = num1;

In an if block

if (i < length)
    characters[i] = num1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.