Posted on by Kalkicode
Code String

Swap characters in string

Here given code implementation process.

//C Program 
//Swap characters in string
#include<stdio.h>

//Swap the string element of given indexes
void swap(char text[],int size,int a,int b)
{
  if((a>=0 && a<size)  && (b >= 0 && b < size))
  {
    //When valid a and b location
    char temp = text[a];
    text[a] = text[b];
    text[b] = temp;
  }

}

int main()
{


  char info[]="ABCDEF";

  //Get the size of string
  int size = sizeof(info)/sizeof(info[0])-1;

  int a = 0;
  int b = 4;
  printf("%s\n",info );
  swap(info,size,a,b);
  printf("%s\n",info );

  return 0;
}

Output

ABCDEF
EBCDAF
//C++ Program 
//Swap characters in string
#include <iostream>
#include <string.h>
using namespace std;
class MyString
{
public:
  //Swap the string element of given indexes
  string swap(string text,int size,int a,int b)
  {
    if((a>=0 && a<size)  && (b >= 0 && b < size))
    {
      //When valid a and b location
      char temp = text[a];
      text[a] = text[b];
      text[b] = temp;

    }
    //return modified result
    return text;
  }

};
int main(int argc, char const *argv[])
{
  MyString obj;

  string info ="ABCDEF";
  //Get the size of string
  int size = info.length();
  int a = 0;
  int b = 4;
  cout<<info<<endl;
  info = obj.swap(info,size,a,b);
  cout<<info<<endl;
  return 0;
}

Output

ABCDEF
EBCDAF
/*
  Java Program
  Swap characters in string
*/
public class MyString {

  //Swapping two string elements by index
  public String swap(String text,int size,int a,int b)
  {
    //Check valid location of swap element
    if((a>=0 && a<size)  && (b >= 0 && b < size))
    {
      //Get first character
      char first = text.charAt(a);

      //Get second character
      char second = text.charAt(b);

      //Put character
      text = text.substring(0, b) 
                + first 
                + text.substring(b + 1);

      text = text.substring(0, a) 
                + second
                + text.substring(a + 1);
    }

    return text;
  }
  public static void main(String[] args) {
    MyString obj = new MyString();
    
    String info ="ABCDEF";
    //Get the size of string
    int size = info.length();
    //Location
    int a = 0;
    int b = 4;
    System.out.print(info+"\n");

    info = obj.swap(info,size,a,b);

    System.out.print(info+"\n");
  }
}

Output

ABCDEF
EBCDAF
using System;

/*
  C# Program
  Swap characters in string
*/

public class MyString {
  //Swapping two string elements by index
  public String swap(String text, int size, int a, int b) {
    //Check valid location of swap element

    if ((a >= 0 && a < size) && (b >= 0 && b < size)) {
      //Get first character
      char first = text[a];
      //Get second character
      char second = text[b];
      //Put characters
      text = text.Substring(0, b) + first + text.Substring(b + 1);
      text = text.Substring(0, a) + second + text.Substring(a + 1);
    }
        //important this text are modified inside a function.  
        //return modified text and assign on actual text variable
    return text;
  }
  public static void Main(String[] args) {
    MyString obj = new MyString();
    String info = "ABCDEF";
    //Get the size of string
    int size = info.Length;
    //Location
    int a = 0;
    int b = 4;
    Console.Write(info + "\n");
    info = obj.swap(info, size, a, b);
    Console.Write(info + "\n");
  }
}

Output

ABCDEF
EBCDAF
<?php
/*
  Php Program
  Swap characters in string
*/
class MyString {
  //Swapping two string elements by index
  public  function swap($text, $size, $a, $b) {
    //Check valid location of swap element

    if (($a >= 0 && $a < $size) && ($b >= 0 && $b < $size)) {
      //Get first character
      $first = $text[$a];
      //Get second character
      $second = $text[$b];
      //Put character
      $text = substr($text,0, $b-strlen($text)) . $first .substr($text,$b+1 );
 
      $text = substr($text,0, $a-strlen($text)) . $second .substr($text,$a+1 );
    }
    return $text;
  }
}

function main() {
  $obj = new MyString();
  $info = "ABCDEF";
  //Get the size of string
  $size = strlen($info);
  //Location
  $a = 0;
  $b = 4;
  echo($info ."\n");
  $info = $obj->swap($info, $size, $a, $b);
  echo($info ."\n");

}
main();

Output

ABCDEF
EBCDAF
/*
  Node Js Program
  Swap characters in string
*/
class MyString {
  //Swapping two string elements by index
  swap(text, size, a, b) {
    //Check valid location of swap element

    if ((a >= 0 && a < size) && (b >= 0 && b < size)) {
      //Get first character
      var first = text[a];
      //Get second character
      var second = text[b];
      //Put character
      text = text.substring(0, b) + first + text.substring(b + 1);
      text = text.substring(0, a) + second + text.substring(a + 1);
    }

    return text;
  }
}

function main(args) {
  var obj = new MyString();
  var info = "ABCDEF";
  //Get the size of string
  var size = info.length;
  //Location
  var a = 0;
  var b = 4;
  process.stdout.write(info + "\n");
  info = obj.swap(info, size, a, b);
  process.stdout.write(info + "\n");
}

main();

Output

ABCDEF
EBCDAF
#  Python 3 Program
#  Swap characters in string
class MyString :
  # Swapping two string elements by index
  def swap(self, text, size, a, b) :
    # Check valid location of swap element
    if ((a >= 0 and a < size) and(b >= 0 and b < size)) :
      data = list(text)
      data[a],data[b] = data[b],data[a]
      return ''.join(data)

    return text
  

def main() :
  obj = MyString()
  info = "ABCDEF"
  # Get the size of string
  size = len(info)
  # Location
  a = 0
  b = 4
  print(info ,"\n", end = "")
  info = obj.swap(info, size, a, b)
  print(info ,"\n", end = "")


if __name__ == "__main__":
  main()

Output

ABCDEF
EBCDAF
# Ruby Program
# Swap characters in string
class MyString 
   # Swapping two string elements by index
  def swap(text, size, a, b) 
     # Check valid location of swap element
    if ((a >= 0 && a < size) && (b >= 0 && b < size)) 
       # Get first character
      first = text[a]
      text[a]=text[b]
      text[b]=first
    end
    return text
  end
end
def main() 
  obj = MyString.new()
  info = "ABCDEF"
   # Get the size of string
  size = info.length()
   # Location
  a = 0
  b = 4
  print(info ,"\n")
  info = obj.swap(info, size, a, b)
  print(info ,"\n")
end
main()

Output

ABCDEF
EBCDAF
/*
  Scala Program
  Swap characters in string
*/
class MyString {
  //Swapping two string elements by index
  def swap(info: String, size: Int, a: Int, b: Int): String = {
    //Check valid location of swap element
        var text = info;
    if ((a >= 0 && a < size) && (b >= 0 && b < size)) {
      //Get first character
      val first: Char = text(a);

      //Get second character
      val second: Char = text(b);

      //Put character
      text = text.substring(0, b) + first + text.substring(b + 1);
      text = text.substring(0, a) + second + text.substring(a + 1);
    }
    return text;
  }
}
object Main {
  def main(args: Array[String]): Unit = {
    val obj: MyString = new MyString();
    var info: String = "ABCDEF";

    //Get the size of string
    val size: Int = info.length();

    //Location
    val a: Int = 0;
    val b: Int = 4;
    print(info + "\n");
    info = obj.swap(info, size, a, b);
    print(info + "\n");
  }
}

Output

ABCDEF
EBCDAF
/*
  Swift Program
  Swap characters in string
*/
class MyString {
  //Swapping two string elements by index
  func swap(_ text: String, _ size: Int, _ a: Int, _ b: Int) -> String {
    //Check valid location of swap element

    if ((a >= 0 && a < size) && (b >= 0 && b < size)) {
      var result = Array(text)
            result.swapAt(a, b)
            return String(result)
    }
    return text;
  }
}
func main() {
  let obj: MyString = MyString();
  var info: String = "ABCDEF";
  //Get the size of string
  let size: Int = info.count;
  //Location
  let a: Int = 0;
  let b: Int = 4;
  print(info ,"\n", terminator: "");
  info = obj.swap(info, size, a, b);
  print(info ,"\n", terminator: "");
}
main();

Output

ABCDEF
EBCDAF

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment