#include <stdio.h>
int my_strlen(char s[]){
int i = 0;
while (s[i]!= '.'){
i++;
}
return i;
}
int main() {
char a[1001];
scanf("%[^\n]", a);
printf("%d", my_strlen(a));
return 0;
}
This code defines a function `my_strlen` that counts characters in a string until it encounters a period `.` instead of the null terminator `\0`. The main function reads a line of input and prints the count returned by `my_strlen`.
- The `while` loop condition uses `s[i] != '.'` but the standard way to find the end of a string in C is to check for the null character `'\0'`, not a period. This means the function will not correctly measure the length of strings that do not contain a period.
- The `scanf` format `"%[^\n]"` reads everything except a newline, but it does not limit the input length, which could cause a buffer overflow if the input exceeds 1000 characters. Consider using a width specifier like `"%1000[^\n]"` to safely read up to 1000 characters.