-6
  string value = "test";             
         for (int i = 0; i < value.Length; i++)
         {                
             Console.WriteLine(value[i]);

         }

output of the program is:

t
e
s
t

I want the output would be "test"

Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
Badruzzaman
  • 41
  • 1
  • 7

4 Answers4

4

Use Console.Write()

string value = "test";             
     for (int i = 0; i < value.Length; i++)
     {                
         Console.Write(value[i]);

     }
Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
2

No need for a loop at all:

string value = "test"; 
Console.WriteLine(value);
sstan
  • 35,425
  • 6
  • 48
  • 66
0

The reason as to why your output is printing on a different line is because of Console.WriteLine..

That needs to be changed to Console.Write

so the in the body of your for loop:

Console.Write(value[i]);
Grizzly
  • 5,873
  • 8
  • 56
  • 109
0

Maybe use Char Array instead of String, if you want to concentrate on each character.
But I would like to join my previous speakers to use

 Console.Write(value);

Please keep in mind, that your string has already the value test and there is no need to iterate through each char.