当前位置:网站首页>有趣的C语言
有趣的C语言
2022-07-27 04:23:00 【GD_small_bit】
C语言作为编程界语言的老大哥,常年排列在年度最受欢迎的编程语言榜单的前三名中,吸引无数多的青年们去学习,正所谓“C生万物”嘛,C语言建立在二进制上,而其他语言建立在C语言上,所以,我今天将为大家带来一个个比较有趣的小程序,难度较易,适合接触C语言不久的人观看。
1.这个程序可以让一排#在多次打印的过程中,左右两边同时被另外一些字符替代,直到最后#全部被替代掉。
在编写代码之前,我们需要先在里面建立两个两字符数组,并且我们要保证两个字符数组长度要相同,如下:
int main ()
{
char arr1[]="Thank for reading my article";
char arr2[]="############################";
}
接下来我们都知道数组可以由下标访问,并且下标是从零开始的,如图:

我们可以通过下标左右同时一个一个的访问,然后再进行修改。那问题来了,怎么一个一个访问呢,这就要求我们的每次访问时下标都不一样,也就是下标时刻要发生变化,直到数组的全部元素都已经访问了。那我们就可以设置一个变量left和变量right和一个循环,在这个循环每进行一次,我的两个变量就变化一次,这样也就满足我们的要求了,如下:
那么如何变化呢,这就要求我们要进一步思考了,变量right从右边作为下标开始,既然要向中间移动,就要让变量right每次减1,同样的道理,变量left作为最左边开始,要向中间移动,每次就要增加1;
然后我们再加个strlen的库函数计算数组长度,进而求出变量right的初始值,也就是strlen求出来的值减一,保证能让数组访问到最后一个元素并且修改,并且加入到这个循环的限制条件,也就是变量right的值要时刻比变量left大。因为,变量right是从最右边开始作为下标并且发挥作用的,肯定要比变量left大,所以变量left<变量right也是循环的限制条件,保证我们的循环不会进入死循环。
#include<stdio.h>
#include<string.h>
int main ()
{
char arr1[]="Thank for reading my article";
char arr2[]="############################";
int sz=strlen(arr2)-1;
int left=0;
int right=sz;
while(left<right)
{
left++;
right--;
}
return 0;
}
最后,我们要让字符数组arr2被arr1替代,再打印arr2字符数组,如下:
#include<stdio.h>
#include<string.h>
int main ()
{
char arr1[]="Thank for reading my article";
char arr2[]="############################";
int sz=strlen(arr2)-1;
int left= 0;
int right= sz;
while(left<right)
{
arr2[left]=arr1[left];
arr2[right]=arr1[right];
left++;
right--;
printf("%s\n",arr2);
}
return 0;
}

当然,我们可以再添加一些函数,让这个结果不会太早出现。如:
在最前面的头文件添加include<windows.h>,然后在循环的里面最后一行加入Sleep(1000),让打印函数每进行一次,停止一秒,这样就有了持续变化的结果。
#include<stdio.h>
#include<string.h>
#include<windows.h>
int main ()
{
char arr1[]="Thank for reading my article";
char arr2[]="############################";
int sz=strlen(arr2)-1;
int left= 0;
int right= sz;
while(left<right)
{
arr2[left]=arr1[left];
arr2[right]=arr1[right];
left++;
right--;
printf("%s\n",arr2);
Sleep(1000);
}
return 0;
}
结果如下:


直到字符数组arr2全部被替换。
今天的分享就到此为止了,如果觉得写得还可以,关注一波再走,后期更精彩。
边栏推荐
- The external symbol parsed by the method "public: virtual _ucdecl nvinfer1:: yololayerplugin:: ~yololayerplugin (void)" "public: virtual
- Prometheus Node Exporter 常用监控指标
- Using JSON type to realize array function in MySQL
- Horizon sunrise X3 PI (IV) running on board (unfinished)
- QString转换char*
- 哈希表刷题(下)
- Session&Cookie&token
- JS modify the key value of the object array
- Head detached from origin/... Causes push failure
- 匿名命名管道, 共享内存的进程间通信理解与使用
猜你喜欢

Nacos startup and login

干货 | 独立站运营怎么提高在线聊天客户服务?
![[day02] Introduction to data type conversion, operators and methods](/img/81/e2c49a4206e5d0d05308a1fc881626.png)
[day02] Introduction to data type conversion, operators and methods

Anonymous named pipes, understanding and use of interprocess communication in shared memory

Dino paper accuracy, and analyze the variant of its model structure & Detr

shel自动设置目录权限

JS three methods of traversing arrays: map, foreach, filter

Final review of management information system
Full revolutionary networks for semantic segmentation (FCN)

第4章 Bean对象的作用域以及生命周期
随机推荐
Network knowledge corner | it only takes four steps to teach you to use SecureCRT to connect to ENSP. You must see the operation guide of common tools
好用的shell快捷键
Database leader Wang Shan: strive for innovation and carefully Polish high-quality database products
els_ 画矩形、代码规划和备份
From scratch, C language intensive Lecture 4: array
Technology sharing | gtid that needs to be configured carefully_ mode
Why does genericservlet have two init methods
【动态规划百题强化计划】11~20(持续更新中)
Spark practice case (upgraded version)
Deep analysis - dynamic memory management
Convolution neural network -- convolution of gray image
Brightcove appoints Dan Freund as chief revenue Officer
Is the e-commerce billing system important? How should the platform choose billing service providers?
无有线网络下安装并配置debian
Oracle数据库字段date怎么才能走索引?
5.component动态组件的展示
Prometheus node exporter common monitoring indicators
JMeter learning notes 004-csv file line number control cycle times
The external symbol parsed by the method "public: virtual _ucdecl nvinfer1:: yololayerplugin:: ~yololayerplugin (void)" "public: virtual
Understand kingbasees V9 in one picture