top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Write a program to reverse the string having numeric and letters. But only letters should be reversed not the digits??

+3 votes
575 views
Write a program to reverse the string having numeric and letters. But only letters should be reversed not the digits??
posted Mar 19, 2014 by Atul Mishra

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Can you provide the example like how you like the output.

1 Answer

+1 vote

Assuming that you input is abc1def2hij3klm and you are looking for mlk1jih2fed3cba as output. It can be achieved in two pass -

1st Pass: reverse the whole string which will give you mlk3jih2fed1cba (you can google to find out the code or if you are using c++ or something similar you can use the library function.
2nd pass: Now your task is to identify the digits if it is digits then reverse it back and insert it at the position. Use the following algo to achieve the same

a[]= "abc1def2hij3klm"
b[] = reverse a;
c[]; /* temp array to store the digit */
d[]; /* temp array to store the digit location */
l=0;

for i=0 to i<length of the string b
  if IS_DIGIT(b[i]) 
    c[l] = b[i]
    d[l] = i;

e[] = reverse c;

for i=0 to i<length of the string e
    b[d[i]] = e[i]

return b;
answer Mar 20, 2014 by Luv Kumar
Similar Questions
0 votes

Suppose you are given "Luv you 123 luv me" the expected output is "vuL uoy 123 vul em"

+3 votes

input: 1 2 3 4 5
output: 5 4 1 2 3 or 1 2 3 5 4

Can any one tell?

+1 vote

Write a C logic that stores 3 numeric values ranges 0 to 9 digits into 10 bits....eg 999 to fit into 10 bits

...