当时这到题,真的想了好久,大家别鄙视我哈.某次看ds的stack与递归的时候,我靠...递归不就OK了吗^_^不用你们鄙视我,我自己先鄙视下自己.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int count_str (char *);
int
main ()
{
char *str = (char *) malloc (sizeof (char) * 100);
printf ("Please input the string you want to count:\n");
fgets (str, 100, stdin);
printf ("The length of %s is mycount=%d,strlen=%d\n", str, count_str (str),
strlen (str));
return 0;
}
int
count_str (char *string)
{
if (*string == '\0')
return 0;
else
return (1 + count_str (++string));
}
gcc -o strlen strlen.c
./strlen
Please input the string you want to count:
this is a
The length of this is a
is mycount=16,strlen=16
看起来都很简单,可是真在那种环境下,还是很白痴的,
|
阅读(1235) | 评论(0) | 转发(0) |