Thread: Rotating a string?

  1. #1
    Registered User Silverdream's Avatar
    Join Date
    Feb 2002
    Posts
    53

    Question Rotating a string?

    hi,
    I want to write a program to rotate any given string. For example:

    if the given string is "space"

    i want the program to print

    space paces acesp cespa espac

    well i have started of like this but couldnt proceed further.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void rotation(char *str);
    
    int main()
    {
    	char str[80];
    
    	printf("\nEnter the string\n");
    	scanf("%s",str);
    
    	rotation(str);
    
    	return 0;
    }
    
    void rotation(char *str)
    {
    	int i,len;
    	len=strlen(str);
    
    	printf("%s",str);
    
    	str++;
    	printf("\n");
    
    	while(*str!='\0')
    	{
    		printf("%c",*str);
    		str++;
    
    
    	}printf("%c",str[]);
    }

    please help

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    This is how I would have approached it;

    Code:
    void rotation(char *str)
    {
    int i,j,len;
    char buff[len+1],temp;
    len=strlen(str);
    strcpy(buff,str);
    
    for(i = 0; i<len; i++)
    {
    temp = buff[0];
    for(j = 0; j<len-1; j++)
    {
    buff[j] = buff[j+1];
    }
    buff[len-1] = temp;
    printf("%s\n",buff);
    }
    }
    Seems to work.....but if I really wanted to do this I would use srtrev()

  3. #3
    Registered User Silverdream's Avatar
    Join Date
    Feb 2002
    Posts
    53
    Hi,

    thanx for the help. i tried it out but it doesnt work. gives me an error

    Constant expression required in function rotation

    in this line

    char buff[len+1],temp;


    Also please explain ur code because i am new to C and finding it difficult to understand.

    Thanx

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Silverdream
    Hi,

    thanx for the help. i tried it out but it doesnt work. gives me an error

    Constant expression required in function rotation

    in this line

    char buff[len+1],temp;


    Also please explain ur code because i am new to C and finding it difficult to understand.

    Thanx

    Sorry.....substitute with char buff[80],temp;

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    31
    try this:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        char *string = "space";
        char temp;
        int i, j, len;
    
        len = strlen(string);
    
        printf("%s\n", string);
    
        for(i = 0; i < len; ++i)
        {
            temp = string[0];
    
            for(j = 0; j < (len - 1); ++j)
            {
                string[j] = string[j + 1];
            }
            string[len - 1] = temp;
            printf("%s\n", string);
        }
    
        return 0;
    }
    hope this helps.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    31
    oops, you fixed yours right before i posted. oh,well.

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    31
    you know before i didnt read your code, i just read the post where silver said it didnt compile so i went ahead and and replied. then i noticed a reply from you correcting your error. after that i decided to look over yours and its identical to my suggestion. i didnt copy you... I swear! lol.
    i just want to clear that up so you dont think im some kind of jerk.

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    LOL...dont worry......

  9. #9
    Registered User Silverdream's Avatar
    Join Date
    Feb 2002
    Posts
    53
    Thanx a lot both of u. It works gr8.
    Last edited by Silverdream; 02-14-2002 at 11:46 AM.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How about this
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ( ) {
      char input[80], rotate[80];
      int  len, i;
      printf( "Enter word\n" );
      scanf( "%s", input );
    
      strcpy( rotate, input );
      strcat( rotate, input );
      len = strlen ( input );
      for ( i = 0 ; i < len ; i++ ) {
        printf( "%.*s\n", len, &rotate[i] );
      }
      return 0;
    }

  11. #11
    Registered User
    Join Date
    Feb 2002
    Posts
    31
    clever

  12. #12
    Registered User Silverdream's Avatar
    Join Date
    Feb 2002
    Posts
    53
    Hi Salem,

    Could u please explain me this part of ur code?

    printf( "%.*s\n", len, &rotate[i] );

    I am not able to understand

  13. #13
    Registered User
    Join Date
    Feb 2002
    Posts
    31
    ok, whats hes done here is use variable formatting (not sure if thats the correct term). basically this line of code:

    printf("%.*s\n", len, &rotate[i]);

    says print len characters starting at the address of rotate[i]. so if rotate = "spacespace" and i = 1, then it will print len ( 5 ) characters starting at 'p', which would be "paces". if i = 2, then it would print len characters starting at 'a', which would be acesp. normally if you pass an address to printf, it will print from that address all the way to '\0', but with variable formatting hes able to control how much of the string gets printed.

    im still pretty new to c myself so i hope there isnt anything incorrect with what i said. if there is, hopefully someone will correct me.

    hope this helps.

  14. #14
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Here's another way to do it

    Code:
    #include <stdio.h>
    
    int main(void)
    {
         char s[180];
         int i, j;
         size_t len;
    
         printf("Enter a string: ");
         fflush(stdout);
         scanf("%s", s);
         len = strlen(s);
         for (i = 0; i < len; ++i) {
                for (j = 0; j < len; ++j)
                       putchar(s[(i + j) % len]);
                putchar('\n');
         }
    
         return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM