Header Ads Widget

Swap the adjacent characters of the string | unthinkable solutions

Program to Swap the adjacent characters of the string :- 

Given a string , swap the adjacent characters of the string . 

Sample input:-

EasyCodingZone

Sample output:-

Enter the string
EasyCodingZone
String after Swapping the characters :
aEysoCidgnoZen

Program to Swap the adjacent characters of the string .

The objective of the code is to swap the adjacent character of the string . But if the length of the string is odd then it is not possible to swap the all the characters . 



Code:- 

#include<bits/stdc++.h>
using namespace std;
int main()
{
string str;
cout<<"Enter the string"<<endl;
cin>>str;
int length=str.size();
// if length is odd then it is not possible
// to swap the characters
if(length%2!=0)
cout<<"Swapping is not possible"<<endl;
else
{
for(int i=0;i<length-1;i=i+2)
{
char tem;
tem=str[i];
str[i]=str[i+1];
str[i+1]=tem;
}
cout<<"String after Swapping the characters : "<<endl;
cout<<str;
}
return 0;
}

Output:-

Enter the string
EasyCodingZone
String after Swapping the characters :
aEysoCidgnoZen

Wipro :-

Infytq :-

Key Points;-

Hackerrank:-


C-tutorial:-

See more:-



Post a Comment

0 Comments