首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
获取字符串长度
[编程题]获取字符串长度
热度指数:67679
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
算法知识视频讲解
键盘输入一个字符串,编写代码获取字符串的长度并输出,要求使用字符指针实现。
输入描述:
键盘输入一个字符串
输出描述:
输出字符串的长度
示例1
输入
helloworld
输出
10
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(0)
邀请回答
收藏(452)
分享
提交结果有问题?
103个回答
141篇题解
开通博客
牛客346353443号
发表于 2022-05-19 12:48:44
#include <stdio.h> void get_line(char* p) { int count = 0; while (*p++ != '\0') //指针接收统计 { count++; } printf("%d", c
展开全文
xqxls
发表于 2021-10-25 16:29:26
题意整理。 键盘输入一个字符串。 -获取该字符串的长度,并输出,要求使用指针实现。 方法一(指针) 1.解题思路 定义一个指针指向字符串开头。 只要指针对应元素不是'\n',将指针后移,同时长度加1。 动图展示: 2.代码实现 #include <iostream> using
展开全文
牛客202857351号
发表于 2022-03-01 09:02:11
#include #include<stdio.h> #include<string.h> int main() { char a[100]; gets(a); int t=0; t=strlen(a); printf("%d",t); return 0; }
源码被猫吃了
发表于 2021-11-10 22:52:19
#include <iostream> #include <cstring> using namespace std; int main() { char str[100] = { 0 }; cin.getline(str, sizeof(str));
展开全文
长青木
发表于 2022-04-01 19:56:03
首先定义一个指针 并让它指向字符数组, 字符数组的数组名表示字符串首字符的地址 再定义一个计数器 让其为0 再用while循环 从头开始遍历字符串 遍历一个让计数器加1个 直到遇到 \0 为止! 最后打印出计数器的值! 次计数器的值就是字符串的长度! #include <iostream&g
展开全文
一个细胞aaaa
发表于 2023-01-30 19:01:04
由于要求用指针来获取,所以设char数据类型的指针指向数组的首地址str。一开始写了数组str里面所有数字为0,如果输入完一串字符,数组里的一些数字0会被替代掉,那么这时候解引用p就不会得到0(p这个时候等于str[0]因为是首地址)因为str[0]已经被字符串覆盖。做一个循环让指针一个字节一个字节
展开全文
力纳克斯
发表于 2022-05-29 11:29:37
#include <stdio.h> int main() { char str[100] = {0}; char *p 
展开全文
一个菜鸟呢
发表于 2022-02-26 16:20:13
题目很简单,方法有很多,我这里用的是地址相减得出中间的元素个数 #include using namespace std; int main() { char str[100] = { 0 }; cin.getline(str, sizeof(str)); char *p1=str; while(
展开全文
风止意难平/
发表于 2022-11-08 20:22:20
#include<stdio.h> int main() { char *a;//先定义个字符指针
展开全文
长青木
发表于 2022-04-02 17:11:00
#include #include <string.h> using namespace std; int main() { char str[100] = { 0 }; cin.getline(str, sizeof(str)); // write your code here..
展开全文
问题信息
难度:
103条回答
452收藏
2395浏览
热门推荐
通过挑战的用户
查看代码
你丫三条
2023-03-14 17:41:20
在写文章的回笼...
2023-03-13 12:18:11
牛客88233...
2023-02-26 16:50:34
要暴富的你很想...
2023-02-19 16:00:33
zlskll
2022-12-08 20:02:15
相关试题
某时刻系统资源总数(8,5,7),...
操作系统
评论
(1)
MySQL中,查询created_...
SQL
评论
(1)
关于 asyncio 并发模型,以...
Python
评论
(1)
循环队列在固定大小的数组实现中的核...
队列
评论
(2)
LoRA(Low-Rank Ada...
大模型开发
评论
(2)
获取字符串长度
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
#include
using namespace std; int main() { char str[100] = { 0 }; cin.getline(str, sizeof(str)); // write your code here...... return 0; }
#include
int main() { char str[100] = { 0 }; gets(str); // write your code here...... return 0; }
helloworld
10