Open In App

C Program to Swap Adjacent Characters of a String

Last Updated : 05 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will learn how to swap adjacent characters of a string in C. To swap all adjacent characters in a string, the string must have an even number of characters. If the number of characters is odd, the last character remains unswapped since it has no adjacent element.

The most straightforward method to swap adjacent characters of a string is by traversing the string using a loop and swapping adjacent character using a temporary variable. Let’s take a look at an example:

C
#include <stdio.h>
#include <string.h>

void swap(char *s) {
  	int l = strlen(s);
  	if (l % 2 != 0) {
      	printf("Cannot Swap");
      	return;
    }
  
    for (int i = 0; i < l; i += 2) {
      
        // Swap using temporary variable
        char temp = s[i];
        s[i] = s[i+1];
        s[i+1] = temp;
    }
}

int main() {
    char s[] = "abcd";
    
    // Swap adjacent characters in the string
    swap(s);

    printf("%s", s);
    return 0;
}

Output
badc

Explanation: In this method, we use a loop to traverse the string two characters at a time. By using array indexing, we can easily access and swap each pair of adjacent characters. The loop ensures that we continue swapping until the end of the string is reached.

There are also a few other methods in C swap adjacent characters of a string. Some of them are as follows:

Using Pointers

This method uses pointer arithmetic to traverse the string, swapping adjacent characters. The pointer moves through the string, swapping pairs of characters, then advances by two positions to compare the next pair.

C
#include <stdio.h>
#include <string.h>

void swap(char *s) {
  
    // Continue till a pair exists
    while (*s && *(s + 1)) { 
      
        // Swap adjacent characters
        char temp = *s;
        *s = *(s + 1);
        *(s + 1) = temp;
      
        // Move the pointer ahead by two positions to swap the next pair
        s += 2;  
    }
}
int main() {
    char s[] = "abcd";
    
    swap(s);

    printf("%s\n", s);
    return 0;
}

Output
badcfe

Using Recursion

The function recursively processes smaller portions of the string, swapping adjacent characters and calling itself to swap the next two characters. This continues until the end of the string or no more pairs remain to be swapped.

C
#include <stdio.h>
#include <string.h>

void sHelper(char *s) {
  
    // Swap only when there are two characters left
    if (*s && *(s + 1)) {  
        
        char temp = *s;
        *s = *(s + 1);
        *(s + 1) = temp;

        // Recursive call for the next pair of characters
        sHelper(s + 2);
    }
}

void swap(char* s) {
  	if (strlen(s) % 2 != 0) {
      	printf("Cannot Swap");
      	return;
    }
  	sHelper(s);
}

int main() {
    char s[] = "abcd";

  	// Swap adjacent characters is s
    swap(s);

    printf("%s", s);
    return 0;
}

Output
badcfe

C Program to Swap Adjacent Characters of a String
Practice Tags :

Similar Reads