0

I'm doing practice for C#, and the problem requires me to make a program that reverses words. I have the answer, but in my search for a solution, I got a weird output that doesn't make sense.

This is the code that leads to the weird output:

using System;
namespace stars
{
    public class Reverse
    {
        public void SpinWords(string sentence)
        {

            int end_index = sentence.Length - 1;
            string rev_sentence = "";
            for (int i = end_index;  i >= 0; i--)
            {
                string letter = sentence.Substring(i, 1);
                rev_sentence = string.Concat(rev_sentence, i);
                Console.Write(rev_sentence);


            }


        }
    }
}

which has a large random numerical output, whereas I would have expected actual letters to come up.

and incase you were wondering this was my solution for a correct output:

using System;
namespace stars
{
    public class Reverse
    {
        public void SpinWords(string sentence)
        {
            //Obtain the largest index in sentence 
            int end_index = sentence.Length - 1;

            //Place letters backwards one-by-one to reverse string
            for (int i = end_index;  i >= 0; i--)
            {
                string letter = sentence.Substring(i, 1);
                Console.Write(letter);


            }


        }
    }
}
ajdawg
  • 17
  • 1
  • 1
    Can you share the sample input and the output you are getting? And what is the expected output? – Chetan Nov 13 '19 at 05:15
  • 2
    `rev_sentence = string.Concat(rev_sentence, i);` concatenates `rev_sentence` & `i`. You're just concatenating your loop control variable & printing the result every iteration of the loop. This should mean that the result isn't random, eg. `SpinWords("Result");` should print "554543543254321543210" – ItsPete Nov 13 '19 at 05:16
  • Note that, to correctly reverse a string, Unicode surrogate pairs and combining characters must get grouped correctly and reversed as a single unit, leaving the order within the group unchanged. To do this see [this answer](https://stackoverflow.com/a/15111719/3744182) to [Best way to reverse a string](https://stackoverflow.com/q/228038/3744182). – dbc Nov 13 '19 at 07:30

3 Answers3

1

C# have already Reverse method but if you want you can create easly own Reverse method. Of course there are many ways in C#

var word = "abcde";
string.Join("",word.Reverse());
string.Join("", word.OrderByDescending(e=> e);

or

var reversedWord = "";
for (var i = word.Length; i > 0; i--)
{
    reversedWord += word[i - 1];
}

or

var charCount = word.Length;
while (0 != charCount--)
{
    reversedWord += word[charCount];
}
1

This is the way to reverse each character is as below :

public static string Reverse( string s )
{
    char[] charArray = s.ToCharArray();
    Array.Reverse( charArray );
    return new string( charArray );
}

This is the way to reverse each word in sentence is as below :

var reversedWords = string.Join(" ",
      str.Split(' ')
         .Select(x => new String(x.Reverse().ToArray())));
Srusti Thakkar
  • 1,835
  • 2
  • 22
  • 52
1

There are two reasons for getting large random numerical output.

First, you are concatenating the rev_sentence with index, which should be replaced by the letter string.

rev_sentence = string.Concat(rev_sentence, letter);

Second, you are printing the output inside the for loop, which will produce repetitive output for every concatenation.

The Following should work as expected

for (int i = end_index; i >= 0; i--)
{
    string letter = sentence.Substring(i, 1);
    rev_sentence = string.Concat(rev_sentence, letter);
}
Console.Write(rev_sentence);
Mahbub Moon
  • 491
  • 2
  • 8