当前位置:网站首页>C语言求字符串的长度
C语言求字符串的长度
2022-07-29 05:08:00 【cpp编程】
C语言写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度,要求用指针。
解题思路:求字符串的长度,还要求用指针,读者首先应该想一下不用指针是否可以,求字符串的长度需要判断字符串的结束标志。
C语言源代码演示:
#include<stdio.h>//头文件
int main()//主函数
{
int sum_Length(char *point);//函数声明
int len;//定义整型变量
char str[20];//定义字符数组
printf("请输入要求长度的字符串:");//提示语句
scanf("%s",str);//因为str是数组,不用加取地址符&
len=sum_Length(str);//函数调用
printf("字符串的长度是%d\n",len);//输出结果
return 0;//主函数返回值为0
}
int sum_Length(char *point)//函数功能的实现
{
int number=0;//定义整型变量
while(*point!='\0')
{
number++;
point++;
}
return number;//将number返回到函数调用处
}
编译运行结果如下:请输入要求长度的字符串:Cyuyan
字符串的长度是6
--------------------------------
Process exited after 2.775 seconds with return value 0
请按任意键继续. . .
今天的分享就到这里了,大家要好好学C语言/C++哟~
写在最后:对于准备学习C/C++编程的小伙伴,如果你想更好的提升你的编程核心能力(内功)不妨从现在开始!
C语言C++编程学习交流圈子,QQ群:763855696【点击进入】
C语言从入门到精通(C语言入门C语言教程C语言零基础C语言基础C语言学习C
整理分享(多年学习的源码、项目实战视频、项目笔记,基础入门教程)
欢迎转行和学习编程的伙伴,利用更多的资料学习成长比自己琢磨更快哦!
编程学习视频分享:
边栏推荐
猜你喜欢
Network Security Learning - Intranet Security 1
Open source Huizhi creates the future | the openeuler sub forum of 2022 open atom global open source summit was successfully held
Create a mindscore environment in modelars, install mindvision, and conduct in-depth learning and training (Huawei)
ARFoundation从零开始3-创建ARFoundation项目
Qml控件:ComboBox
"Invisible Bridge" built in the free trade economy: domestic products and Chinese AI power
What if the computer cannot open excel? The solution of Excel not opening
Mysql的自连接和联合查询
Qml类型:MouseArea
6.2 function-parameters
随机推荐
P2181 diagonal
Deadlock analysis using jstack, jconsole, and jvisualvm
[sudden] solve remote: support for password authentication was removed on August 13, 2021. please use a perso
网安学习-内网安全1
[wechat applet -- solve the alignment problem of the last line of display:flex. (discontinuous arrangement will be divided into two sides)]
Cache penetration, cache breakdown, cache avalanche and Solutions
[untitled]
Numpy Foundation
Quick start JDBC
ODOO开发教程之透视表
Google GTEST event mechanism
Google gtest事件机制
roLabelImg转DATO格式数据
关于thymeleaf的配置与使用
7.2-function-overloading
Open the tutorial of adding and modifying automatically playing music on the open zone website
On AspectJ framework
小白高薪捷径-Qt开发游戏—贪吃蛇
AttributeError: ‘module‘ object has no attribute ‘create_ connection‘
ARFoundation从零开始8-Geospatial API(地理空间)开发