how to rotate a line of text ??

This is a discussion on how to rotate a line of text ?? within the C Programming forums, part of the General Programming Boards category; I would like to know how to write a function that rotates the line of text. Eg. Input line = ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    4

    how to rotate a line of text ??

    I would like to know how to write a function that rotates the line of text.

    Eg. Input
    line = darude
    n = 3

    Output
    udedar

    Any help is appreciated !!!

  2. #2
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    heres some code...

    len=strlen(text);

    for(i=n;i<len;i++)
    cout<<text[i];

    for(i=0;i<n;i++)
    cout<<text[i];


    Haven't tested but should work.

    Sorry for the couts use whatever you like to use in C

  3. #3
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    Hi there,
    I will not be giving u the whole code but here is an algorithm that would solve ur problem...

    1. scan the text into an array (arr)
    2. scan the number of characters to rotate (say num)
    3. i = 0
    4. if i < num go to step 5 else go to step 10
    5. read the character at arr(i) into temp (a variable)
    6. set an internal loop to move all the characters from location 1 to (string length - 1) back by 1 location. i.e. arr(1) will go to arr(0).

    7. go to the end position and insert character in temp there.
    8. increment i
    9. go to step 4
    10.print the string. (rotated).
    11.exit

    Hope u work it out...

    Regards,
    Sriharsha.
    Help everyone you can

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Here's one possible solution, a bit nasty but it works. I'll give you the whole code because if this is homework and you turn in my code, your teacher will know that you didn't write it
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    static void rotateString ( char * a, int n )
    {
      int i, j;
      char buffer[BUFSIZ] = {'\0'};
      for ( i = n, j = 0; a[i] != '\0'; i++ )
        buffer[j++] = a[i], a[i] = '\0';
      strcat ( buffer, a ), strcpy ( a, buffer );
    }
    
    int main ( void )
    {
      char a[] = "darude";
      rotateString ( a, 3 );
      (void)puts ( a );
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    4
    how do u go to the end of the string ???

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >how do u go to the end of the string ???
    If it is a nul terminated string then you can simply use a length variable that holds the last element:
    size_t len = strlen ( array );

    But be aware that this is the index of the nul in a string, the last character in the string before the nul is strlen ( array ) - 1.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 01:23 AM
  2. Is there any way to read a line from a text file?
    By megareix in forum C Programming
    Replies: 13
    Last Post: 01-09-2009, 01:13 PM
  3. Reading random line from a text file
    By helloamuro in forum C Programming
    Replies: 24
    Last Post: 05-03-2008, 11:57 PM
  4. How do I make my edit box move text to the next line?
    By ElWhapo in forum Windows Programming
    Replies: 2
    Last Post: 01-04-2005, 11:21 PM
  5. inputting line of text vs. integers in STACK
    By sballew in forum C Programming
    Replies: 17
    Last Post: 11-27-2001, 11:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21