By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,044 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,044 IT Pros & Developers. It's quick & easy.

How to left rotate a given string by k positions ?

P: 56
If we are given a string for instance abcdef and k=2 , so output should be cdefab , i.e. first two characters are appended at end

Here's my code , please tell where's the mistake :

#include<stdio.h>

int main()
{
int count=0,a=0,length=0;
char name[]="abcdef";
char *ptr,*ptr1;
ptr=name;
ptr1=name;
char *name1=name;
while(*ptr!='\0')
{
length++;
ptr++;
}
printf("enter the position by which you have to rotate ");
scanf("%d",&count);
while(a<length )
{
*ptr++=*ptr1++;
name[a]='\0';
name1++;
a++;
}
name1++;
*ptr='\0';
printf("%s",name1);

return 0;
}
Apr 8 '16 #1

✓ answered by donbock

How do you want to handle corner cases?
  1. What behavior is required if k is 0? Rotation by 0 characters is the same as no rotation at all.
  2. What behavior is required when k is negative? Do you need to rotate right?
  3. What behavior is required when k is greater than or equal to the length of the string?
  4. What behavior is required when cases 2 and 3 are both true?
  5. Your program should not fail if the length of the string is 0.
You should test for any illegal k values and report an error if one is found.

Share this Question
Share on Google+
8 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
You could try:
Expand|Select|Wrap|Line Numbers
  1. char name[] ="abcdef";
  2.         char* array = new char[2 + 1];
  3.     strncpy(array, name, 2);
  4.     array[2] = '\0';
  5.  
  6.     strcpy(name, name + 2);
  7.     strcpy(name + strlen(name), array);
I'm using a k of 2.

The idea is to copy the part that rotates to the end of the string off into a buffer. The strncpy I use does not append a \0 so I append one explicitly.

Then I need to copy the rest of the string forward to the beginning.

The strcpy the string into itself by copying from name+2 to name. This will always work and the \0 of name stops ther strcpy.

All that remains is to copy the original first part of the string (held in array) to the end of the string.
Apr 8 '16 #2

P: 56
Actually we don't have to use any library function here like strncpy, can you make changes in my code itself and modify it so as to solve the purpose .
Apr 9 '16 #3

Expert 100+
P: 2,398
How do you want to handle corner cases?
  1. What behavior is required if k is 0? Rotation by 0 characters is the same as no rotation at all.
  2. What behavior is required when k is negative? Do you need to rotate right?
  3. What behavior is required when k is greater than or equal to the length of the string?
  4. What behavior is required when cases 2 and 3 are both true?
  5. Your program should not fail if the length of the string is 0.
You should test for any illegal k values and report an error if one is found.
Apr 9 '16 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
The idea of a library function is to save you the effort of reinventing the wheel. Writing code is a last resort. Whenever possible, use a library function.

Of course, if this class specifically says to not use a library function, then you would write from scratch.

As to debugging your code, have you stepped through it using your debugger?
Apr 9 '16 #5

P: 56
No , I have no idea regarding which debugger to use . Even I tried with online debugger but it takes too long to respond so that is why I never worked with debuggers.
Apr 9 '16 #6

weaknessforcats
Expert Mod 5K+
P: 9,197
What compiler are you using? For example, Visual Studio includes a debugger that lets you step through your code and be able to see the contents of your variables.

It's important to learn to debug. More important than coding.
Apr 9 '16 #7

P: 56
I am using gcc on windows . MINGW 32-gcc for windows
Apr 10 '16 #8

weaknessforcats
Expert Mod 5K+
P: 9,197
Here you go: http://www.tutorialspoint.com/gnu_debugger/.

Let me know how it goes.
Apr 10 '16 #9

Post your reply

Sign in to post your reply or Sign up for a free account.