首页 > 试题广场 >

获取字符串长度

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

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


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

输入

helloworld

输出

10
# 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 <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>
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<stdio.h>
int main() {
    char str[100];
    fgets(str, 100, stdin);//stdin读入从键盘输入的数据
    int n = 0;
    char* p = str;
    while (*p++ != '\n') {//fgets输入长度小于指定长度时会在字符串结束符'\0'之前添加换行符\n
        n++;
    }
    printf("%d\n", n);
    return 0;
}

发表于 2023-11-18 20:29:23 回复(0)
#include <stdio.h>
#include <string.h>

int main() {
    char str[100] = {0};
    char *ptr = str;
    int cnt = 0;
      
    scanf("%s",ptr);

    while((*ptr) != '\0')
    {
        cnt++;
        ptr++;
    }

    printf("%d",cnt);

    return 0;
}

发表于 2023-09-25 15:56:08 回复(0)
#include <cstring>
#include <stdio.h>
#include <string.h>

int main() {

    char name[100]="";
    scanf("%[^\n]", &name);
    printf("%d", int (strlen(name)));
    return 0;
}


发表于 2023-08-26 21:32:59 回复(0)
#include <iostream>
using namespace std;

int main() {

    char str[100] = { 0 };
    cin.getline(str, sizeof(str));
    
    // write your code here......
    int n=0;
    char *p=str;
    while(*p++)
        n++;
    cout<<n;
    return 0;
}

发表于 2023-05-27 16:55:32 回复(0)
#include <stdio.h>

int my_strlen(char * pa)
{
    char * copy = pa;
    while (*copy++ != '\0') ;
    return copy - pa -1;
}

int main() {
    char arr[100] = {0};
    //scanf("%s", arr);
    gets(arr);
    printf("%d", my_strlen(arr));
    return 0;
}

发表于 2023-03-18 20:59:59 回复(0)
#include <stdio.h>

int mystrlen(char *a)
{
    int count = 0;
    while(*a++ != '\0')
    {
        count++;
    }
    return count;



}



int main(void)
{
    char ch[50];
    char * ch1 = ch;

    while((*ch1 = getchar()) != EOF)
    {
        ch1++;
        



    }
    *--ch1 = '\0';
    printf("%d",mystrlen(ch));



    return 0;
}

发表于 2022-11-05 22:11:16 回复(0)
#include <stdio.h>
#include <string.h>

int my_strlen(char *arr, int sz) {
//	char *p = arr;
	int cnt = 0;
	int count = 0;
	while (*arr != '\0') {
		arr++;
		cnt++;
	}
//	for (int i = 0; i < sz; i++) {
//		if (arr[i] == ' ') {
//			count++;
//		}
//	}
	return cnt;
}

int main(void) {
	char arr[20] = {0};
	scanf("%[^\n]",arr);
	int sz = sizeof(arr) / sizeof(arr[0]);
	int ret = my_strlen(arr, sz);
	printf("%d", ret);
	return 0;
}

发表于 2022-08-24 12:36:14 回复(0)
#include <iostream>
using namespace std;

int main() {

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

    // write your code here......
    char* uo = str;
    int i = 0;
    int mun = 0;
    for(i=0;i<sizeof(str);i++)
    {
        if(*(uo+i)!='\0')
        {
            mun++;
        }
    }
    printf("%d",mun);

    return 0;
}
发表于 2022-08-18 20:10:14 回复(0)
#include <iostream>
#include <string.h>
using namespace std;

int main() {

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

    // write your code here......
    int num = strlen(str);
    printf("%d",num);

    return 0;
}

发表于 2022-08-06 20:30:14 回复(0)
为什么遇到空格就会停止?
发表于 2022-07-25 20:42:28 回复(5)
#include <stdio.h>
void get_line(char* p)
{
    int count = 0;
    while (*p++ != '\0')
    { 
        count++;
   }
    printf("%d", count);
}
int main()
{
    char arr[100];
    scanf("%[^\n]",arr);
    get_line(arr);
    return 0;
}
发表于 2022-05-19 12:50:33 回复(0)
#include<stdio.h>
#include<string.h>
int main()
{
char arr[]="helloworld";
printf("%d",strlen(arr));
return 0;
}
发表于 2022-05-09 16:17:33 回复(9)

问题信息

难度:
16条回答 1469浏览

热门推荐

通过挑战的用户

查看代码