CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Feb 2015
    Posts
    11

    Question Can someone PLEASE help me in displaying reverse of string input value without...

    Someone please help me write a code which displays reverse of input(string) value without using any class library/builtIn functions.

    for example,

    String s= reverseString("hello");

    //I want to have this "hello" to be print out/output backwards. So we need to create a reverseString's function.

    //I have tried with this below code:

    String s= reverseString("hello");
    string reverse;
    int i, j;
    String reverseString(string str)
    {
    char[] c=new char[str.char()];
    for(int i=c.length-1, i>-1, i--)
    {
    reverse=j[i];
    return reverse;
    }
    Console.WriteLine("Reverse of your input string is:" + j[i]);
    }

    //I am not sure about this code, do not understand clearly, please help me in this, will appreciate it. Thank you.

  2. #2
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    94

    Re: Can someone PLEASE help me in displaying reverse of string input value without...

    you can index a string like a array
    it counts from the end of the string to the beginning
    but adds to the character array from start to end

    for example
    pass your string to this method
    Code:
            public void ToConsoleNumericalString(string s)
            {
                byte[] bytearray = new byte[s.Length];
                int index = 0;
                while (index < s.Length)
                {
                    bytearray[index] = (byte)(s[index]);
                    Console.WriteLine("string index: "+index +" letter: " + s[index]+" byte value= " + bytearray[index]  );
                    index++;
                }
            }
    it outputs...
    string index: 0 letter: h byte value= 104
    string index: 1 letter: e byte value= 101
    string index: 2 letter: l byte value= 108
    string index: 3 letter: l byte value= 108
    string index: 4 letter: o byte value= 111

    another way to do the reverse is like so
    Code:
           public void Test()
           {
               string myString = "hello";
               Console.WriteLine(ReverseString(myString));
           }
            /// <summary>
            /// we didn't check for a null string but this is for a simple example
            /// </summary>
            public string ReverseString(string the_string_to_reverse)
            {
                string reversed_string ="";
                
                // find the index to the last letter in the string array
                int the_index_in_string = the_string_to_reverse.Length -1;
                
                // start from that last index and loop while its zero or more
                while(the_index_in_string >= 0)
                {
                    // add the character at the index into our reversed string
                    reversed_string += the_string_to_reverse[the_index_in_string];
                    
                    // then decrease the index
                    the_index_in_string--;
                }
               
                 // were all done when we get here so return it
                return reversed_string;
            }
    now doing it that way with += string is inefficient for large strings
    that's why he used the character array
    typically you do the same as i showed, just with StringBuilder to avoid garbage
    StringBuilder is in the System.Text class so you have to add that using statement
    then you get something like so

    Code:
            /// <summary>
            /// we didn't check for a null string but this is for a simple example
            /// </summary>
            public string ReverseString(string string_to_reverse)
            {
                StringBuilder r = new StringBuilder();
                int index = string_to_reverse.Length - 1;
                while (index >= 0)
                {
                    r.Append(string_to_reverse[index]);
                    index--;
                }
                return r.ToString();
            }
    Last edited by willmotil; February 19th, 2015 at 09:27 PM.

  3. #3
    Join Date
    Feb 2015
    Posts
    11

    Question Re: Can someone PLEASE help me in displaying reverse of string input value without...

    Hello Willmotil,

    Thank you for the code, appreciate it, but I do like to know how to display the reverse output by using char[]. I want to know a correct code for it. If you know, please help me. Thank you.

  4. #4
    Join Date
    Feb 2015
    Posts
    11

    Re: Can someone PLEASE help me in displaying reverse of string input value without...

    oh, I got it and understood. I went thru your code again with carefully by understanding deeply how it works and I did understood. Thank you so much, I truuuely appreciate it. Actually, this was my interview question! Thank you Willmotill so much!!!...very nice ...neat well explained code!!!...Thank you for your time effort...
    Last edited by Krisna; February 19th, 2015 at 08:47 PM.

  5. #5
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    94

    Re: Can someone PLEASE help me in displaying reverse of string input value without...

    welcome, i edited it a bit for clarity and so you could experiment with it a bit

    to just reverse into a character array you would do this
    but i don't see why anyone would be using a char array with StringBuilder around
    Code:
            public char[] ReverseStringIntoCharArray(string s)
            {
                int len = s.Length;
                char[] r = new char[len];
                int end = len -1;
                for (int i = 0; i < len; i++)
                {
                    r[i] = s[end - i];
                }
                return r;
            }
    
    n that is really just this which is just less readable
    
            public char[] ReverseStringIntoCharArray(string s)
            {
                char[] r = new char[s.Length];
                for (int i = 0; i < s.Length; i++)
                {
                    r[i] = s[(s.Length -1) - i];
                }
                return r;
            }
    with stringbuilder u could just return r then later on Console.Writeline(r); or turn r into a char array
    Last edited by willmotil; February 19th, 2015 at 09:51 PM.

  6. #6
    Join Date
    Feb 2015
    Posts
    11

    Re: Can someone PLEASE help me in displaying reverse of string input value without...

    you are genius. I am so happy)) Thank youuu Willmotil...your mind so intellect! this is very good. I like it and appreciate it...

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,494

    Re: Can someone PLEASE help me in displaying reverse of string input value without...

    Please forward your assignment grade to Willmotil when you get it.

  8. #8
    Join Date
    Feb 2015
    Posts
    11

    Re: Can someone PLEASE help me in displaying reverse of string input value without...

    I am sorry (Arjay) I did not understand what you meant.

  9. #9
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    94

    Re: Can someone PLEASE help me in displaying reverse of string input value without...

    he means "you gonna get fired" when they realize your copying pasting lol

    though seriously
    your not supposed to ask questions for a interview
    your supposed to already know the answers
    Last edited by willmotil; February 19th, 2015 at 11:32 PM.

  10. #10
    Join Date
    Feb 2015
    Posts
    11

    Re: Can someone PLEASE help me in displaying reverse of string input value without...

    ) I am not copying pasting, I actually DID understand your codes. I really wanted to know and learn and matter affect I have another interview tomorrow at Temple University so I wanted to be prepared. Thank you (wilmotil)for teaching me. If you were having an interview instead of me then you would definitely have got the job because this guy asked me JUST/ONLY this question of a reverse string code and I was not sure how to do it so he ended the interview and did not ask any further/other technical questions. So I really wanted to know how this reverse code works. So thank you again.

  11. #11
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    94

    Re: Can someone PLEASE help me in displaying reverse of string input value without...

    well in that case just for the record its not that simple anyways
    see
    R. Martinho Fernandes answer to the question here
    http://stackoverflow.com/questions/2...verse-a-string
    found by simply googling "c# how to reverse a string"

    simple answers like this can actually be found just by googling them and then finding that its not so simple
    Last edited by willmotil; February 20th, 2015 at 12:59 AM.

  12. #12
    Join Date
    Feb 2015
    Posts
    11

    Re: Can someone PLEASE help me in displaying reverse of string input value without...

    Ok. thank you Willmotil, I did open and viewed the stackoverflow.com and it has exactly what I was looking for. thank you so much, I really appreciate it!!!...thank you for your codes too as It was very clear/simple/organized/neat/understandable and I did understand and liked it...very nice/good logic you have applied!!!...At the end you have mentioned 'its not so simple'...what do u mean by that? what is not so simple?

  13. #13
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    94

    Re: Can someone PLEASE help me in displaying reverse of string input value without...

    what do u mean by that? what is not so simple?
    quoted from the same link posted above
    chars in C# are 16-bit UTF-16 code units; a supplementary character is encoded using two of them
    – Bradley Grainger
    below a quote from this msn page found here
    https://msdn.microsoft.com/en-us/library/x2f3k4f6.aspx
    The .NET Framework
    defines a text element as a unit of text that is displayed as a single character, that is, a grapheme.
    A text element can be,... a (base character), a (surrogate pair), or a (combining character) sequence.
    The Unicode Standard http://www.unicode.org/versions/Unicode7.0.0/
    defines a surrogate pair as a coded character representation for a single abstract character that consists of
    a sequence of two code units,
    where,... the first unit of the pair is a high surrogate and the second is a low surrogate.
    The Unicode Standard
    defines a combining character sequence as a combination of a base character and one or more combining characters.
    A surrogate pair can represent a base character or a combining character
    then see the msn example code at the bottom of this link here
    https://msdn.microsoft.com/en-us/lib...vs.100%29.aspx
    Last edited by willmotil; February 21st, 2015 at 12:21 PM.

  14. #14
    Join Date
    Feb 2015
    Posts
    11

    Re: Can someone PLEASE help me in displaying reverse of string input value without...

    Ok.

  15. #15
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,117

    Re: Can someone PLEASE help me in displaying reverse of string input value without...

    Quote Originally Posted by Arjay View Post
    Please forward your assignment grade to Willmotil when you get it.
    Actually he said this was an interview question....I hope his potential employer (at least the person conducting the interview) is a member here on CodeGuru.....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured