in Programming in C recategorized by
379 views
4 votes
4 votes

The provided C code is a version of the C string library function strlen(), which calculates the length of a given string.

unsigned int mystrlen(char *c)
{
    unsigned int i = 0;
    /* Your code here. */
}


Which line of code creates a working and accurate version of strlen()?

  1.  
      if (*c == '\0') {
            return i;
        } else {
            return mystrlen(++c) + 1;
        }
    
  2.  
      if (*c == '\0') {
            return i;
        } else {
            return mystrlen(c++) + 1;
        }
    
  3.  
    while (*(c + i) != '\0') i++;
    return i;
    
  4.  
    while (*(c + i) != '\0') ++i;
    return i;
    

in Programming in C recategorized by
379 views

1 Answer

1 vote
1 vote
if (*c == '\0') {

    return i;

} else {

    return mystrlen(c++) + 1;

}

This is incorrect. The post-increment c++  leads to Infinite loop. It is better to use pre-increment ++c

Since in "mystrlen(c++)", same value is passed every time
Answer:

Related questions