Question

I'm trying to write a program in C# console that will basically have the user enter a sentence with each word separated by a comma then it will have the output on the console display each word individually. Here is an example of what I am trying to do.

Please enter a sentence separated by commas:
Hello, my, name, is, john

then the output would look like this

Hello, my, name, is, john
my, name, is, john
name, is, john
is, john
john

This is a homework assignment but class was cancelled on this day so we never got a lesson on this.

According to my reading you have to use the index of method, but so far its just printing out the index number and not the actual words. Here is what i got so far, Its pretty bare but I am just getting started.

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

namespace Chapter_16_Sample_1
{
    class Program
    {
        static void Main(string[] args)
        {
            string sentence;


            Console.WriteLine("Please enter four words seperated by a comma");
            sentence = Console.ReadLine();

            int first = sentence.IndexOf(",");

            Console.WriteLine("{0}", first);
            Console.ReadLine();
        }
    }
}

Like I said it does give my the correct index number of the first comma but if I can figure out how to pull out the entire word I think I can figure this assignment out.

Was it helpful?

Solution

You are getting the index of first occurrence of comma, then displaying it in the console.

You need String.Substring method to get a substring from the user input.Substring method takes index as parameter and then returns the substring starting from that index until the end of your string unless you provide a count parameter.

For example, this code will display , John in the console:

string input = "Hello, John";
int index = input.IndexOf(',');
Console.WriteLine(input.Substring(index));

As you can see it also includes the character at the starting index (comma in this case, in order to avoid this you can just use input.Substring(index + 1) instead of input.Substring(index)

Now, if we return your question, you can use a while loop, and then get the substring starting the first comma,display it and update the value of userInput on each iteration.And also use a variable to hold index of the first comma in your string, so when it becomes -1, you will know that there is no comma exists in your string, then you break the loop and display the last part:

string userInput = Console.ReadLine();
int index = userInput.IndexOf(','); // get the index of first comma

while (index != -1) // keep going until there is no comma
{
    // display the current value of userInput
    Console.WriteLine(userInput); 

    // update the value of userInput
    userInput = userInput.Substring(index + 1).Trim(); 

    // update the value of index
    index = userInput.IndexOf(',');
}
Console.WriteLine(userInput); // display the last part

Note: I have also used String.Trim method to remove trailing spaces from the userInput.

OTHER TIPS

You can do it like this:

  • Read the sentence.
  • The idx variable will hold the position of the last comma that was retrieved by IndexOf. You can use the overload of IndexOf that let you specify the start index of the search for the next comma. IndexOf will return -1 when it can't find the value, that's the condition to stop the loop.
  • Last step is use the method Substring to print the substring from the position idx+1 to the end of the string.

The code is this. Please try to understand it, you won't learn anything if you just copy paste this.

Console.WriteLine("Please enter four words separated by a comma");

string sentence = Console.ReadLine();

Console.WriteLine(sentence);

int idx = -1;

while (true)
{
    idx = sentence.IndexOf(',', idx + 1);
    if (idx == -1) break;
    Console.WriteLine(sentence.Substring(idx + 1));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top