大家来编一个c语言程序 5

来源:百度知道 编辑:UC知道 时间:2024/07/02 06:27:45
求一个字符串的长度

书上的题目就是这样的

下列代码可以直接运行。是c
#include <stdio.h>
int main()
{
char test_string[1024];

printf("please input a string\n");
scanf("%s",&test_string);

int i = 0;
for (;test_string[i]!='\0';i++) { }

printf("the length of string %s is %d\n",test_string,i); return 0;
}

int length(char* s)
{
int i=0;
if (s==0)
return 0;
while (*s++) i++;
return i;
}

请把不符合C规范的改成C, 我用的C++

#include<stdio.h>
#include<string.h>
void main()
{
int i=0;
char str[100];
printf("Please input the string:\n");
scanf("%s",&str);
for(;*str!=null;str++)
i++;
printf("This string's size is:%d\n",i);
}