Link to home
Start Free TrialLog in
Avatar of arijit_rebaca
arijit_rebaca

asked on

Shift letters of a string

Suppose a string is "arunabha". If I want to shift it by 3 letter so that it comes to "ahbanuna", how it can be implemented?
Avatar of rajeev_devin
rajeev_devin

>> Suppose a string is "arunabha". If I want to shift it by 3 letter so that it comes to "ahbanuna", how it can be
>> implemented?
Hoe u r shifting here? It is not shifting.
If you shift  "arunabha" left then it becomes "nabhaaru"
If you shift  "arunabha" right then it becomes "bhaaruna"
You appear to have a typographical error in your question.  The transformation is not a shift.  At first glance, I thought it was a rotation, but upon closer examination, it appears to be a string reversal with a typo (r->n).

Can you give a correct and unambiguous example of the transformation?  (Your use of the palindrome "anuna" as one of the substrings leads to ambiguity.)


Avatar of arijit_rebaca

ASKER

Probably you are asking for this kind of solution:
Call this function ,
char* ShiftMe(char* src, int number)
{
      char out_buf[256];
      memset(out_buf, 0 , sizeof(out_buf));
      strncpy(out_buf + number, src, strlen(src) - number);
      int len = strlen(src);
      for (int i = 0; i < number; i++)
      {
            out_buf[i] = src[len - i - 1];
      }
      return out_buf;
}      
without using string.h function. How can it be implemented ?
Avatar of Infinity08
there's one big problem with your code : you're returning a pointer to a local buffer (which doesn't exist any more when the function returns). You need to allocate memory on the heap for your out_buf :

  char *out_buf = (char*) calloc(256, sizeof(char));

Make sure you don't forget to free this once you don't need it any more :

  char *shifted_str = Shiftme(str, 3);
  /* uses shifted_str */
  free(shifted_str);

>>  without using string.h function. How can it be implemented ?
Why don't you want to use string.h ? It's a standard library, and it's perfectly fine to use it ? Any specific reason ?
No, such specific reson. Actually I was asked this Q in an interview. Can it be implemented using circular buffer(linklist) ?
 
ASKER CERTIFIED SOLUTION
Avatar of uniques
uniques

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> Can it be implemented using circular buffer(linklist) ?
Of course, but it's not really the most optimal way (unless you have to do a lot of shifts). Imagine a circular buffer like this :

   ----------------------------------
  |                                          |
   -> s -> t -> r -> i -> n -> g--
       ^
       |

the string pointer points to the 's' as the first character, so the string would read "string" when read. If we shift three characters right now, we get :

   ----------------------------------
  |                                          |
   -> s -> t -> r -> i -> n -> g--
                           ^
                           |

and the string would read "ingstr" as the string pointer points to 'i' as the first character.

btw, also note that the internal buffer problem is still present in uniques code !
Why did you give a B grade for uniques' answer ? He gave you a solution (keeping in mind the local buffer problem) ... was there something not clear ? Something you still have questions about ? Feel free to ask more !
Infinity08,  unique's solution should have been given a D or an F grade.  It is severely flawed:
 - It does not perform the rotation in place.  
 - Its fixed-size buffer arbitrarily limits the length of the string to be transformed.  
 - If a string longer than 256 bytes is provided, a buffer overrun occurs corrupting the stack and overwriting the return address.
 - The function returns a pointer to a stack local buffer, which goes out of scope upon return from the function.

If the Author had presented this solution in a job interview, I would not have hired him.

I realise that, brett, but the answer was accepted ... if arijit_rebaca wasn't happy about it, he could just have asked for more information, better code, ... before accepting it. That's all i wanted to say :) After all, we're here to help people, not to get points ...
Precisely.  We are here to help people, and giving someone the wrong answer is no help.
In fact it not only hurts the questioner, it hurts future users, since these forums are searchable.

Hi Brett,

The solution is flawed, but it was not the intention to give a wrong answer. I tried .... and it did not turn out to be the exact correct answer. More experienced experts can post better solutions.