当前位置:网站首页>有趣的C语言
有趣的C语言
2022-07-26 22:50: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全部被替换。
今天的分享就到此为止了,如果觉得写得还可以,关注一波再走,后期更精彩。
边栏推荐
- OSPF的重发布及路由策略
- Mechanical hard disk Selection Guide -- from the selection experience
- HCIA Basics (1)
- First knowledge of C language (2)
- Educational Codeforces Round 132 (Rated for Div. 2), problem: (D) Rorororobot
- Influence of pre frequency division value and automatic reload value on interrupt frequency
- Tcp的三次握手与四次挥手
- TCP的三次握手与四次挥手(简述)
- About unsafe problems such as fopen and strError encountered in vs2022 or advanced version running environment
- Lora通信应用开发
猜你喜欢

HCIA动态路由RIP基础实验

HCIA静态路由基础模拟实验
![[explain C language in detail] takes you to play with loop structure (for_while_do while)](/img/d9/75053297873a5b5458514e7f557cdc.png)
[explain C language in detail] takes you to play with loop structure (for_while_do while)

MySQL course 1. simple command line -- simple record welcome to supplement and correct errors

C语言——关系运算符和逻辑运算符、if语句、switch语句、分支结构的嵌套

二层封装技术(HDLC、PPP--PAP\CHAP、GRE)实验练习

C语言实现小游戏【三子棋】注释详细 逻辑清晰 快来看看吧!!

Esp8266wi fi data communication

Static comprehensive experiment (comprehensive exercise of static route, loopback interface, default route, empty interface, floating static)

7.16 written examination of Duoyi network
随机推荐
(题意+详细思路+加注释代码) Codeforces Round #805 (Div. 3)F. Equate Multisets
6.28 flush written test
OSPF configuration in mGRE environment and LSA optimization - reduce the amount of LSA updates (summary, special areas)
记录HandsomeBlog的star用户
Codeforces Round #807 (Div. 2), problem: (C) Mark and His Unfinished Essay
STM32入门教程第一讲
Text to image paper intensive reading ssa-gan: text to image generation with semantic spatial aware Gan
多线程中 synchronized 锁升级的原理是什么?
Experiment exercise of two-layer packaging technology (HDLC, ppp--pap\chap, GRE)
Golang - sync包的使用 (WaitGroup, Once, Mutex, RWMutex, Cond, Pool, Map)
TCP's three handshakes and four waves (brief introduction)
HandsomeForum学习论坛
lvs+keepalived项目实战
RS-485总线通信应用
Lecture 3 - GPIO input / output library function usage and related routines
第四讲—讲解GPIO_Write函数以及相关例程
Mechanical hard disk Selection Guide -- from the selection experience
最新京东短信登录+傻妞机器人保姆级部署教程(2022/7/24)
7.16 written examination of Duoyi network
数字芯片的面积优化:第三届“华为杯”研究生创芯大赛数字方向上机题1详解