Jump to content

simple C# code bug

Go to solution Solved by Bigst4v4,

char[] ReverseText = new char[1000] ;

 

string str = "abcdefg";
Console.WriteLine(str);

Console.readLine(str);

 

int length = str.Length;

int size = length;

for (int i = 0; i < length; i++)         

  {

                ReverseText [i] = UserText [size]; // error here
                size--;
  }

 
Try this ..

So im trying to create a "reverse a string" program but when i compile i have an error which says : "Index was outside the bounds of the array." but when i subtract " UserText.Length" by 1 i lose my one character at the end but it does compile.       

 

            string UserText;
            char[] ReverseText = new char[1000] ;
            //string test = "This is my string";
            Console.WriteLine ("Enter a String:");
            UserText = Console.ReadLine ();

            int length = UserText.Length; 
            int size=length;
            for (int i = 0; i < length; i++)
            {
                
                ReverseText [i] = UserText [size]; // error here
                size--;
            }

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
https://linustechtips.com/topic/445715-simple-c-code-bug/
Share on other sites

Link to post
Share on other sites

char[] ReverseText = new char[1000] ;

 

string str = "abcdefg";
Console.WriteLine(str);

Console.readLine(str);

 

int length = str.Length;

int size = length;

for (int i = 0; i < length; i++)         

  {

                ReverseText [i] = UserText [size]; // error here
                size--;
  }

 
Try this ..
Link to comment
https://linustechtips.com/topic/445715-simple-c-code-bug/#findComment-5973716
Share on other sites

Link to post
Share on other sites

 

char[] ReverseText = new char[1000] ;

 

string str = "abcdefg";

Console.WriteLine(str);

Console.readLine(str);

 

int length = str.Length;

int size = length;

for (int i = 0; i < length; i++)         

  {

                ReverseText [i] = UserText [size]; // error here

                size--;

  }

 
Try this ..

 

just solved my prob. in the next for (printing reverse word) i had that -1 character error :D. thanks anyway

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
https://linustechtips.com/topic/445715-simple-c-code-bug/#findComment-5973747
Share on other sites

Link to post
Share on other sites

snip

nevermind you got it to work lol

CPU: AMD FX-6300 4GHz @ 1.3 volts | CPU Cooler: Cooler Master Hyper 212 EVO | RAM: 8GB DDR3

Motherboard: Gigabyte 970A-DS3P | GPU: EVGA GTX 960 SSC | SSD: 250GB Samsung 850 EVO

HDD: 1TB WD Caviar Green | Case: Fractal Design Core 2500 | OS: Windows 10 Home

Link to comment
https://linustechtips.com/topic/445715-simple-c-code-bug/#findComment-5973891
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×