1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <stdio.h> int my_strlen(char *s) { char *start = s, *end = s; while(*end) end++; return (end - start) / sizeof(char); } void main() { char buf[30]; while (1) { printf("input string: "); fgets(buf, 30, stdin); for (int i = 0; buf[i]; i++) if (buf[i] == '\n') buf[i] = '\0'; if (buf[0] == '\0') break; printf("string length: %d\n", my_strlen(buf)); } }