Problem: Reverse a given string keeping its special character(s) at the same place
Input Format: First line contains a single integer, the number of testcases n. Next n line contains n strings as input.
(NOTE: The actual problem didn’t have the number of testcases as input. But for your better convenience we have worked with testcases.)
Sample Input:
3
intell#ect
h@ello
a#b@c
Sample Output:
tcelle#tni
o@lleh
c#b@a
Explanation: Here in the first example the whole string is reversed keeping the special character ‘#’ fixed in its original position i.e. on position 7.
In the second case, whole string is reversed keeping the special character ‘@’ fixed in its original position i.e. on position 2.
In the last case, the whole string is reversed keeping the special characters ‘#’, ‘@’ fixed in its original position i.e. on position 2 and 4 respectively.
Solution: We strongly recommend you to try the problem first before moving to the solution.
# Solution of String Reversal Except Special Characters problem in Python
# www.codewindow.in
def solve(s):
import re
str_list=re.findall("[a-z,A-Z]",s)
str_list.reverse()
for i in range(len(s)):
if ((ord(s[i])>=32 and ord(s[i])<=47) or (ord(s[i])>=58 and ord(s[i])<=64) or (ord(s[i])>=94 and ord(s[i])<=96) or (ord(s[i])>=123 and ord(str[i])<=126)):
str_list.insert(i,s[i])
print(''.join(str_list))
# Driver Code
test_case=int(input()); # Take number of testcases
while(test_case):
str=input()
solve(str)
test_case-=1
Input:
3
intell#ect
h@ello
a#b@c
Output:
tcelle#tni
o@lleh
c#b@a
Follow Us
You Missed
- Dunzo Off-Campusing Drive | Data Engineer | codewindow.in
- Verbal Ability Questions Solved | Synonyms Antonyms – codewindow.in
- Verbal Ability Questions Solved | Statement Correction – codewindow.in
- Verbal Ability Questions Solved | Spotting Error – codewindow.in
- Verbal Ability Questions Solved | Speech and Tenses – codewindow.in
- Try
- Verbal Ability Questions Solved | Sentence Arrangement – codewindow.in
- Verbal Ability Questions Solved | Prepositions – codewindow.in
- Verbal Ability Questions Solved | Grammar – codewindow.in
- Verbal Ability Questions Solved | Articles – codewindow.in
- Zoho Off Campus Drive | Product Marketing Associate / Specialist | codewindow.in
- BerryWorks Off Campus Recruitment Drive – Software Engineer – Codewindow.in
- Microsoft Off Campus Hiring Drive – Technical Support – Codewindow.in
- Infosys Pseudocede & Puzzle Solved – codewindow.in
- Infosys Verbal Questions Solved – codewindow.in
- Amazon Off Campus Drive | Trainee | codewindow.in
- Hawkins Off Campus Drive | Management trainees | codewindow.in
- Infosys Mathematical Questions Solved – codewindow.in
- Paypal Off Campus Recruitment Drive – Software Engineer – Codewindow.in
- IBM Off Campus Hiring Drive – Associate Systems Engineer – Codewindow.in
- Infosys Off Campus Hiring Drive – SP and DSE – Codewindow.in
- VIRTUSA Recruitment 2022 | Associate Engineer | codewindow.in
- Infosys Logical Questions solved – codewindow.in
- Google Off Campus Hiring Drive – IT Support Engineer – Codewindow.in
- Capgemini Pseudocode Solved | Set 4 – codewindow.in
- Capgemini Pseudocode Solved | Set 3 – codewindow.in
- Capgemini Pseudocode Solved | Set 2 – codewindow.in
- JECA 2022 Question Paper – 90+ Sample Questions Exposed – codewindow.in
- Capgemini Pseudocode Solved | Set 1 – codewindow.in
- Verizon Off Campusing | Software Developer | codewindow.in
- TCS Recruitment Off-Campusing | Service Desk Analyst | codwindow.in
- Amdocs Off-Campusing | Devops Engineer | codewindow.in