It is assumed that a person enters a line of characters per keyboard separated by a blank space followed by another line of characters, the program should indicate if the characters that are separated by the blank space have the same amount for example ( asd sad). I think the approach I did is fine but for some reason it does not work ...
The output I get when I enter the keyboard (asd asd) is the else, in addition to this I also get
System.IndexOutOfRangeException on this line
if (var1[cont] == ' ')
This is my code
string var1;
int num1;
var1 = Console.ReadLine();
for (int cont = 0; cont <= var1.Length; cont++)
{
if (var1[cont] == ' ')
{
num1 = cont + 1;
if (((var1.Length - 1) - num1) == num1)
{
Console.WriteLine("son simetricos");
}
else
{
Console.WriteLine("no son simetricos");
}
}
}
Console.WriteLine(var1.Length);
Console.ReadKey();
It is true I could understand that part every time I entered the first IF at some time when I fulfilled the condition taking into account that starts from 0 and that Length gives us the number of elements, enters the IF and compares an OutRange element.
But for some reason I still can not enter the second IF, I think it is not performing the operations that I indicated or some term does not have the correct data type ..
Someone below said that I would have to remove cont + 1 but I think that is not so because cont is equal subindice that I am going through for example income (AB) A is index 0 if I do cont +1 I say there is a character after this the total of characters will remain 1 for q 1 is the blank that gives me the total of characters without the blank, well now I only have to subtract that total cont + 1, if that subtraction is equal to +1 then they are the same or symmetrical