How to left rotate a given string by k positions ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • radha gogia
    New Member
    • Feb 2015
    • 56

    How to left rotate a given string by k positions ?

    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,len gth=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",&cou nt);
    while(a<length )
    {
    *ptr++=*ptr1++;
    name[a]='\0';
    name1++;
    a++;
    }
    name1++;
    *ptr='\0';
    printf("%s",nam e1);

    return 0;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You could try:
    Code:
    char name[] ="abcdef";
    		char* array = new char[2 + 1];
    	strncpy(array, name, 2);
    	array[2] = '\0';
    
    	strcpy(name, name + 2);
    	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.

    Comment

    • radha gogia
      New Member
      • Feb 2015
      • 56

      #3
      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 .

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        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.
        Last edited by donbock; Apr 9 '16, 06:11 AM. Reason: Added corner cases 4 & 5.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          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?

          Comment

          • radha gogia
            New Member
            • Feb 2015
            • 56

            #6
            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.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              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.

              Comment

              • radha gogia
                New Member
                • Feb 2015
                • 56

                #8
                I am using gcc on windows . MINGW 32-gcc for windows

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  Here you go: http://www.tutorialspoint.com/gnu_debugger/.

                  Let me know how it goes.

                  Comment

                  Working...