程序找到字符串的长度 [英] A program to find length of string

查看:105
本文介绍了程序找到字符串的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/5685497/using-recursion-to-find-the-length-of-a-string-in-c\">Using递归找到一个字符串的长度用C

下面是我的code:

#include<stdio.h>
#include<string.h>
int string_length(char s[]);
main()
{
        char s[50];
        int l;
        printf("Enter the string\n");
        scanf("%s\n",s);
        l=strlen(s);
        printf("Length of string = %d\n",l);
}
int string_length(char s[])
{
        int i;
        i=0;
        while(s[i] != '\0')
                ++i;
                return i;

}

编译之后,它扫描两个输入值。

After compile it scan for two input values.

这有什么错我的code ??????

What's wrong with my code??????

推荐答案

摆脱换行的 scanf函数

scanf("%s",s);

这应该得到这个code工作。

That should get this code to work.

但我无法理解为什么你写一个函数来计算字符串的长度,如果你不得不使用的strlen()

But I am unable to understand why you wrote a function to compute string length if you had to use strlen().

HTH,结果
斯利拉姆。

HTH,
Sriram.

这篇关于程序找到字符串的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆