Alex Johnson
Alex Johnson

Reputation: 15

Adding strings to a list in C# one at a time

I'm fairly new at C# as a language so this is a fairly basic/simple problem that I've encountered. I don't quite know how to add each one of these letters into a list to present them all in a line at the end. For example, there's an 'IF/ELSE' statement but both produce a letter at the end. Here is my code so far, I would appreciate any help/input (please note, I started learning the language 2 days ago!)

using System;
namespace caesarCipher
{
    class Program
    {
        static void Main(string[] args)
        {
            string text;
            Console.WriteLine("Enter the text to encrypt ");
            text = System.Convert.ToString(Console.ReadLine());
            string lower = text.ToLower();
            Random rnd = new Random();
            int shift = rnd.Next(1, 25);
            foreach (char c in lower)
            {
                int unicode = c;
                int shiftUnicode = unicode + shift;
                Console.WriteLine(shiftUnicode);
                if (shiftUnicode >= 123)
                {
                    int overflowUnicode = 97 + (shiftUnicode - 123);
                    char character = (char)overflowUnicode;
                    string newText = character.ToString();
                }
                else
                {
                    char character = (char)shiftUnicode;
                    string newText = character.ToString();
                }

            }
            Console.ReadLine();
        }
    }
}

Upvotes: 0

Views: 92

Answers (2)

oRole
oRole

Reputation: 1346

I think what you want to do is to add up each character to a string. You tried to do that using a variable called newText but as your loop continues the value of newText gets overwritten.

if (shiftUnicode >= 123)
{
    int overflowUnicode = 97 + (shiftUnicode - 123);
    char character = (char)overflowUnicode;

    // value gets overwritten here
    string newText = character.ToString();
}
else
{
    char character = (char)shiftUnicode;

    // value gets overwritten here
    string newText = character.ToString();
}

What you need to do is:
- define newText above your foreach loop, so that you can access out of its scope
- append any new char to the end of your newText instead of overwriting its value
- print the value of newText to the Console using Console.WriteLine()

using System;
namespace caesarCipher
{
    class Program
    {
        static void Main(string[] args)
        {
            string text;
            Console.WriteLine("Enter the text to encrypt ");
            text = System.Convert.ToString(Console.ReadLine());
            string lower = text.ToLower();
            Random rnd = new Random();
            int shift = rnd.Next(1, 25);

            // declare and initialize newText here
            string newText = string.Empty;
            foreach (char c in lower)
            {
                int unicode = c;
                int shiftUnicode = unicode + shift;
                Console.WriteLine(shiftUnicode);
                if (shiftUnicode >= 123)
                {
                    int overflowUnicode = 97 + (shiftUnicode - 123);
                    char character = (char)overflowUnicode;

                    // append the new character to newText
                    newText += character;
                }
                else
                {
                    char character = (char)shiftUnicode;

                    // append the new character to newText
                    newText += character;
                }

            }

            // Print the value of newText to the Console
            Console.WriteLine(newText);
            Console.ReadLine();
        }
    }
}

Upvotes: 0

Steve
Steve

Reputation: 216243

You need to keep in memory each character you have 'encrypted' and just when you exit from the loop you can build the new 'encrypted' string

....
List<char> newChars = new List<char>();
foreach (char c in lower)
{
    int unicode = c;
    int shiftUnicode = unicode + shift;
    //Console.WriteLine(shiftUnicode);
    if (shiftUnicode >= 123)
    {
        int overflowUnicode = 97 + (shiftUnicode - 123);
        char character = (char)overflowUnicode;
        newChars.Add(character);    
    }
    else
    {
        char character = (char)shiftUnicode;
        newChars.Add(character);
    }
}

string newString = new string(newChars.ToArray());
Console.WriteLine(newString);
....

Upvotes: 1

Related Questions