locked
Double Quotation Marks Around Each Word in a String RRS feed

  • Question

  • I am trying to write a console application that places double quotation marks around each word in a string.

    For example, if someone inputs "The red train is heading east" in the console window, then, the output should be "The" "red" "train" "is" "heading" "east"

    My code so far is:

    Only the first word shows up. Any help is much appreciated. I've been working on this one problem for four hours.

     
    Thursday, May 3, 2012 9:38 AM

Answers

  • The reason that you are only seeing the first word is becuase as soon as you process the first word, you exit your loop with goto start.  I won't lecture about using goto, but let's just say that it is almost always a bad idea.  Additionally, you never put in the double quotes and all spaces are being omitted.  Here is your code with my minimal suggestions:

      static void Main(string[] args)
      {
      start:
       string myString;
       char[] separator = { ' ' };
       Console.WriteLine("Type any string please");
       myString = Console.ReadLine();
       string[] myWords;
       myWords = myString.Split(separator);
    
       foreach (string word in myWords)
       {
        Console.Write("\"{0}\" ", word);
       }
    
       Console.WriteLine();
       Console.ReadKey();
       Console.Clear();
    
       goto start;
      }
    


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    • Marked as answer by latsushi Thursday, May 3, 2012 11:11 PM
    Thursday, May 3, 2012 11:59 AM

All replies

  • hii try this

     var str="HIII Sandeep Welcome Back";
    
                var x = str.Split(' ').Select(a => "\"" + a + "\"");
                foreach(var s in x)
                {
                    Console.WriteLine(s);
                }


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    Thursday, May 3, 2012 9:49 AM
  • The reason that you are only seeing the first word is becuase as soon as you process the first word, you exit your loop with goto start.  I won't lecture about using goto, but let's just say that it is almost always a bad idea.  Additionally, you never put in the double quotes and all spaces are being omitted.  Here is your code with my minimal suggestions:

      static void Main(string[] args)
      {
      start:
       string myString;
       char[] separator = { ' ' };
       Console.WriteLine("Type any string please");
       myString = Console.ReadLine();
       string[] myWords;
       myWords = myString.Split(separator);
    
       foreach (string word in myWords)
       {
        Console.Write("\"{0}\" ", word);
       }
    
       Console.WriteLine();
       Console.ReadKey();
       Console.Clear();
    
       goto start;
      }
    


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    • Marked as answer by latsushi Thursday, May 3, 2012 11:11 PM
    Thursday, May 3, 2012 11:59 AM
  • Only the first word shows up.

    That's because you goto start after the first word. If you want to restart the whole Main method after the loop, then do it after the loop, not inside it.

    Thursday, May 3, 2012 12:00 PM
  • Hi,

    In addition to Louis.fr, you could have easily seen this using the VS debugger by stepping through the code (see http://csharp.net-tutorials.com/debugging/stepping-through-the-code/)


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Thursday, May 3, 2012 12:37 PM
  • Thank you for your help. I had previously put in the double quotes with 

    ("\"{0}\" ", word); but the whole phrase was surrounded with quotes instead of each word being surrounded with quotes. I kept experimenting with

    the code but I never bothered to move trap in its own loop. I also never took the goto statement out of the loop.

    Thanks again :-)

    Thursday, May 3, 2012 11:14 PM