代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *create(int n)
{
char *s = (char *)malloc(sizeof(char)*n);
if(s == NULL)
return s;
return s;
}
size_t my_strlen(const char *s)
{
int i = 0;
while(*(s + i))
{
i++;
}
return i;
}
char *free_space(char *s)
{
if(s == NULL)
return NULL;
free(s);
s = NULL;
return s;
}
int main(int argc, const char *argv[])
{
int n, i;
int count = 0;
printf("please enter n:");
scanf("%d", &n);
char *s = create(n);
getchar();
//scanf("%s", s);
for(i = 0; i < n - 1; i++)
{
scanf("%c", s + i);
if(*(s + i) == '\n')
break;
}
*(s + i) = 0;
int len = my_strlen(s);
printf("%d\n", len);
s = free_space(s);
return 0;
}
结果
思维导图