演示多个字符从两端移动向中间汇聚
==演示welcom to my blog ==
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <windows.h>
int main()
{
char arr1[] = "welcom to my blog";
char arr2[] = "#################";
int left = 0;
int right = strlen(arr1) - 1;
while (left <= right)
{
arr2[left] = arr1[left];
arr2[right] = arr1[right];
printf("%s\n", arr2);
Sleep(1000);
system("cls");
left++;
right--;
}
system("pause");
return 0;
}
几点说明
Sleep(1000); 的头文件 #include <windows.h>
Sleep函数:功 能: 执行挂起一段时间 用 法: unsigned sleep(unsigned seconds);
Sleep(1000); 表示程序暂停一秒
//暂停一秒 可以看到字符变化
不想让程序跑的太快
2.
system(“cls”); 的头文件#include<stdlib.h>
清空屏幕的作用不然会出现如图的效果,很丑啊。
3.
int right = strlen(arr1) - 1;
字符串的长度-1为数组最后一个的下标
字符的个数等于字符串的长度+1//因为有/0结束
4.
原理其实很简单。两个数组,让数组一对数组二进行依次覆盖。
最后
我感受到了一点点有意思的地方,就和我电脑第一次出现hello,world一样开心。
本来是元宵节就该发出来的博客,结果我写到一半,电脑也没关,就跑去看晚会去了,今天只能含着泪痛恨自己,没有收尾工作。