kikit
7/6/2016 - 10:22 AM

Reverse an string without affecting special characters

Reverse an string without affecting special characters

/*
http://ideone.com/hUuU3D
http://www.geeksforgeeks.org/reverse-an-array-without-affecting-special-characters/
http://www.practice.geeksforgeeks.org/problem-page.php?pid=973
*/

#include <iostream>
#include <string>
#include <vector>
using namespace std; 

int main() {
	int t;
	cin >> t;
	while(t--){
		string s;
		cin >> s;
		int l = 0;
		int h = s.size() - 1;
		char temp = ' ';
		while(l < h){
			if((s[l] >= 'a' && s[l] <= 'z') || (s[l] >= 'A' && s[l] <= 'Z')){
				if((s[h] >= 'a' && s[h] <= 'z') || (s[h] >= 'A' && s[h] <= 'Z')){
					temp = s[l];
					s[l] = s[h];
					s[h] = temp;
					l++;
					h--;
				}else{
					h--;
				}
			}else{
				l++;
			}
		}
		cout << s <<endl;
	}
	return 0;
}