How do I save an array of chars in a variable?

-5

I'm doing an encryption program, starting by inverting 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]);
        }
    }
}

It works and the string is inverted. But when I'm going to save chars[i] to a variable, ie save all inverted string to 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 repeat loop for?

    
asked by anonymous 26.08.2018 / 22:24

2 answers

1

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

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

Note that you are not creating any encryption with this code. Not even close to this.

You can even make it more efficient by eliminating even the StringBuilder allocation, but I do not think it's that important, it would be for maximum efficiency and tests would be done to determine if you really think so.

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}");
    }
}
    
26.08.2018 / 22:53
0

I The example variable was being rewritten.

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

Working Code:

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);

    }
}
    
26.08.2018 / 23:16