I want to reverse string
"My! name.is@rin"
My output must be
"yM! eman.si@nir"
I wrote following code which gives output as "yM eman si nir" Please resolve the issue :
return str = stringIn.split("").reverse().join("").split(/[^a-zA-Z]/g).reverse().join(" ");
I want to reverse string
"My! name.is@rin"
My output must be
"yM! eman.si@nir"
I wrote following code which gives output as "yM eman si nir" Please resolve the issue :
return str = stringIn.split("").reverse().join("").split(/[^a-zA-Z]/g).reverse().join(" ");
Share
Improve this question
edited Mar 18, 2021 at 15:11
Penny Liu
17.4k5 gold badges86 silver badges108 bronze badges
asked Oct 16, 2015 at 7:47
rkkkkrkkkk
1331 gold badge1 silver badge9 bronze badges
3
- What do you think your code does? – Sverri M. Olsen Commented Oct 16, 2015 at 7:52
- Is it words you want to reverse, or the entire string? – OliverRadini Commented Oct 16, 2015 at 7:55
- try reversing each work + escape and unescape each word – Praveen Commented Oct 16, 2015 at 7:56
7 Answers
Reset to default 11From your regular expression, it seems your criterion for "special characters" is anything other than the letters A to Z.
You can use String.prototype.replace with a regular expression to match sequences of letters, then provide a replace function that modifies the match before replacing it, e.g.
var stringIn = 'My! name.is@rin';
var rev = stringIn.replace(/[a-z]+/gi, function(s){return s.split('').reverse().join('')});
document.write(rev); // yM! eman.si@nir
Here is one more approach we can do the same:
function isLetter(char){
return ( (char >= 'A' && char <= 'Z') ||
(char >= 'a' && char <= 'z') );
}
function reverseSpecialString(string){
let str = string.split('');
let i = 0;
let j = str.length-1;
while(i<j){
if(!isLetter(str[i])){
++i;
}
if(!isLetter(str[j])){
--j;
}
if(isLetter(str[i]) && isLetter(str[j])){
var tempChar = str[i];
str[i] = str[j];
str[j] = tempChar;
++i;
--j;
}
}
return str.join('');
}
document.write(reverseSpecialString("Ab,c,de!$"));
let check=(str)=>{
let pat=/[a-zA-z]/igm;
if(str.match(pat)){
return true
}else{
return false
}
}
let r=(str)=>{
let arr=str.split('');
let l=0;
let r=arr.length-1;
while(l<r){
if (!check(arr[l])){
l++;
}else if(!check(arr[r])){
r--;
}else{
let tmp=arr[l];
arr[l]=arr[r];
arr[r]=tmp;
l++;
r--;
}
}
return arr.join('');
}
document.write(r("p@$$word"));
Pythonic approach which answers in a single line.
import re
def reverseOnlyString(text_in):
return ("".join([tup[0][::-1] + tup[1] for tup in re.findall(r'(\w*)(\W*)', text_in)]))
my_string = 'my name@#$ is 123 Test'
print(reverseOnlyString(my_string))
my_string = "My name is Tom Hank"
print(reverseOnlyString(my_string))
my_string = "MynameisTom Hank"
print(reverseOnlyString(my_string))
my_string = "%$%^^ *&&^ABC5443&***("
print(reverseOnlyString(my_string))
==>ym eman@#$ si 321 eliN
==>yM eman si moT knaH
==>moTsiemanyM knaH
==>%$%^^ *&&^3445CBA&***(
Pythonic approach without regex:
p = "a!!!b.c.d,e'f,ghi"
q = ''.join([i for i in p if i.isalpha() ])[::-1]
r = ''
for i in range(len(p)):
if p[i].isalpha():
r+=q[0]
q =q[1::]
else:
r+=p[i]
print(r)
print(p)
Ruby Solution:
[46] pry(main)> "My! name.is@rin"
=> "My! name.is@rin"
[47] pry(main)> "My! name.is@rin".gsub(/\w+/) { |word| word.reverse }
=> "yM! eman.si@nir"
def reverse(str1):
l1=list(str1)
n=len(str1)
l=0
r=n-1
while l<r:
if not l1[l].isalpha():
l+=1
elif not l1[r].isalpha():
r-=1
else:
l1[l],l1[r]=l1[r],l1[l]
l+=1
r-=1
return ''.join(l1)
str1="a,b$c"
q=reverse(str1)
print(q)
c,b$a
uses a simple approach that traverses the string twice. if a special character is encountered then just proceed . else swap the alphanumeric character. the check for alphanumeric character is done using x.isalpha()