山科日记—求字符串的长度(编程题)

Description

编写一个求字符串长度的函数,其原型如下:

int strlen(char str[]);

其中str[]表示待求长度的字符串,返回值是str[]的长度。

注意:主函数已经给出,只需提交strlen()函数及必要的头文件包含命令。

Input

输入为多行。第一行N>0表示有N个测试用例,后面有N行,每行包含一个字符串(不超过1000个字符)。

Output

输出为多行,每行对应于一个测试用例。每行的格式为:

case i:lenght=j.

其中i表示测试用例编号(从1开始),j表示相应的字符串长度。

Sample Input

4

I love China!

Do you want to pass this examination?

You will succeed finially!

Wish you succeed!

Sample Output

case 1:length=13.

case 2:length=37.

case 3:length=26.

case 4:length=17.

HINT

Append Code

append.c,

int main()

{

    int i,N;

    char str[1001];

    scanf("%d",&N);

    getchar();

    gets(str);

    printf("case 1:length=%d.",strlen(str));

    for (i=2;i<=N;i++)

    {

        gets(str);

        printf(" case %d:length=%d.",i,strlen(str));

    }

    return 0;

}

#include <stdio.h>
//#include <stdlib.h>
//#include <math.h>
//#include <string.h>
int strlen (char str[])
{
    char  *p=str;
    int i=0;
    while(*p!='')
    {
        i++;p++;
    }

    return i;
}
int main()
{
    int i,N;
    char str[1001];
    scanf("%d",&N);
    getchar();
    gets(str);
    printf("case 1:length=%d.
",strlen(str));
    for (i=2;i<=N;i++)
    {
        gets(str);
        printf("
case %d:length=%d.
",i,strlen(str));
    }
    return 0;
}

  

作者:7oDo

仅供参考,请勿抄袭。

Hang Hang Hang !!!

原文地址:https://www.cnblogs.com/Jie-Fei/p/8298937.html