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 !!!
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 !!!
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
-
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
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
-PreludeCode:#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; }
My best code is written with the delete key.
how do u go to the end of the string ???
>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.