FAQ: Method Calls and Input - Capture Output

This community-built FAQ covers the “Capture Output” exercise from the lesson “Method Calls and Input”.

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

Learn C#

FAQs on the exercise Capture Output

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!
You can also find further discussion and get answers to your questions over in Language Help.

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

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

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!

This exercise is working fine so far.

1. The designer of C# is "Anders Hejlsberg" . His first name is nice, but we want to print the second name alone.
First, find the index of the space ( " " ) in the string designer and store it in a variable indexOfSpace .

2. Use Substring() and the variable indexOfSpace to get the second name. Store the returned value in a variable secondName .

The only thing I can recommend as a good practice is this:
Once you have declared the string indexOfSpace, the following would be much better when you don’t actually know how long the second string you want is:

designer.Substring(indexOfSpace, designer.Length - indexOfSpace);

The full length of the variable designer is 16.
In a case where we again don’t know how long the other string is, subtracting the index of the space (which is 6) from the full length: 16 - 6 = 10. This is useful for the program to know how far to go from the start (which is indexOfSpace)

Then, the .substring() method takes the second string starting from indexOfSpace, and ending at 10, and finally returns the wanted value. Using the above method works dynamically for any two strings separated by a space.

Output:

Hejlsberg

9 Likes

That was incredibly useful, thank you.
I am not convinced that this course explains the syntax very well.
Also the course progresses even if the code is wrong.

5 Likes

This was really helpful, thank you

That’s one of the problems i have been experiencing.

Struggled with this exercise (3. Capture Output 3/10).
Point 1 completed with a tick, but gave errors on the output window.
I could not get past point 2.
When I viewed the solution, i had missed the int from the variable.
I didn’t find the hints very helpful.
I currently have low confidence in learning C#.

2 Likes

C# is more challenging to learn than Python, say. See if this helps:

    static void Main(string[] args)
    {
      string designer = "Anders Hejlsberg";
      int indexOfSpace = designer.IndexOf(" ");
      string secondName = designer.Substring(indexOfSpace);
      Console.WriteLine(secondName);
      
      string designBy = "Polo by Ralph Lauren";
      int indexOfBy = designBy.IndexOf(" ");
      string byRalphLauren = designBy.Substring(indexOfBy);
      Console.WriteLine(byRalphLauren);
      Console.WriteLine(designBy.Substring(indexOfBy, 3));
      Console.WriteLine(designBy.Substring(indexOfBy-1, 4));
    }

I’ll file a bug report on the content because the output is a little misleading to learners. This is an observation and not a knock on the lesson writers. Notice that when you add the lines of code, you’ll see that a blank space precedes the last name Hejlsberg and the word by in the designBy string.

Another point: The String.Substring() has an optional parameter to give the length of the string you want output. If this length number takes the string past its end, you’ll see an error message.

If you know Python, see if you can reintrepret the code into Python – at least by using string_name.index(" ") or string_name.index("by").

2 Likes

Agreed!

Thank you for providing a solution WITH an easy-to-understand, useful description.

Perfect!

I think the easiest way to make it is following
using System;

namespace CaptureOutput
{
class Program
{
static void Main(string args)
{
string designer = “Anders Hejlsberg”;
int secondName = designer.IndexOf(“Hejlsberg”);
Console.WriteLine(designer.Substring(secondName));

}

}
}
These are just enough

The exercise progresses even if the code is wrong. For instance, I made the mistake of forgetting to declare the variable type: it still worked.

Hi guys, I noticed the “correct” solution is with a trailing space before the last name.
string secondName = designer.Substring(indexOfSpace);
Console.WriteLine("–" + secondName + “–”);

Outputs:
– Hejlsberg– (notice the trailing space)

I.M.O. it should be :
string secondName = designer.Substring((indexOfSpace + 1));

why we use designer.IndexOf(" ") not designer.IndexOf(“Hejlsberg”). Because both give same answer, which one is right. In previous lessons of .Indexof it states to use character or string we need as output between quotations. But how can it store correct string in the variable only by using space between ( " " ). What if there are paragraphs instead of two words. Can someone explain! Thanks

1 Like

i think that’s its the easiest way too and readable.

.IndexOf stores the first occurrence of a character in a string. Since in the string “Anders Hejlsberg” there is only one space, that’s the one that is stored.
In case you would need to find all spaces (or all occurrences of any character) in a larger string, you would probably have to use another method and/or “for” loops.
You can see some examples here or here.

1 Like

Here’s my solution:

string designer = "Anders Hejlsberg";
int indexOfSpace = designer.IndexOf(" ");
string secondName = designer.Substring(indexOfSpace+1);
Console.WriteLine(secondName);

Thanks to @harrjt for pointing out the empty space which gets printed in case only indexOfSpace is taken as a parameter for the Substring method. I added the +1 so that the substring starts from the next character, which is H.

2 Likes

As much as this makes sense, in this case Substring() doesn’t need the other output as it takes the first value as a starting point and returns all chars until the end of the string.
This:

designer.Substring(indexOfSpace); 
//returns the exact same output as 
designer.Substring(indexOfSpace, designer.Length - indexOfSpace);
//even if we had two surnames like "Hejsberg Somextrasurname" , it would return both
//so the second argument you recommend is redundant in this case
1 Like

Yess, I wanted to add this, good to point it out :slight_smile:

Hello :slight_smile:
I’ll try to be as blatant as possible… In real life when you start to make apps, you will know where exactly to expect what type of input. So at some fields you know a user will enter his name and surname for example and in the other one his phone number. So you know by default to expect at least one spacing (the one between name and surname). You will not know the names of your users and ur program is not supposed to work or anticipate everyone’s name and surname like that… but what it can anticipate is a string that contains spacings. And like this you will always separate his name from the last name and you can then store them separately. So, for example, your app will then greet them like “Hello Name!” and not “Hello Name MiddleName MaidenName LastName”.

1 Like

Thanks for the information levyrigor :slightly_smiling_face:

I also agree that this was not explained before and also confused.