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

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

5. how do u go to the end of the string ???

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