0

I'm attempting to make strlen in C standard library on my own.
Following is my code and the strlen func I made is given the name mystrlen in the script below.

   #include <stdio.h>
  
   //int mystrlen(const char* str);
  
   int mystrlen(const char* str){
           int i;
           for (i=0;str[i]!='\0';i++);
           return i;
  }
 
 
  int main(void){
          char input[100];
          fgets(input,sizeof input, stdin);
          printf("The length of your string : %d\n", mystrlen(input));     
          return 0;
  }                        

However, when I executed the compiled code, the output always shows the length of input string one greater than its actual size.

Example: (input is abc)

abc
The length of your string : 4

I think this is because my code counts '\0', the last character in the string as one character. I have searched for the Internet about how to avoid count the NULL character, but I didn't find any useful info.

Could you please tell me how to solve this problem? Thank you.

XYJ
  • 11
  • 5
  • There's probably a newline character there. – 500 - Internal Server Error Dec 31 '22 at 14:37
  • Your `mystrlen` function appears to be correct (except the return type of the function and the variable `i` should have been `size_t`). Note that the `fgets` function retains the newline (`'\n'`) character, if there is one. – M. Nejat Aydin Dec 31 '22 at 14:39
  • The 4th `char` in your string is `\n` (newline). Your `mystrlen` is reporting the proper length, not including the zero termination. – wohlstad Dec 31 '22 at 14:39
  • Thank you all for your answers. I have understand the reason why my codes didn't work well. – XYJ Dec 31 '22 at 14:45
  • In my code, the end condition of ```for``` loop is set to ```str[i]!='\0'```, therefore my understanding is that ```'\0'``` comes after the ```'\n``` (the newline character), which is retained by ```fgets```. Is this right? I appreciate it if anyone could give me some comments. – XYJ Dec 31 '22 at 14:49
  • @XYJ: That is correct. The null character is always at the end of the string, because that is how the end of the string is defined. If `fgets` reads a `\n` character, then it will be before the null character. Note that `fgets` will not always read a newline character, for example if the line is too long to fit in the memory buffer or when reading a file whose last line does not contain a newline character. But in most cases, it will read a newline character. – Andreas Wenzel Dec 31 '22 at 17:00
  • @AndreasWenzel Thank you for your response. I now understand more about ```fgets```. I've come to understand that ```fgets``` can limit the length of reading string, which sometimes results in the input string not terminating by ```\0```. – XYJ Jan 01 '23 at 04:50
  • @XYJ: The input string will always be terminated by `\0`. What the input string will not always have is a `\n` newline character. However, this only applies if the function `fgets` reports success. If the function `fgets` reports failure (which is indicated by the return value of the function), then you cannot rely on either character being present. – Andreas Wenzel Jan 01 '23 at 10:58
  • @AndreasWenzel Thank you for your notice. I used to be a little perplexed about the distinction between ```\0``` and ```\n```, but I now grasp it. – XYJ Jan 01 '23 at 12:20

0 Answers0