当前位置:网站首页>[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,进步神速!
边栏推荐
猜你喜欢
随机推荐
双指针法 | leecode刷题笔记
VMware启动报错:另一个程序已锁定文件的一部分,进程无法访问(删除最近的.lck文件夹)
实时数仓架构演进及选型
数字孪生园区场景中的坐标知识
从Oracle日志解析学习数据库内核原理
Oracle 11g rac打完补丁,dbca新建数据库还需要执行应用补丁的sql吗?
js商品总价格、最高价格商品、排除重复商品[初版]
nacos集群配置详解
Flink SQL搭建实时数仓DWD层
金仓数据库KingbaseES安全指南--6.13. 关于身份验证的常见问题
Default username and password (SQL)
扎克伯格“喜迎”苹果AR产品,上市两年终迎大幅涨价
Mysql——分组统计
红蓝对抗经验分享:CS免杀姿势
3.NVIDIA Deepstream开发指南中文版--Deepstream 环境配置
锁定和并发控制(一)
Locking and Concurrency Control (4)
Nacos配置中心用法详细介绍
Coordinate knowledge in digital twin campus scenarios
链表的归并排序[自顶向下分治 || 自低向上合并]