---
title: "How to swap neighbor characters in string? e.g. string “DEMO” would be: EDOM"  
description: "How to swap neighbor characters in string? e.g. string “DEMO” would be: EDOM"  
author: "Ahnkar Misra"  
published: 2023-02-02  
updated: 2023-02-02  
canonical: https://www.mindstick.com/forum/157370/how-to-swap-neighbor-characters-in-string-e-g-string-demo-would-be-edom  
category: "c#"  
tags: ["c#", "java", "programming language"]  
reading_time: 1 minute  

---

# How to swap neighbor characters in string? e.g. string “DEMO” would be: EDOM

How to [swap](https://www.mindstick.com/forum/34027/how-to-swap-the-values-of-two-columns-in-sql-server) neighbor [characters](https://answers.mindstick.com/qa/42112/who-is-the-originator-of-avengers-characters) in [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp), e.g. string “[DEMO](https://www.mindstick.com/blog/124848/hp-hpe0-j57-2019-cheat-sheet-exam-questions-tips-to-pass)” would be: EDOM

## Replies

### Reply by Ravi Vishwakarma

Following is the simple C# program to swap neighbor characters in a given string,

```cs
using System;
using System.Text;
namespace HelloWorld
{
 class Program
 {
   static void Main(string[] args)
   {
     Console.WriteLine(Program.SwapNeighbourChar('DEMO'));
     Console.ReadLine();
   }
   public static string SwapNeighbourChar(string strToSwap)
   {
       char[] arrStr = strToSwap.ToCharArray();
       StringBuilder strbuild = new StringBuilder();
       for (int i = 0; i <= arrStr.Length - 1; i++)
       {
           if (i != arrStr.Length - 1)
           {
               strbuild.Append(arrStr[i + 1]);
           }
           strbuild.Append(arrStr[i]);
           i = i + 1;
       }
       return strbuild.ToString();
   }
 }
}

Output: EDOM
```


---

Original Source: https://www.mindstick.com/forum/157370/how-to-swap-neighbor-characters-in-string-e-g-string-demo-would-be-edom

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
