• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Tuesday, August 11, 2020

How to Reverse a String in C#

 Admin     August 11, 2020     C#, Interview Question, Programming, Programs     No comments   

In this article, I am going to discuss How to Reverse a String in C# with and without using built-in Methods. Please read our previous article where we discussed Character Occurrence in a String in C# program with some examples. As part of this article, we are going to use the following three approaches to reverse a string C#.
  1. Using For Loop
  2. Using For Each Loop
  3. Using the built-in Reverse method of Array class
Program Description:
Here, we will take the input as a string from the user, and then we will convert that string in reverse order as shown in the below image.
How to Reverse a String in C# with Different Mechanisms:

Reverse a String in C# without using Built-in method:
In the below program, we are using for loop to reverse a string.
using System;
namespace LogicalPrograms
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a String : ");
            string originalString = Console.ReadLine();
            string reverseString = string.Empty;
            for (int i = originalString.Length - 1; i >= 0; i--)
            {
                reverseString += originalString[i];
            }
            Console.Write($"Reverse String is : {reverseString} ");
            Console.ReadLine();
        }      
    }
}
Output:
Reverse a String in C# without using Built-in method

Using the for-each loop to reverse a string in C#:
In the following example, we use for each loop to reverse a string in C#.
using System;
namespace LogicalPrograms
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a String : ");
            string originalString = Console.ReadLine();
            string reverseString = string.Empty;
            foreach (char c in originalString)
            {
                reverseString = c + reverseString;
            }
            Console.Write($"Reverse String is : {reverseString} ");
            Console.ReadLine();
        }      
    }
}
Reverse a String Using in-built Reverse Method in C#:
In the following example, we use the built-in Reverse method of the Array class to reverse a string.
using System;
namespace LogicalPrograms
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a String : ");
            string originalString = Console.ReadLine();
            char[] stringArray = originalString.ToCharArray();
            Array.Reverse(stringArray);
            string reverseString = new string(stringArray);
            
            Console.Write($"Reverse String is : {reverseString} ");
            Console.ReadLine();
        }      
    }
}
In the next article, I am going to discuss how to reverse each word in a given string in C# using different mechanisms. I hope now you understood how to reverse a string with and without using any built-in method in C#.

Summary:
I hope this post will be helpful to understand How to Reverse a String in C#
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

0 comments:

Post a Comment

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • ASP.NET State Management
    State management is a technique or way to maintain / store the state of an Asp.Net controls, web page information, object/data, and user in ...
  • Anonymous Types in C#
    Hi friends! Today we are going to learn about Anonymous Types in C#. Let's start with the Introduction Anonymous is a type that does...
  • Usability of SecureString object in C#
    Introduction Hi friends! In this blog we will be discussing a very interesting as well as useful topic in C# and that is Securestring objec...
  • Filters in ASP.Net MVC
    Hi friends! Today we are going to discuss about using a very important feature of MVC i.e. “Filters“ . Filters are a unique feature of Asp...
  • Application Life cycle of Asp.Net MVC
    Today i am going to tell you the Application life cycle of an Asp.Net MVC Application, this post is important for those who are new to Asp.N...
  • OOPS – Difference Between Class and Struct
    Hi friends! Today we are going to discuss a very quite interesting and important topic of Object Oriented Programming(OOPs) i.e. “Classes an...
  • C# Programming Interview Questions
    Today i am going to tell you some of the basic programming questions that are frequently asked in interview for fresher and experienced as w...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape