我写的代码如下
void StringCount( char s[] )
{
int letter = 0, blank = 0, digit = 0, other = 0, i;
int n = strlen(s);
for ( i=0; i<n; i++ ) {
if ( (s[i]>='a'&&s[i]<='z') || (s[i]>='A'&&s[i]<='Z') )
letter++;
else if ( s[i]==' ' || s[i]=='\n' )
blank++;
else if ( s[i]>='0'&&s[i]<='9' )
digit++;
else
other++;
}
printf("letter = %d, blank = %d, digit = %d, other = %d", letter, blank, digit, other);
}
虽然得到的结果是正确的,但是由于前面的头文件没有string.h,会出现warning
warning: implicit declaration of function ‘strlen’ [-Wimplicit-function-declaration]
int n=strlen(s);
^~~~~~
a.c:26:11: warning: incompatible implicit declaration of built-in function ‘strlen’
a.c:26:11: note: include ‘<string.h>’ or provide a declaration of ‘strlen’
参考了一下别人的方法,找到解决办法了 ,修改如下
int n = 0;
while ( s[n] != '\0')
n++;