C # Is any conversion missing?

0

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

    
asked by Shiki 27.03.2017 в 22:55
source

5 answers

1

The correction above is correct, also keep in mind that you should not add one to const

 num1 = cont + 1;

Since it will always give you as a result that "they are not symmetrical"

    
answered by 27.03.2017 / 23:10
source
1

Your cycle for should be in one of the following ways:

for (int cont = 0; cont < var1.Length; cont++)

OR

for (int cont = 0; cont <= var1.Length - 1; cont++)

For example, if you have an array with 10 items, your major index will be 9, since .NET handles all arrays with indexes starting at 0, so if you use var1.Length (que para el ejemplo que te pongo seria igual a 10) when the cycle for reaches the Last you will try to access index 10, which does not exist within the arrangement and therefore will present the error that is happening to you.

    
answered by 27.03.2017 в 23:06
1

How about something like this

  string var1 = Console.ReadLine();
        string[] separadores = { " " };

        string[] subCadenas = var1.Split(separadores, StringSplitOptions.RemoveEmptyEntries);
        if (subCadenas[0].Length == subCadenas[1].Length)
        {
            Console.WriteLine("son simetricos");
        }
        else {
            Console.WriteLine("no son simetricos");
        }
        Console.WriteLine(subCadenas[0].Length);
    
answered by 27.03.2017 в 23:20
1

The Exception IndexOutOfRangeException occurs when you try to sign in to an element of the arrangement using an index outside the limit (major) or minor

To solve this error it would be enough to modify the condition of your for since the indexes start from 0 (first element) and end in (n -1) (last item)

for (int cont = 0; cont < var1.Length; cont++)

Update

With respect to validate if they are symmetrical or not. I could try this.

if (var1[cont] == ' ')
{
   if (((var1.Length - 1) - cont) == cont) Console.WriteLine("son simetricos");
   else Console.WriteLine("no son simetricos");
   break;
}
    
answered by 27.03.2017 в 23:06
1

For what I understand what you want to do after all is that a user enter two different words, and that the system tells you if they have the same number of letters each, no matter what they are.

I made a small code that does that. You can try it on link

//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
public class Program
{
    public static void Main(string[] args)
    {
        //Your code goes here
        var temporal = Console.ReadLine();
    //Con la instrucción que esta abajo, separo las palabras por el espacio vacío que hay entre ellas y genero un arreglo.
        var palabras = temporal.Split(' ');

        //valido que sean al menos dos palabras 
        if(palabras.Length > 1){
            if(palabras[0].Length == palabras[1].Length){
                Console.WriteLine("Son iguales");
            }else{
                Console.WriteLine("No son iguales");
            }
        }else{
            Console.WriteLine("Debe ingresar por lo menos dos palabras.");
        }
    }
}
}

Making adjustments to the code you can put the number of words you want and compare each one using a little imagination:

//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
public class Program
{
    public static void Main(string[] args)
    {
        //Your code goes here
        var temporal = Console.ReadLine();
        var palabras = temporal.Split(' ');

        //valido que sean al menos dos palabras 
        if(palabras.Length > 1){

            var numeroLetras = 0;
            bool iguales = true;
            foreach(string temp in palabras){
                if(numeroLetras != 0 && numeroLetras != temp.Length){
                    iguales = false;
                }
                numeroLetras = temp.Length;
            }

            if(iguales){
                Console.WriteLine("Todas las palabras tienen el mismo largo");
            }else{
                Console.WriteLine("Las palabras tienen distinto numero de letras.");
            }
        }else{
            Console.WriteLine("Debe ingresar por lo menos dos palabras.");
        }
    }
}
}

This is how it looks on the page that I sent you, to get the input, you must click on the plus sign that appears as a minus sign in the photo, where the cursor is.

I hope I helped you friend. Greetings!

    
answered by 28.03.2017 в 01:11