-4

The output needs to have spaces between each character so it should be like '? e m a n r u o y s i t a h W' and not like "emanruoysitahw'. How do I do this with the code I have already written. Thanks for any help.

using System;


namespace strings
{
    class Program
    {
        static void Main(string[] args)

        {
           string inString= "What is your name?",outString="";

           foreach(char c in inString)
           {
           outString = c + outString;
           }
          {
          Console.WriteLine("" + outString);
          }

        }
    }
}
reg
  • 43
  • 1
  • 3
  • 6
    What have you tried so far? We help with problems in your code and not taking your requirements and writing the code for you. – Chris Nov 03 '14 at 16:59
  • possible duplicate of [Alternatives to " " for creating strings containing multiple whitespace characters](http://stackoverflow.com/questions/1716230/alternatives-to-for-creating-strings-containing-multiple-whitespace-characte) – JMK Nov 03 '14 at 17:00
  • I have googled my it but I can't find anything that works. – reg Nov 03 '14 at 17:00
  • 2
    @reg Sure looks like homework to me. It's a good idea to make an attempt at solving your problem rather than just asking people to solve it for you. You'll learn better that way (whether it's homework or not). – mason Nov 03 '14 at 17:03
  • I think we should all help newbies and I rarely downvote. But this is clearly homework. Shame on you for posting this! – Unknown Coder Nov 03 '14 at 17:04
  • @reg Look into putting your characters into an array and then [Join](http://msdn.microsoft.com/en-us/library/57a79xd0%28v=vs.110%29.aspx) it with space as the separator as one possible approach. – Filburt Nov 03 '14 at 17:07
  • @reg: You have outstring that you are adding to character by character. Why not just add your spaces there too? I'm confused as to whether there is something I missed that means this obvious solution won't work... – Chris Nov 03 '14 at 17:09
  • Also can you clarify your requirement of "without using the space bar". Do you mean you aren't allowed to have the character `' '` in your code anywhere or just that you don't want to put the spaces in the original string? – Chris Nov 03 '14 at 17:11

2 Answers2

7

You could:

 outString = string.Join(" ", input.ToCharArray());
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • 1
    +1. (not very serious comment) The only possible improvement to match "no spacebar" could be to replace `" "` with `"\x20"` :) - than whole program can be typed without ever pressing space. – Alexei Levenkov Nov 03 '14 at 17:22
1

The improper (and inefficient) way most of us use:

outString += c + "\x26";

The proper (yet inefficient) way most of us dont't use:

outString.Insert(c + "\x26");

The proper and efficient way smart programmers use

var s = new StringBuilder();
s.append(c + "\x26");
outString = s.ToString();

Wait. You don't have a space bar do you? Use this:

StringBuilder(s)=new
StringBuilder();
s.append(c+"\x26");
outString=s.ToString();

How on earth is this a real life situation anyway?

DividedByZero
  • 4,333
  • 2
  • 19
  • 33
  • 2
    I'd probably avoid the string concatenation if I was going down that route in favour of `StringBuilder`, particularly as the string has an indeterminate length... – Joe Nov 03 '14 at 17:03
  • It's a good idea *not* to answer right away if it looks like a homework question. Instead, you should make sure they've made an attempt to solve the problem before just explaining how to do it. – mason Nov 03 '14 at 17:04
  • @Joeb454 I agree with you, but the OP doesn't know how to add a **space** to a string.. – DividedByZero Nov 03 '14 at 17:07
  • I reckon you probably used the space bar though I will concede that you may have just copy and pasted the space from somewhere else... :) – Chris Nov 03 '14 at 17:12
  • @mason this is just flat out not a good answer (also not completely bad, so no -1): building long string with string concatenation is just bad idea. – Alexei Levenkov Nov 03 '14 at 17:18
  • @Chris you can also do alt + 255 for a space. That way you don't press space bar. – deathismyfriend Nov 03 '14 at 17:20
  • @deathismyfriend: alt+32 would be a more traditional space I believe (255 being a non-breaking space I believe). – Chris Nov 03 '14 at 17:27
  • 1
    @Chris No need for the space bar anymore :) – DividedByZero Nov 03 '14 at 18:01
  • @RandomUser: Did you mean `\x20` in that code? Also you can use line a line break between `new` and `StringBuilder` instead of a space if you wanted. OtherwiseGoodJob.ILikeIt.:) – Chris Nov 03 '14 at 23:37