当前位置:网站首页>[C Language Brush Questions] Three Questions for Getting Started with Pointers | String Length, String Copy, Two Number Swap
[C Language Brush Questions] Three Questions for Getting Started with Pointers | String Length, String Copy, Two Number Swap
2022-08-02 19:06:00 【No. 7 Andy】
作者:柒号华仔
个人主页:欢迎访问我的主页
个人信条:星光不问赶路人,岁月不负有心人.
个人方向:主要方向为5G,同时兼顾其他网络协议,编解码协议,C/C++,linux,云原生等,感兴趣的小伙伴可以关注我,一起交流.
推荐一款模拟面试、刷题神器:*登录免费刷题
目录
前言
Brought it to my friends todayC语言刷题,一起刷题,Let's go through customs and interview together to enter the big factory,At the same time, brushing the questions is also reinforcement learning,The only way to consolidate knowledge.
The title comes from Niuke Tiba of Niuke.com,链接为:Click to jump to brush the artifact.经典高频面试题,应有尽有,各种编程语言,一网打尽.努力吧,我们都是赶路人!

)
CC1 获取字符串长度
1.1 题目描述
描述
键盘输入一个字符串,编写代码获取字符串的长度并输出,要求使用字符指针实现.
输入描述:
键盘输入一个字符串
输出描述:
输出字符串的长度
示例
输入: helloworld
输出: 10
1.2 题目解析
After the program reads the keyboard input string,字符串以’\0’作为结束符号,Use a pointer to point to this array of strings,Accumulate the length of the string by shifting the pointer,直到碰见’\0’结束.
1.3 代码实现
#include<stdio.h>
#include<string.h>
int main(void)
{
char a[64];
gets(a);
char *start=a;
char *end=a;
while(*end!='\0'){
end++;
}
printf("%d",end-start);
return 0;
}
CC2 复制部分字符串
2.1 题目描述
描述
键盘输入一个长度为len(1 <= len < 30)的字符串,再输入一个正整数 m(1 <= m <= len),将此字符串中从第 m 个字符开始的剩余全部字符复制成为另一个字符串,并将这个新字符串输出.要求用指针处理字符串.
输入描述:
键盘输入一个长度为len(1 <= len < 30)的字符串,再输入一个正整数 m(1 <= m <= len)
输出描述:
输出复制的新字符串
示例
输入: helloworld 6
输出: world
2.2 题目解析
When solving the problem, pay attention to entering the string first,Then enter the first few characters to start copying.数组是从0开始,因此mIt needs to be subtracted by one to start the calculation.The title requires processing with pointers,Arrays are not allowed.
2.3 代码实现
#include <stdio.h>
#include <string.h>
int main() {
char s[30] = {
0 };
gets(s);
int m = 0;
scanf("%d", &m);
char *p = &s[m - 1];
int n = strlen(s);
for (int i = 1; i < n - m + 1; i++) {
printf("%c", *(p + i));
}
return 0;
}
CC3 编写函数实现两数交换(指针方式)
3.1 题目描述
描述
编写一个函数,实现两个整数的交换,要求采用指针的方式实现.
输入描述:
键盘输入2个整数 m 和 n
输出描述:
输出交换后m 和 n 的值,中间使用空格隔开
示例
输入: 2 3
输出: 3 2
3.2 题目解析
The first thing to note is the use of pointers,为了实现交换,We need to define an intermediate volume for transition transformations.
3.3 代码实现
#include <stdio.h>
int main()
{
int m=0,n=0,temp=0;
int* pm=&m;
int* pn=&n;
int* pt=&temp;
scanf("%d\n%d",pm,pn);
*pt=*pm;
*pm=*pn;
*pn=*pt;
printf("%d %d\n",m,n);
return 0;
}
结语
纸上得来终觉浅,绝知此事要躬行,C语言的学习还是得基础知识+Do-it-yourself synchronization,Find a website where you can practice online,比如牛客网,多多练习,假以时日,Believe that the gains are huge,进步神速!
边栏推荐
猜你喜欢

【电子器件笔记6】三极管(BJT)参数和选型

js通过两种方式进行对商品价格排序

实时数仓架构演进及选型

安装TimeGen波形绘图软件

navicat创建连接 2002-can‘t connect to server on localhost(10061)且mysql服务已启动问题

工信部电子五所张志强:中国数据库行业发展趋势分析

融云「 IM 进阶实战高手课」系列直播上线

Continuous integration (4) Jenkins configuration alarm mechanism

Inconsistency between oracle and mysql statement results

数字孪生园区场景中的坐标知识
随机推荐
链表的归并排序[自顶向下分治 || 自低向上合并]
【genius_platform软件平台开发】第七十五讲:YUY2转RGB24实现源码
【面经】被虐了之后,我翻烂了equals源码,总结如下
【二】通过props进行传值,子页面多种方式接收
分类实验报告作业
图像质量评价指标
研发了 5 年的时序数据库,到底要解决什么问题?
mysql 《一》触发器
julia系列1:介绍与安装
乌总统解除乌克兰国家安全局信息和情报分析部负责人职务
总结嵌入式C语言难点 (1部分) 【结尾有资料】
MySQL常见函数
使用 LaunchPad 上的 TAO 工具包体验轻松创建 AI 模型
One article to understand DI dependency injection in php
软件技术功能开发思路
数字孪生园区场景中的坐标知识
Continuous integration (4) Jenkins configuration alarm mechanism
Pytest学习笔记
8大软件供应链攻击事件概述
RAID存储级别分类