Click here to Skip to main content
Click here to Skip to main content

How to reverese a string in C#/.NET with almost one line of code using P/Invoke

, 4 Jan 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
How to reverese a string in C#/.NET with almost one line of code using a build in Windows library function which is available on any Windows system.

Introduction 

This short article shows how to use an exported function from the Microsoft Visual C Run-Time Library wich is almost available on any Windows System starting Windows 95 to the latest Windows 8 to reverse a string in C# within a single line of code.

Background 

At times we need to reverese a string in .NET for particular reasons. In general developers code their own functions and classes holding some sort of class/function pair to accomplish this task, unless there is no ready to use code or external library/assembly available. But the implementation itself can heavily differ in code quality, speed and size, depending on the developers skills and generated code by the compiler. Keep in your mind, that there are many more compilers beside the broadly used Microsoft compilers, who can generate IL code and they can differ in optimization and other variables which finally can affect the applications performance. By using the Microsoft Visual C Run-Time Library functions _strrev() and/or _wcsrev() we can quickly reverse a string within a single line of code. These functions can easily be imported and used by a .NET project using the .NET Framework's Interop techniques. CodeProject has a lot of great articles on the Interop topic, so there is no need to explain what .NET Interop in detail is. By using these two Microsoft supplied functions, there are some major advantages over external third-party libraries: 

  • The Microsoft Visual C Run-Time Library is available without exception on any Windows system starting from Windows 95 to the latest Windows 8, covering the 32- and 64 bit variants. The library itself is de-facto an integral part of the Windows system and many Windows applications itself depend on its existence to work properly.
  • The functions are very well documented and very stable, so they can be safely used in any project without exceptions. Beside the fact that there are different version available and possibly installed on a system, the input and output data is ALWAYS the same on any library version.
  • The library code is maintained by Microsoft and any bugfixes will be available on any Windows systems with new or update releases of the Microsoft Visual C Run-Time Library.
  • The code runs pretty fast. Fast means here that the implementation code, the code inside the library, is written in plain C and the only thing to marshal between the library and the managed application is the string (in fact the string pointer) itself. This can make the reversing operation on even large strings very fast.

Using the code   

Using the code is pretty straightforward and the C# signatures can easily be imported to any .NET language like e.g. VB.NET by simply translating them into their language counterparts. The project files hold a simple C# Visual Studio 2008 file format Forms expample project that shows you a simple application with basic commented code on how to use these two functions. Lets have a look at the code now:

The very first thing we have to do is to import the System.Runtime.InteropServices namespace into our project in order to be able to use the Interop mechanism and use the two functions in our project:

using System.Runtime.InteropServices;      

Now we can declare our import functions signatures inside a class in our project. They have to look like this:

/*  Original documentation for _strrev, _wcsrev, _mbsrev, _mbsrev_l 
 *  on the Micdrosoft MSDN Libary pages: 
 *  http://msdn.microsoft.com/en-us/library/9hby7w40%28v=vs.80%29.aspx
 */

// UNICODE variant
[DllImport("msvcrt.dll",
    CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.LPWStr)]
public static extern string _wcsrev(
    [MarshalAs(UnmanagedType.LPWStr)]
    [In] string str);

// ANSI variant
[DllImport("msvcrt.dll",
    CharSet = CharSet.Ansi)]
[return: MarshalAs(UnmanagedType.LPStr)]
public static extern string _strrev(
    [MarshalAs(UnmanagedType.LPStr)]
    [In] string str);

That's it! Now they can be used like any C# function within your code: Here is a short example to show you how:

public partial class Form1 : Form
{
    /*  Original documentation for _strrev, _wcsrev, _mbsrev, _mbsrev_l 
     *  on the Micdrosoft MSDN Libary pages: 
     *  http://msdn.microsoft.com/en-us/library/9hby7w40%28v=vs.80%29.aspx
     */

    // UNICODE variant
    [DllImport("msvcrt.dll",
        CharSet = CharSet.Unicode)]
    [return: MarshalAs(UnmanagedType.LPWStr)]
    public static extern string _wcsrev(
        [MarshalAs(UnmanagedType.LPWStr)]
        [In] string str);

    // ANSI variant
    [DllImport("msvcrt.dll",
        CharSet = CharSet.Ansi)]
    [return: MarshalAs(UnmanagedType.LPStr)]
    public static extern string _strrev(
        [MarshalAs(UnmanagedType.LPStr)]
        [In] string str);
 
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        // make sure you pass in a valid string
        // passing a null will lead to a System.AccessViolationException exception
        if (string.IsNullOrEmpty(this.textBox1.Text.Trim()) == true)
        {
            MessageBox.Show("The original string cant be empty. Please set the original text first!",
                this.Text,
                MessageBoxButtons.OK,
                MessageBoxIcon.Exclamation);

            return;
        }

        try
        {
            //we are using the UNICODE variant, since all strings in .NET are UNICODE by default
            this.textBox2.Text = _wcsrev(textBox1.Text);
        }
        catch (Exception err)
        {
            MessageBox.Show("Error reversing string: " + err.Message,
                this.Text,
                MessageBoxButtons.OK,
                MessageBoxIcon.Error);
        }
    }
}

Important to know

Please make sure that you pass in a valid string as the functions parameter and not a null. Passing in something like "" or a string.Empty (both are the same at runtime) is perfectly fine, but passing in a null as parameter will definitely lead to a System.AccessViolationException kind of exception and crash your application if you dont handle the exception inside a try/catch block.

History 

  • 03/01/13 - Initial release
  • 04/01/13 - Changed topic text to make it more fit the approach technique

External links  

Original documentation for _strrev, _wcsrev, _mbsrev, _mbsrev_l:
http://msdn.microsoft.com/en-us/library/9hby7w40%28v=vs.80%29.aspx.

License

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

Share

About the Author

Kerem Guemruekcue
Software Developer (Senior)
Germany Germany
I am a former developer from germany who has worked on a broad range of IT technologies and software/hardware related tasks regarding Microsoft Windows and Linux systems. My primary focus and field of application was the development of system software/hardware, troubleshooting on software/hardware, designing and teaching how to write code and build working systems, but i am not tied to anything special at all. Currently i am developing in C, C++, C# and a little in Assembly if there is need for it. Beside that there are lots of other languages and stuff, but nothing really worth to be menitioned here. In my spare time i try to be as much as i can with my family, i love sports and reading. I also try to contribute code and solutions and help wherever i can.

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberfredatcodeproject19-Dec-13 5:22 
SuggestionAfter all, I vote for Microsoft.VisualBasic.Strings.StrReverse(...) PinmemberAndreas Gieriet5-Jan-13 12:16 
SuggestionOther way Pinmember ProgramFOX5-Jan-13 0:42 
GeneralRe: Other way PinmemberAndreas Gieriet5-Jan-13 12:19 
GeneralMy vote of 1 Pinmemberabdurahman ibn hattab4-Jan-13 9:29 
GeneralRe: My vote of 1 PinmemberKerem Guemruekcue4-Jan-13 20:29 
GeneralMy vote of 2 Pinmember GeekBond 4-Jan-13 6:34 
GeneralRe: My vote of 2 PinmemberKerem Guemruekcue4-Jan-13 7:10 
QuestionInterop is a little heavy Pinmemberjfriedman4-Jan-13 5:18 
AnswerRe: Interop is a little heavy PinmemberKerem Guemruekcue4-Jan-13 5:48 
GeneralRe: Interop is a little heavy Pinmemberjfriedman4-Jan-13 6:15 
GeneralRe: Interop is a little heavy PinmemberKerem Guemruekcue4-Jan-13 6:30 
GeneralMy vote of 2 PinmemberJon Andersson4-Jan-13 3:08 
GeneralRe: My vote of 2 PinmemberKerem Guemruekcue4-Jan-13 3:45 
GeneralMy vote of 1 [modified] PinmemberRogerDW4-Jan-13 2:06 
GeneralRe: My vote of 1 PinmemberKerem Guemruekcue4-Jan-13 2:17 
GeneralMy vote of 1 PinmemberKrzysztof Kalinowski4-Jan-13 0:58 
GeneralRe: My vote of 1 PinmemberKerem Guemruekcue4-Jan-13 1:35 
GeneralMy vote of 3 PinmemberAndreas Gieriet3-Jan-13 23:00 
GeneralRe: My vote of 3 PinmemberKerem Guemruekcue4-Jan-13 1:28 
GeneralRe: My vote of 3 PinmemberAndreas Gieriet4-Jan-13 2:08 
GeneralRe: My vote of 3 PinmemberKerem Guemruekcue4-Jan-13 2:29 
GeneralRe: My vote of 3 PinmemberAndreas Gieriet4-Jan-13 2:44 
GeneralRe: My vote of 3 PinmemberKerem Guemruekcue4-Jan-13 3:42 
GeneralRe: My vote of 3 PinmemberAndreas Gieriet5-Jan-13 12:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1411019.1 | Last Updated 4 Jan 2013
Article Copyright 2013 by Kerem Guemruekcue
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid