C语言指针解答:写一个函数,求一个字符串长度,在main函数中输入字符串,并输出其长度

2025-04-06 17:45:38
推荐回答(4个)
回答1:

#include "stdio.h"
#include "string.h"

void main()
{
int len;
char a[100];
printf("输入字符串:");
scanf("%d",a)//或者gets(a);
len=strlen(a);
printf("长度为:%d\n",len);
}
可以了~~~

回答2:

#include
#define Maxsize 20
int makesize(char *p)
{
int count=0;
while(*p++)
count++;
return count;
}
int main()
{
char name[Maxsize];
printf("输入字符串:\n");
gets(name);
printf("字符串长度是%d",makesize(name));
return;
}

回答3:

#include
#include

int get_strlen(string *str);
int main()
{
string *str;
int length;
printf("Please input the string:");
scanf("%s",str);
length = get_strlen(*str);
printf("The length of the string is : %d\n",length);
return 0;
}
int get_strlen(string *str)
{
int count = 0;
while(*str)
{
++count;
*str++;
}
return count;
}
没有编译过,不知道能不能编译!

回答4:

#include
main()
{
int nl;
nl=0;
while(getchar()!=EOF)
nl++
printf("%d",nl);
}