本题要求:
昨天学习了字符串,今天让我们实践一下吧~!
大家都见过各个商店门上挂着的跑马灯呢,就是一行滚动的字,今天我们就用strncpy实现这个功能。
输入格式:
无
输出格式:
输出跑马灯,内容为HelloWorld
输入样例:
无
输出样例:
解题思路 :
就使用strncpy即可
代码 :
#include <iostream>
#include <cstring>
#include <windows.h>
using namespace std;
#define MAX 12
void gotoXY(int x, int y)
{
COORD coord = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
int main() {
char a[MAX + 1];
char c[101]= "HelloWorld";
int size = strlen(c);
int i = 0;
while (true) {
Sleep(100);
memset(a, ' ', sizeof(a));
int temp = size;
if (size + i > MAX) {
temp -= (size + i) - MAX;
}
strncpy(a + i, c, temp);
strncpy(a, c + temp, size - temp);
gotoXY(0,0);
cout << a;
i++;
if (i >= MAX) {
i = 0;
}
}
return 0;
}