1
int strlength(const char *myStr){
    //variable used for str length counter
    int strlength = 0;
    //loop through string until end is reached. Each iteration adds one to the string length
        while (myStr[strlength] != '\0'){
            putchar(myStr[strlength]);
            strlength++;
        }

    return strlength;
}

Why will this not work as intended? I just want to find the length of a string.

SystemFun
  • 1,062
  • 4
  • 11
  • 21

4 Answers4

4

From a comment on another answer:

I am using fgets to read in a string. and i have checked to make sure that the string that was typed was stored correclty

In that case, there is a trailing newline stored, so your function computes

strlength("hello\n")

The code is correct, you just didn't pass it the input you believed to pass.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • What do you want to avoid exactly? If you don't want a `\n` at the end of the string, just remove it. (Or, if `fgets` doesn't do what you want, don't use it.) – David Schwartz Jan 26 '13 at 19:40
  • @Vlad If you have POSIX, you can use `getline()`. Otherwise, compute the length, and overwrite the newline [if there is one, if the input was longer than the size passed to `fgets`, there won't be one]. – Daniel Fischer Jan 26 '13 at 19:41
  • @David. I'm unsure I understand fgets properlyy then. Will it always store a '\n'? – SystemFun Jan 26 '13 at 19:42
  • @Vlad: No. It will only store a newline in the string if it reads a newline. "`fgets()` reads in at most one less than `size` characters from `stream` and stores them into the buffer pointed to by `s`. Reading stops after an `EOF` or a newline. If a newline is read, it is stored into the buffer. A `\0` is stored after the last character in the buffer." – David Schwartz Jan 26 '13 at 19:44
  • @David. Well then it will always store a '\n' Unless you hit enter after the maximum length specified is reached correct? – SystemFun Jan 26 '13 at 19:48
  • @Vlad If there is a newline in the input stream, `fgets` will store it if there is space in the buffer (according to the size argument). – Daniel Fischer Jan 26 '13 at 19:48
  • @Vlad: Consider a file that only contains the letter "A". Reading stops at end of file. – David Schwartz Jan 26 '13 at 19:49
  • Ok thanks. I think I understand. Is there a better option than fgets() if getline() is not avilable? – SystemFun Jan 26 '13 at 19:51
  • @Vlad Depends on what "better" means. You could also `scanf("%99s", myStr);` [assuming `myStr` has space for 100 `char`s], that will leave the newline in the input buffer and not store it. You can also write your own input function repeatedly calling `getchar()`. – Daniel Fischer Jan 26 '13 at 19:54
3

More reliable version:

size_t strlength(const char * myStr)
{
    return strlen(myStr);
}
James M
  • 18,506
  • 3
  • 48
  • 56
  • 1
    I think this kinda defeats the purpose of the OP's function though :P. still +1 for mentioning the conventional way. – Kevin Jan 26 '13 at 19:34
  • It's not homework. At all. I can easily fix it by subtracting 1 from my return. but i shouldn't need to and I dont know why – SystemFun Jan 26 '13 at 19:37
2

You can try this also:-

int string_length(char *s)
{
   int c = 0;

   while(*(s+c))
      c++;

   return c;
}
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

No need to worry about fgets() and removing the trailing \n.

while (myStr[strlength] != '\0'){
            putchar(myStr[strlength]);
            strlength++; //When mysStr[strlength] == \0, this line would have already incremented by 1
}

Quick fix:

return (strlength-1);//This will return correct value.

A more better approach:

int strlen(const char *s)
{
   char *str=s;
   while(*str)
   {
      str++;
   }
   return (s-str);
}
askmish
  • 6,464
  • 23
  • 42