Header Ads Widget

Reverse without changing the position of numbers -- TCS nqt solution

 Problem:-

Given a sting Str, which contains numbers (0 to 9) and also letters of the English alphabet ( 'a' to 'z' and 'A' to 'Z'). The task is to reverse the string such a way that the positions of numbers in the string are left unaltered.

Example 1:

Input:

a1b2igh3-Value of Str

Output:

h1g2iba3

Explanation:

Without changing the positions of 1, 2 and 3, only the positions of characters 'h', 'g. 'i, 'b' and 'a' have been reversed.

Example 2:

Input:

Ab5c7de96 -- Value of Str

Output: 

ed5c7bA96

Code(C++):-

#include<bits/stdc++.h>
using namespace std;
int main(){
string str;
cin>>str;
int l=0,r=str.length()-1;
while(l<r)
{
if(str[l]>='0' && str[l]<='9')
l++;
if(str[r]>='0' && str[r]<='9')
r--;
char tem=str[l];
str[l]=str[r];
str[r]=tem;
l++;
r--;
}
cout<<str;
return 0;
}



Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-

Post a Comment

0 Comments