首页 > 试题广场 >

获取字符串长度

[编程题]获取字符串长度
  • 热度指数:57825 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
键盘输入一个字符串,编写代码获取字符串的长度并输出,要求使用字符指针实现。

输入描述:
键盘输入一个字符串


输出描述:
输出字符串的长度
示例1

输入

helloworld

输出

10
#include<stdio.h>
#include<string.h>
int main()
{
char arr[]="helloworld";
printf("%d",strlen(arr));
return 0;
}
发表于 2022-05-09 16:17:33 回复(10)
int my_strlen(char s[])

{
    int len = 0;
    char *p = s;
    while ( *p != '\0')
    {
        len++;
        p++;
    }
    return len;
}
int main()
{
    char str[100];
    scanf("%s", str);
    my_strlen(str);
    printf("%d\n", my_strlen(str));
    return 0;
}
发表于 2022-03-23 20:54:04 回复(1)
#include <bits/stdc++.h>
#include <iostream>
using namespace std;

int main() {

    char str[100] = { 0 };
    cin.getline(str, sizeof(str));

    // write your code here......
    char *p = str;
    int cnt = 0;
    while((*p)!= '\0'){
        cnt ++;
        p ++;
    }
    cout<< cnt << endl;


    return 0;
}

发表于 2022-01-13 16:14:30 回复(1)
#include<stdio.h>
//#include<string.h>
//获取字符串长度
int main()
{ 
char c;
int l=0;
while((c=getchar())!='\n')
{
    l++; 
}
printf("%d",l);

}

发表于 2022-05-08 15:29:06 回复(5)
//解题思路:创建一个指针变量开始指向首元素,通过循环一直移向字符串末尾'\0'停下,
//再用尾指针减头指针就等于中间元素的个数,即字符串长度。
#include <iostream>
using namespace std;

int main() {

    char str[100] = { 0 };
    cin.getline(str, sizeof(str));

    // write your code here......
    scanf("%s",&str);
    //获取字符串长度
    char* end = str;
    while(*end != '\0') end++; //找到字符串末尾
    printf("%d\n",end - str); //尾指针减头指针等于中间元素的个数 
    
    return 0;
}

发表于 2022-05-19 21:11:55 回复(1)
#include <iostream>
using namespace std;

int main() {

    char str[100] = { 0 };
    cin.getline(str, sizeof(str));

    char* pr = str;
    while (*(pr++)!='\0');
    cout << pr - str - 1 << endl;

    return 0;
}

发表于 2023-04-23 19:14:35 回复(0)
#include <iostream>
using namespace std;

int main() {

    char str[100] = { 0 };
    cin.getline(str, sizeof(str));

    // write your code here......
    int i = 0;
    while (str[i] != '\0')i++;
    cout << i;

    return 0;
}

发表于 2023-04-10 23:45:06 回复(0)
#include <iostream>
#include<string>
using namespace std;

int main() {
    string a;
    cin >> a;
    string b=a;
    cout << b.size() << endl;
}
// 64 位输出请用 printf("%lld")
发表于 2022-10-04 21:17:07 回复(0)
#include <iostream>
using namespace std;

int main() {

    char str[100] = { 0 };
    cin.getline(str, sizeof(str));

    // write your code here......
    int i=0;
    while(str[i]!='\0'){
        i++;
    }
    printf("%d",i);
    return 0;
}

发表于 2022-03-01 12:25:25 回复(0)
自测试可以跑进2ms, 但提交只能跑进3ms, 有没有大佬帮忙解惑一下, 那些排行榜的大佬都是怎么跑进2ms的, 我就算直接复制排行里大佬的代码提交还是跑不进2ms...(语言用的C, 但环境是C++)
#include <stdio.h>

int main() {
  int cChar = 0;
  // =========写法1
  while (++cChar, getchar() != EOF);  // '\n': cChar - 1
  printf("%d", cChar - 2);
  // =========写法2
  while (getchar() != '\n') ++cChar;  // EOF: cChar - 1
  printf("%d", cChar);
  return 0;
}




发表于 2024-08-04 04:51:50 回复(0)
#include <stdio.h>
#include<string.h>
int main(){
    char str [100] ={0};
    char *ptr = str;
    int cnt =0;
    scanf("%s",str);
while((*ptr) != \'0')
{
    cnt++;
    str++;
}  
    printf("%d\n",cnt);
return 0;
}
发表于 2024-07-17 16:39:01 回复(0)
#include <iostream>
#include <cstring>
using namespace std;

int main() {

    char str[100] = { 0 };
    cin.getline(str, sizeof(str));

    // write your code here......
    cout<<strlen(str)<<endl;

    return 0;
}

发表于 2024-02-02 20:05:37 回复(0)
#include<stdio.h>
int my_strlen(char* str)
{
char* arr=str;
whlie(*str!='\0')
{
str++;
}
return str-arr;
}
int main( )
{
int len=my_strlen("hellowworld");
printf("%d\n",len);
return 0;
}
编辑于 2024-01-05 18:13:31 回复(0)
#include <iostream>
using namespace std;

int main() {

    char str[100] = { 0 };
    cin.getline(str, sizeof(str));
    // write your code here......
    unsigned i=0;
    while (str[i]!=0) {
        i++;
        if(i>=sizeof(str)){
            break;
        }
    }    

    cout<<i<<endl;
    return 0;
}
发表于 2023-10-19 09:46:44 回复(0)
最全的求字符串长度的方法都在这里,欢迎浏览http://t.csdn.cn/7Tvte
发表于 2022-11-20 09:21:26 回复(0)
发表于 2021-11-25 20:53:33 回复(1)
你们为啥写那么复杂
#include <stdio.h>

int main() {

    char str[100] = { 0 };
    gets(str);

    // write your code here......
    char *ps = str;
    int i = 0;

    while(*(ps+i) != '\0')
    {
        i++;
    }

    printf("%d",i);

    return 0;
}


发表于 2025-06-18 13:08:58 回复(0)
#include<stdio.h>
int mystrlen(char *p)
{
    int count = 0;
    while(*p != '\0')
    {
        count++;
        p++;
    }
    return count;
}
int main()
{
    char arr[100] = {0};
    gets(arr);
    printf("%d",mystrlen(arr));
    return 0;
}
发表于 2025-02-09 20:16:00 回复(0)
#include <stdio.h> int main() { //键入一个字符串,计算其长度并输出。 printf("请输入一个字符串:\n"); char arr[100]; scanf("%s",&arr); //计算长度 int count=0; int i=0; while(arr[i]!='\0') { count++; i++; } //输出 printf("%d\n",count); return 0; }</stdio.h>
发表于 2025-01-28 19:22:55 回复(0)
#include <fstream>
#include <iostream>
using namespace std;

int main() {

    char str[100]={0};
    cin.getline(str,sizeof(str));
    char *p=str;char *p2=str;
    for (int i=0; i<sizeof(str); i++) {
     
     if (str[i]==0) {
     break;
     }
     p++;
    }
    // write your code here......
    cout<<p-p2;

    return 0;
}
发表于 2024-12-05 15:31:18 回复(0)