- 2
How to Reverse String without change the place of Special Characters?
example - abc#def,gh$@ output will be- hgf#edc,ba$@
18 Respuestas
0
in java or any language
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();
0
without using any library function. .
0
ok,
for(int i=str.length()-1; i>=0; i--){
String revStr + =str.charAt(i);
}
revStr will have reversed strimg
0
hi
0
I wrote a script in python that reverse the string without changing the position of special characters:
https://code.sololearn.com/cW6tG7B4y1De/#py
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);
}
0
Mohd Abid, did you solved the problem of reversing the string?
0
No , my friend Isaac Salcedo ... Can you write in java?
0
Yes, I will write the program in Java.
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
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
0
https://code.sololearn.com/cU4ICitfYo0g/?ref=app
0
You did it!
0
yes..
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))
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$@
- 1
language?