How to save an array of chars in a variable?

Asked

Viewed 204 times

-5

I’m doing an encryption program, starting to reverse the string with the following code:

namespace APS
{
    class Program
    {
        public static void Main(string[] args)
        {
            string frase;
            Console.WriteLine("Digite uma frase: ");
            frase = Console.ReadLine();
            frase = frase.ToLower();
            frase = frase.Replace(" ","0");
            var chars = frase.ToCharArray();
            Console.WriteLine("Original string: {0}", frase);
            Console.WriteLine("Character array: ");
            for (int i = frase.Length - 1; i > -1; i--)
                Console.Write(chars[i]);
        }
    }
}

Works and the string is reversed. But when I go to save chars[i] in a variable, i.e., save all string reversed into an error variable, it looks like this:

using System;

namespace APS
{
    class Program
    {
        public static void Main(string[] args)
        {
            string frase;
            Console.WriteLine("Digite uma frase: ");
            frase = Console.ReadLine();
            frase = frase.ToLower();
            frase = frase.Replace(" ","0");
            var chars = frase.ToCharArray();
            Console.WriteLine("Original string: {0}", frase);
            Console.WriteLine("Character array: ");
            for (int i = frase.Length - 1; i > -1; i--)
                VAR EXEMPLO = chars[i];
                //QUAL A FORMA CERTA DE SALVER TODA ESSA SEQUÊNCIA DE CARACTERES?
        }       
    }
}

How do I save and use out of loop loop for?

  • 1

    What is the need to write everything in high box?

  • I recommend you read this link.

  • But is that really how you wrote it? This is a really wrong syntax, you know the syntax of C#?

2 answers

1

The code is confusing and inefficient, to build strings should use the StringBuilder, without it the complexity of it becomes prohibitive in large texts.

And if you are going to manipulate face character individually do everything you need in it at once. When you manipulate strings Each time it is making a loop and is allocating memory that may be unnecessary, like this case. Just because you don’t see the complexity hidden by the method doesn’t mean it’s not there. Don’t use a method without knowing what it does. Most have harmful side effects for certain situations. Some are considered even legacies.

Note that you are not creating any encryption with this code. Nowhere near this.

You can even make it more efficient by eliminating even the allocation of StringBuilder, but I don’t think it’s that important, it would be for maximum efficiency and testing would be done to determine if with real thought.

using static System.Console;
using System.Text;

public class Program {
    public static void Main() {
        WriteLine("Digite uma frase: ");
        var frase = ReadLine();
        WriteLine($"Original string: {frase}");
        var invertida = new StringBuilder(frase.Length);
        foreach (var chr in frase) invertida.Append(char.ToLower(chr == ' ' ? '0' : chr));
        WriteLine($"Character array: {invertida}");
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

0

I. The example variable was being rewritten.

II. it is not a global variable but a local variable, so it only exists in that context, if you try to use it outside the structure for will accuse error exactly for this reason.

Code working:

using System;

public class Program
{
    public static void Main()
    {
            var exemplo = "";
            string frase;
            Console.WriteLine("Digite uma frase: ");
            frase = Console.ReadLine();
            frase = frase.ToLower();
            frase = frase.Replace(" ","0");
            var chars = frase.ToCharArray();
            Console.WriteLine("Original string: {0}", frase);
            for (int i = frase.Length - 1; i > -1; i--){
                exemplo += chars[i];
            }
            Console.WriteLine("Character array: " + exemplo);

    }
}
  • vlw even. kkk so simple to resolve thanks

  • @Viniciuscaetano that good guy kk, can mark the answer as accepted then?

  • Only one more doubt here:

  • as I do to catch the indece of an array and not its value. Ex: char[] a = new char[]{'a','b','c'}; I don’t want a[0] = a, I want to take only the indece type if your indece in the array

  • @Viniciuscaetano the recommended would be you ask another question or edit this your asking about it. Next to the answers there are two arrows, one up and one down, below the down arrow there is a corrected/checked symbol that when you pass the mouse arrow over it turns green, that’s where you mark the answer as accepted. abs!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.