首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

一段求字符串长度的代码,求改错解决思路

2012-04-05 
一段求字符串长度的代码,求改错C/C++ code#include stdafx.hint _tmain(int argc, _TCHAR* argv[]){int

一段求字符串长度的代码,求改错

C/C++ code
#include "stdafx.h"int _tmain(int argc, _TCHAR* argv[]){    int len=0;    char s1[80];    int str_len(char);    printf("enter the string:\n");    scanf("%s",s1);    len=str_len(s1[80]);    printf("the length is :%d\n",len);    return 0;}int str_len(char s[]){    int i=1;    while(s[i]!='\0')    {    i++;}    return i;}


[解决办法]

不知道这里是不是有啥陷阱和玄机..
我随便改了一个,是这个意思么......

#include<stdio.h>

int str_len(char[]);

int main()
{
int len=0;
char s1[80];
printf("enter the string:\n");
scanf("%s",s1);
len=str_len(s1);
printf("the length is :%d\n",len);
return 0;
}

int str_len(char s[])
{
int i=0;
while(s[i]!='\0')
{
i++;
}
return i;
}

[解决办法]
函数的声明在main之外
[解决办法]
C/C++ code
#include <iostream.h>#include <stdio.h>int main(int argc, char* argv[]){    int len=0;    char s1[80];    int str_len(char*);    printf("enter the string:\n");    scanf("%s",s1);    len=str_len(s1);    printf("the length is :%d\n",len);    return 0;}int str_len(char* s){    int i=1;    while(s[i]!='\0')    { i++;}    return i;} 

热点排行