+ 1

C# practice - What am I doing wrong?

I'm a total beginner in programming - doing it as a hobby. Started learning C#. I tried writing a very basic program that prints out my first name "Tom" in capital letters and my last name "Eckert" in small case letters. I managed to have it write my last name in small case but somehow it also writes my full name in capitals instead of only my first name, and I don't seem to understand what I'm doing wrong. Help would be appreciated! Here's my code: using System; namespace Review { class Program { static void Main(string[] args) { string myName = "Tom Eckert"; int firstNamePosition = myName.IndexOf("Tom"); int lastNamePosition = myName.IndexOf("Eckert"); string firstName = myName.Substring(firstNamePosition); string lastName = myName.Substring(lastNamePosition); string firstNameUpper = firstName.ToUpper(); string lastNameLower = lastName.ToLower(); Console.WriteLine(firstNameUpper + " " + lastNameLower); } } }

15th Aug 2021, 9:35 AM
Tom Eckert
Tom Eckert - avatar
3 Réponses
+ 3
Tom Eckert There is problem in Substring of 1st name so do this: string firstName = myName.Substring(firstNamePosition, lastNamePosition - 1); There is another way using Split method: https://code.sololearn.com/ccpuVoLbBBZS/?ref=app
15th Aug 2021, 10:07 AM
A͢J
A͢J - avatar
+ 3
Awesome! I was actually learning through Codecademy and they somehow didn't teach the option of defining the end position. Thanks for your help! It works :)
15th Aug 2021, 10:35 AM
Tom Eckert
Tom Eckert - avatar
+ 2
Tom Eckert nice hobby. You search for the first name position and then copy the whole string to the firstName. You need to set the last letter of firstName for example as follows: string firstName = myName.Substring(firstNamePosition,lastNamePosition-1); Happy coding! https://code.sololearn.com/cratXi047kXQ/?ref=app
15th Aug 2021, 10:01 AM
JaScript
JaScript - avatar