Click here to Skip to main content
15,912,756 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This code should be get a string then return reverse that string in output but it has problem I want to know can you check it and say wat should I do ecxatlly,thank u so much.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace prog_83_reshteh_method_
{
    class Program
    {
        public void reshteh(string x)
        {
            Console.WriteLine("enter a string");
            string str = Console.ReadLine();
            int y= Convert.ToInt32(str);

            for (int i = str.Length - 1; i >= 0; i--)
            {
                Console.WriteLine(str.Substring(i,1));
            }

        }
        static void Main(string[] args)
        {
            Console.WriteLine("enter a string");
            string str = Console.ReadLine();
            int y = Convert.ToInt32(str);

            Program obj = new Program();
            obj.reshteh(x);

        }
    }
}
Posted
Updated 30-May-11 8:59am
v2
Comments
[no name] 30-May-11 15:00pm    
The subject line is for a short description, not for the question. I've edited this for you.
shohreh25 30-May-11 15:07pm    
thank u

[no name] 30-May-11 15:02pm    
And what problem does it have? You need to give complete details if you expect answers


  1. Consider using an extension method[^] on the string called Reverse(), if you are using .net 3.5 or above. If below 3.5 create a method string Reverse(string original) {...}
  2. As for the alogrithm "my string"[1] returns the character 'y'. So all you have to do is to iterate over the array, if you iterate forwards you need to fill the built string backwards and vice versa.
  3. How you build the string is up to you, you could use a StringBuilder or just add to an existing string or even create an reversed array of char and create a string from that, depends what you need.
 
Share this answer
 
There are many things wrong here.

1) Duplicate code: If you want your reshteh method to work using the "x" argument, then remove the following code block from the reshteh method and update the for block to use the x variable instead of the str variable.
C#
Console.WriteLine("enter a string");
string str = Console.ReadLine();
int y= Convert.ToInt32(str);

for( int i = x.Length -1; i >= 0; i-- ) ...
 
Share this answer
 
A string is simply an array of characters, there is no need to use SubString for this.

C#
public void reshteh(string x)
{
  Console.WriteLine("enter a string");
  string str = Console.ReadLine();

  for (int i = str.Length; i >= 0; i--)
  {
    Console.WriteLine(str[i]);
  }

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900