How to Reverse String without change the place of Special Characters? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 2

How to Reverse String without change the place of Special Characters?

example - abc#def,gh$@ output will be- hgf#edc,ba$@

28th Feb 2017, 11:01 PM
Mohd Abid
Mohd Abid - avatar
18 Answers
0
in java or any language
1st Mar 2017, 6:56 AM
Mohd Abid
Mohd Abid - avatar
0
Ok Here , IN JAVA You will need a StringBuffer object instead of String object. Create as follows, StringBuffer someVariable = new StringBuffer("Your String with characters here "); someVariable.reverse();
1st Mar 2017, 10:31 AM
Meharban Singh
Meharban Singh - avatar
0
without using any library function. .
1st Mar 2017, 7:45 PM
Mohd Abid
Mohd Abid - avatar
0
ok, for(int i=str.length()-1; i>=0; i--){ String revStr + =str.charAt(i); } revStr will have reversed strimg
1st Mar 2017, 9:53 PM
Meharban Singh
Meharban Singh - avatar
0
hi
1st Mar 2017, 9:58 PM
abdou haroun
abdou haroun - avatar
0
I wrote a script in python that reverse the string without changing the position of special characters: https://code.sololearn.com/cW6tG7B4y1De/#py
2nd Mar 2017, 4:06 AM
Isaac Salcedo
Isaac Salcedo - avatar
0
ANOTHER METHOD.... System.out.print(reverse(str)); //assume str contains input public static String reverse(String x) { if (x.length() <= 1) { return x; } return reverse(x.substring(1, x.length())) + x.charAt(0); }
2nd Mar 2017, 5:44 AM
Meharban Singh
Meharban Singh - avatar
0
Mohd Abid, did you solved the problem of reversing the string?
12th Mar 2017, 1:52 AM
Isaac Salcedo
Isaac Salcedo - avatar
0
No , my friend Isaac Salcedo ... Can you write in java?
15th Mar 2017, 2:49 PM
Mohd Abid
Mohd Abid - avatar
0
Yes, I will write the program in Java.
15th Mar 2017, 5:02 PM
Isaac Salcedo
Isaac Salcedo - avatar
0
This is my Java version of the program that reverse a string without changing the special characters' position that I wrote in python. https://code.sololearn.com/ct55oHhAaccY/?ref=app
19th Mar 2017, 4:46 AM
Isaac Salcedo
Isaac Salcedo - avatar
0
This program is similar to reversing a string using recursion or by loop.. the only difference is you need to skip the non alphanumeric characters of the string. To detect whether a character is alphanumeric or not.. you can use isalnum function of type.h header file. while(leftIndex < rightIndex){ temp = inputString[leftIndex]; inputString[leftIndex] = inputString[rightIndex]; inputString[rightIndex] = temp; // Skip special characters while(!isalnum(inputString[++leftIndex]); while(!isalnum(inputString[++rightIndex]); } Here is the program of reversing a string using recursion and using loop. you need to modify the code little bit to skip non alphanumeric characters. http://www.techcrashcourse.com/2014/10/c-program-reverse-string.html http://www.techcrashcourse.com/2015/03/c-program-to-reverse-string-using-recursion.html
15th Apr 2017, 6:09 AM
Arun Kumar
Arun Kumar - avatar
0
https://code.sololearn.com/cU4ICitfYo0g/?ref=app
9th Jul 2017, 5:36 PM
Mohd Abid
Mohd Abid - avatar
0
You did it!
10th Jul 2017, 12:26 AM
Isaac Salcedo
Isaac Salcedo - avatar
0
yes..
10th Jul 2017, 9:43 AM
Mohd Abid
Mohd Abid - avatar
0
Hey Guyz, Please find the code for python : y = input("Enter a string you want to reverse: ") x=list(y) special_chars = "!\"#$%&\'()*+,-./:;<=>?@[\]^_`{|}~" begin = 0 end = len(x) - 1 for i in range((len(x)//2)): if (x[begin] in special_chars) and (x[end] in special_chars): begin = begin + 1 end = end - 1 elif(x[begin] in special_chars): begin = begin + 1 elif (x[end] in special_chars): end = end - 1 else: x[begin],x[end] = x[end],x[begin] begin = begin + 1 end = end - 1 z="" print(z.join(x))
19th Jan 2020, 7:24 PM
Aman Agrawal
Aman Agrawal - avatar
0
Below is the python code which will reverse each word in a string or whole string with out changing the place of special characters: str_smpl = 'abc#def,gh$@' lst = [] for word in str_smpl.split(' '): letters = [c for c in word if c.isalpha()] for c in word: if c.isalpha(): lst.append(letters.pop()) continue else: lst.append(c) lst.append(' ') print("".join(lst)) output: hgf#edc,ba$@
17th May 2020, 8:56 PM
Aashish Pandey
Aashish Pandey - avatar
- 1
language?
1st Mar 2017, 4:36 AM
Meharban Singh
Meharban Singh - avatar