首页 > 试题广场 >

获取字符串长度

[编程题]获取字符串长度
  • 热度指数:43468 时间限制: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 回复(9)
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 回复(3)
//解题思路:创建一个指针变量开始指向首元素,通过循环一直移向字符串末尾'\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<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)
#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)
发表于 2021-11-25 20:53:33 回复(1)
# include <stdio.h>
int main()
{
    //记录一下,这是自己写出来的第一个代码
    int count = 0;
    int i = 0;
   
    char arr[15] = { 0 };
    char* str = arr;
    scanf("%[^\n]", arr);
    for (i = 0;*str!= '\0';i++)
    {
    count++;
    str++;
    }
    printf("%d", count);
    return 0;
}
编辑于 2024-04-17 17:54:12 回复(1)
#include <iostream>

int MyStrlen(const char* p)
{
    const char* end = p;

    while (*end)
    {
        end++;
    }

    return end - p;
}

int main()
{
    char str[80] = { 0 };
    int len = 0;

    std::cin.getline(str, 80);

    len = MyStrlen(str);

    std::cout << len << std::endl;

    return 0;
}

编辑于 2024-03-26 20:50:19 回复(0)
#include <stdio.h>
#include <string.h>
int main() {
    char a[100];
    printf("输入字符串:");
    fgets(a, sizeof(a), stdin);
    int t = 0;
    t = (int)strlen(a);
    printf("%d", t);
    return 0;
编辑于 2024-03-20 20:11:34 回复(0)
#include <cassert>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int getline(char* str){
    assert(str);
    int count=0;
    while (*str!='\0') {
        count++;
        str++;
    }
    return count; 
}
int main() {

    char str[100] = { 0 };
    //因为scanf取一行字符时碰到空格,制表符,\r,\n就结束了。
    //所以用%[^\n],这个格式符的意思是读\n之外的所有字符,也就是说读到/n为止!
    scanf("%[^\n]",str);
    int length=getline(str);
    printf("%d",length);
    
    

    return 0;
}

编辑于 2024-03-13 10:45:53 回复(0)
#include <stdio.h> #include<string.h> int main() { char a[ ]= "helloworld"; char *p; p = a; printf("%d\n", strlen(p)); system("pause"); return 0; }</string.h></stdio.h>
编辑于 2024-03-05 10:19:06 回复(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 <iostream>
using namespace std;

int main() {

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

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

发表于 2024-01-21 14:42:39 回复(0)