rotate

Write a function rotate that takes in a string s and “rotates” the string to the right k times. Do not rotate the string if k is negative. Could you please help me I do not know how to do it.
 
  
@Nata

Here is a small program that shows how to rotate a line of text. Now it's up to you to see how to create the function, and use this. Best of luck to you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Rotate.cpp : main project file.

#include <iostream>
#include <string>
#include <Windows.h>

using namespace std;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;

void gotoXY(int x, int y);

int main()
{
    string rotating_string = "This is a moving marquee... ";
	int len = rotating_string.length();
	char letter_holder;

	do
	{
		gotoXY(14,13);
		cout << rotating_string;
		Sleep(500);
		letter_holder = rotating_string[len];
		for( int x=len;x>0;x--)
		{
			rotating_string[x] = rotating_string[x-1];
		}
		rotating_string[0] = letter_holder;

	} while(true);


    return 0;
}

void gotoXY(int x, int y) 
{ 
	CursorPosition.X = x; 
	CursorPosition.Y = y; 
	SetConsoleCursorPosition(console,CursorPosition); 
}
Last edited on
Topic archived. No new replies allowed.