FAQ: Working with Text - Review

This community-built FAQ covers the “Review” exercise from the lesson “Working with Text”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C#

FAQs on the exercise Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Once again, trying to be a good student, I wanted to get my hand at the extra homework from this page:

https://www.codecademy.com/courses/learn-c-sharp/lessons/csharp-working-with-text/exercises/review-working-with-strings-csharp

Perhaps it is me, but I don’t feel equipped with the knowledge for obtaining random values or creating an array of string at this point in the course. I can probably figure it out on my own from knowing other languages, but I feel like that kind of exercise would be extremely daunting for an actual newcomer to coding.

Maybe it’s the goal of this extra exercise, to get us outside our comfort bubble and explore the internet for answer, but I feel like that expectation is poorly communicated. It really feels like we should already have all the knowledge for these extra exercises, but we don’t.

I’ll try to solve it on my own, but this is more a pedagogic concern for greener students than me. Let me know what you think.

3 Likes

Actually, it turns out it might take me many hours to understand all that is needed. Perhaps I’m taking the long way and I’m doing it the wrong way, but here’s is what my newbie brain tried to do:

      string strToRandom = "Once upon a midnight dreary, while I pondered, weak and wary.";
        
      int lengthStr = strToRandom.Length;
			string randomizedStr = strToRandom;
      for (int characterNumber = 0; characterNumber < lengthStr; characterNumber++)
      {
        Random rnd = new Random();
        int randomNumber = rnd.Next(0, 2);

        bool isCap = Convert.ToBoolean(randomNumber);
        
        if (isCap)
        {
          randomizedStr[characterNumber] = char.ToUpper(strToRandom[characterNumber]);
        }
      }
			//Console.WriteLine(randomizedStr);
    }

Here is what I had to learn on my own so far to make it work:
For loops
Random methods (What is a method?)
If statements

And then I was hit with something about String being read only andI had to use another class. Learning about classes seems to be way outside of the scope of this chapter, is it not?

Hi Shintenpu,

You were almost there, a small change and using a StringBuilder could make it work.
You can find info on it here:

Note, you also need to include System.Text class. This is done by adding it to the top of your code so the first two lines read:

using System;
using System.Text;

Now we have access to a StringBuilder it can be used as followed in your example.

if (isCap)
{
	StringBuilder sb = new StringBuilder(randomizedStr);
	sb[characterNumber] = char.ToUpper(strToRandom[characterNumber]);
	randomizedStr = sb.ToString(); 
}

You could also declare “StringBuilder sb = new StringBuilder(randomizedStr);” outside of the for loop after declaring randomizedStr to save declaring it several times. The result is the same though.

I do agree that this is a fair few steps above the level of the exercise and learners could feel bad after not being able to complete it whilst assuming they should have the needed info already.

1 Like

This highlights another failure of this course: the concept of classes wasn’t explained yet. At the moment I undertook the course, there was no material about it.

The order of these “homework” according to the current knowledge students have don’t match.

Anyway, thank you for your answer and the time you took to write it.

I saw that several others in this forum had shared results for the randomized case assignment, so I wanted to give an example of the word randomization one. This required researching the documentation for how to use for, foreach, List<>, Random, and TryParse.
I did write the code to use TryParse to convert a string from ReadLine to allow the user to select the length of the randomized text, but it appears that this exercise doesn’t allow console input.

If I had to do it over again I would be more consistent about where I initialized variables. I tried to do a lot of it at the head of the program, in hopes of making things more readable in the meat of the code, but I started being inconsistent as I continued.

using System;
using System.Collections.Generic;

namespace Review
{
    class Program
    {
        static void Main()
        {
            /* use this space to write your own short program! 
            Here's what you learned:

            DATA TYPES: char, string
            STRING INTERPOLATION: $"blah blah"
            STRING INFO: .Length, .IndexOf()
            PARTS OF STRINGS: bracketNotation[], .Substring() 
            STRING MANIPULATION: .ToUpper(), .ToLower()

            Good luck! */
            // the source of words
                        Random rand = new Random();
            string defaultSource = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch! Beware the Jubjub bird, and shun The frumious Bandersnatch!";
            string randomizeThis = null;
            Console.WriteLine("Would you like to use the default source material? (Y/N)");
            string defaultTrue = Console.ReadLine();
            if (defaultTrue.ToLower() == "y")
            {
                randomizeThis = defaultSource;
            }
            else
            {
                Console.WriteLine("Please input desired source material.");
                randomizeThis = Console.ReadLine();
            }
            
            string randoPoem = null;
            string poemLengthInput;
            // split the source into individual words in a list called words     
            List<string> words = new List<string>();           
            foreach (string word in randomizeThis.Split(' '))
            {
                words.Add(word);
            }
            int sourceWordCount = words.Count;
            Console.WriteLine($"The source material is {sourceWordCount} words long.");
            // ask the user how long they would like the poem to be
            Console.WriteLine("How long would you like your poem to be?");
            //Since this exercise doesn't open in a shell console allowing input, force the length here.
            //poemLengthInput = "10";
            poemLengthInput = Console.ReadLine();
            Int32.TryParse(poemLengthInput, out int poemLength);
            for (int i = 0; i < poemLength; i++)
            {
                randoPoem += $"{words[rand.Next(sourceWordCount)]} ";
            }
            Console.WriteLine(randoPoem);
            /* print all the words in the list
            foreach(string aword in words)
            {
              Console.WriteLine(aword);
            }
            */
        }
    }
}

Hi, while finishing this lesson I also thought about being a good student and learning while making this task, I totally agree with shintenpu’s comments, and I was able to finish this mainly because I know a bit of c++ and it helped me to think in different ways to do what I was asked to do, I would like some feedback in how to improve my code, or to know if there are easier ways :>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Randomizer
{
    class Program
    {
        static void Main(string[] args)
        {//declaring variables
            string userInput;


            //Printing text and storing user input in variable.
            Console.WriteLine("Please introduce a sentence to randomize");
          userInput = Console.ReadLine();

            //Ramdomize user's input
            for (int i = 0; i < userInput.Length; i++)
            {
                Random rnd = new Random();
                int coinFlip = rnd.Next(2);
                

                if (coinFlip == 0)
                {
                    //Print user's new randomized input
                    Console.WriteLine($"{userInput.ToUpper()[i]}");
                }

                else if (coinFlip == 1)
                {
                    //Print user's new randomized input
                    Console.WriteLine($"{userInput.ToLower()[i]}");
                }
                
               
               
            }
            Console.ReadLine();
        }
    }
}

I also realized it works better inside visual studio than when it is already a .exe and I tried making it print the whole sentence in one line but I only was able to do it with the command Console.Write(); and when I did it, even though it worked the program wasn’t able to make the phrase change between lowercase and uppercase, if there’s a way to make it print all the phrase in one line and work perfectly I would like to know it.

Hello, @cloud2880584360.

Welcome to the forum.

I have a couple of suggestions for you. To get your output on a single line, change Console.WriteLine to Console.Write. Also, since there are only 2 possible values for coinFlip, there is no need for else if. You can just use else. Lastly, you only need your first using System statement. The other using statements, I believe, are unnecessary. Otherwise, great job!