Given a string str, the task is to swap adjacent characters of this string in C.
Input: str = "geeks" Output: NA Not possible as the string length is odd Input: str = "geek" Output: egke
- Check if the length of the string is even or odd.
- If the length is odd, swapping cannot be done.
- If the length is even, take each character of the string one by one and swap it with the adjacent character.
Below is the implementation of the above approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- String with k distinct characters and no same characters adjacent
- Swap characters in a String
- Swap all occurrences of two characters to get lexicographically smallest string
- Java program to swap first and last characters of words in a sentence
- Rearrange characters in a string such that no two adjacent are same
- Rearrange characters in a string such that no two adjacent are same using hashing
- Check if a given string can be formed using characters of adjacent cells of a Matrix
- Minimum replacements to make adjacent characters unequal in a ternary string
- Minimum replacements to make adjacent characters unequal in a ternary string | Set-2
- Program to toggle all characters in a string
- Program to check if first and the last characters of string are equal
- Python program to check if a string contains all unique characters
- Program to find the XOR of ASCII values of characters in a string
- Lexicographically smallest array formed by at most one swap for every pair of adjacent indices
- Minimum string such that every adjacent character of given string is still adjacent
- Program to print characters present at prime indexes in a given string
- Program to find the product of ASCII values of characters in a string
- Program to find the largest and smallest ASCII valued characters in a string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : kabitac45