0

firstly i would like to say sorry because of very easy question for you guys , as i am beginner , i am trying to write a program to take input in a string and reverse it in new string , so i can compare if both strings are same than it is palindrome..

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

    namespace Assg2_question1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string word, reverse ="";
                Console.WriteLine("type any word to check if it palindrome or not");
                word =  Console.ReadLine();
                int lengthOfWord, i;
                lengthOfWord = word.Length;
                for(i=lengthOfWord ; i>=1 ; i-- )
                {
                    reverse = word[lengthOfWord];
                }
            }
        }
    }

i have two string word and reverse , when i try to reverse the string it shows error on reverse = word[lengthOfWord]; can not implicitly convert type char to string, why is that ? because i never use char in my program

faizan
  • 109
  • 7
  • `reverse` is a string variable while `word[lengthOfword]` returns a character at a specific index in the word string. So here conversion from char to string is not possible. that's what the error is about. – Chetan Mar 27 '17 at 10:56
  • You use char. Your word[lengthOfWord] returns single character which is a char. To see it better you can do `char letter = word[lengthOfWord]; reverse = letter.ToString()` – FCin Mar 27 '17 at 10:56
  • Possible duplicate of [Best way to reverse a string](http://stackoverflow.com/questions/228038/best-way-to-reverse-a-string) – ASh Mar 27 '17 at 11:00
  • string word = string.Empty; string reverse = string.Empty; Console.WriteLine("type any word to check if it palindrome or not"); word = Console.ReadLine(); var sb = new StringBuilder(); for (var i = word.Length - 1; i >= 0; i--) { var ch = word[i]; sb.Append(word[i]); } reverse = sb.ToString(); Console.WriteLine(word); Console.WriteLine(reverse); Console.ReadKey(); } – Weggo Mar 27 '17 at 11:03

4 Answers4

2

You need a StringBuilder. Also your array bounds checking was messed up here. Array index starts from 0 and word.Length - 1 is the last index:

string word , reverse = "";
Console.WriteLine("type any word to check if it palindrome or not");
word = Console.ReadLine();
int lengthOfWord, j;
StringBuilder sb = new StringBuilder();
lengthOfWord = word.Length - 1;
for (j = lengthOfWord; j >= 0; j--)
{
    sb.Append(word[j]);
}
reverse = sb.ToString();

You got the error because word[j] is a char and not a string. Also string is immutable better use StringBuilder.

Sadique
  • 22,572
  • 7
  • 65
  • 91
1

You can also reverse a string using LINQ:

var reverse = new string(word.ToCharArray().Reverse().ToArray());

If a for loop is your thing then this will work:

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

Note the difference between your code :
reverse = word[lengthOfWord] versus reverse += word[i]
The former assigns each time the last character, while the latter adds each seperate character to the string.

But in that case, using a stringbuilder is more efficient:

var sb = new System.Text.StringBuilder();
for (int i = word.Length - 1; i >= 0; i--)
{
    sb.Append(word[i]);
}
var reverse = sb.ToString();

That's because String is an immutable object. So whenever the content of a string is modified, it will actually allocate a new string in memory. Not very efficient when that's done a lot, like in a loop.
But by using StringBuilder the content of the object can be changed without allocating new memory each iteration.

LukStorms
  • 28,916
  • 5
  • 31
  • 45
0

Your variable word is a String. And a String is a Char array. Because it's an array you can acces it via [index] and that's what you are doing. So when you use word[lengthOfWord] you are getting a Char. And a Char can not be implicitily converted as a String.

But when you want to reserve a String your logic isn't correct at all.

Mighty Badaboom
  • 6,067
  • 5
  • 34
  • 51
  • reverse = reverse + word[lengthOfWord]; – faizan Mar 27 '17 at 10:57
  • i edited this after a dry run to hold the previous character , but now it shows error Index was outside the bounds of the array. – faizan Mar 27 '17 at 10:58
  • "So when you use `word[lengthOfWord]`" not really doing so results in an out of bound exception. May it would be worth pointing out that OP is not using the correct index `i`. – Mong Zhu Mar 27 '17 at 11:05
  • @MongZhu That's why I wrote my last sentence. But I thought it would be helpfull for him to know why his code won't complie in the first way so that the could try the next steps on his own. – Mighty Badaboom Mar 27 '17 at 11:09
  • if you ask me that you tried to add to 3 and 5 but you get the result of -2, and I answer this with your last sentence. Will you know how to correct your calculation? – Mong Zhu Mar 27 '17 at 11:22
0

Use string builder:

    static void Main(string[] args)
    {
        string word = string.Empty; 
        string reverse = string.Empty;
        Console.WriteLine("type any word to check if it palindrome or not");
        word = Console.ReadLine();
        var sb = new StringBuilder();
        for (var i = word.Length - 1; i >= 0; i--)
        {
            var ch = word[i];
            sb.Append(word[i]);
        }
        reverse = sb.ToString();
        Console.WriteLine(word);
        Console.WriteLine(reverse);
        Console.ReadKey();
    }
Weggo
  • 138
  • 2
  • 11