Special array reversal
PROBLEM :
Given a string S, containing special characters and all the alphabets, reverse the string without
affecting the positions of the special characters.
Input
The first line of input contains an integer T denoting the number of test cases. Then T test cases
follow.
The first line of each test case contains a case-insensitive string S.
Output
Print out the reversed string without affecting special characters.
Constraints
1 <= T <= 100
0 < S <= 100
Examples
Input
3
A&B
abc%sld*
dakA&*hA@#N
Output
B&A
dls%cba*
NAhA&*ka@#d
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<string.h>
string Special_array_reversal(string ) ;
int main()
{
int t ;
string str ;
cin>>t ;
while(t--)
{
cin>>str ;
str=Special_array_reversal(str) ;
cout<<str<<endl ;
}
return 0;
}
string Special_array_reversal(string str)
{
int l ;
l=str.length() ;
int i,j ;
char s ;
i=0 ;
j=l ;
while(i<j)
{
if(!((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z')))
{
i++ ;
continue ;
}
if(!((str[j]>='A'&&str[j]<='Z')||(str[j]>='a'&&str[j]<='z')))
{
j-- ;
continue ;
}
s=str[i] ;
str[i]=str[j] ;
str[j]=s ;
i++ ;
j-- ;
}
return str ;
}
---------------------------------------------------------------------------------
Given a string S, containing special characters and all the alphabets, reverse the string without
affecting the positions of the special characters.
Input
The first line of input contains an integer T denoting the number of test cases. Then T test cases
follow.
The first line of each test case contains a case-insensitive string S.
Output
Print out the reversed string without affecting special characters.
Constraints
1 <= T <= 100
0 < S <= 100
Examples
Input
3
A&B
abc%sld*
dakA&*hA@#N
Output
B&A
dls%cba*
NAhA&*ka@#d
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<string.h>
string Special_array_reversal(string ) ;
int main()
{
int t ;
string str ;
cin>>t ;
while(t--)
{
cin>>str ;
str=Special_array_reversal(str) ;
cout<<str<<endl ;
}
return 0;
}
string Special_array_reversal(string str)
{
int l ;
l=str.length() ;
int i,j ;
char s ;
i=0 ;
j=l ;
while(i<j)
{
if(!((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z')))
{
i++ ;
continue ;
}
if(!((str[j]>='A'&&str[j]<='Z')||(str[j]>='a'&&str[j]<='z')))
{
j-- ;
continue ;
}
s=str[i] ;
str[i]=str[j] ;
str[j]=s ;
i++ ;
j-- ;
}
return str ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment