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.

9
  • 3
    And in what way doesn't it work? Commented Jan 26, 2013 at 19:28
  • I keep getting an off by one error. I.E. hello returns 6 Commented Jan 26, 2013 at 19:28
  • Duplicate of C size char* array Commented Jan 26, 2013 at 19:31
  • What's the calling code to this? Commented Jan 26, 2013 at 19:32
  • 1
    If you're getting 6 instead of 5, you might do better printing printf(" %d", mySte[strlength]); so you can see exactly what's being processed. But the code should produce 5 for "hello". Are you sure you're testing what is written in the question? Commented Jan 26, 2013 at 19:33

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

9 Comments

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.)
@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].
@David. I'm unsure I understand fgets properlyy then. Will it always store a '\n'?
@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. Well then it will always store a '\n' Unless you hit enter after the maximum length specified is reached correct?
|
3

More reliable version:

size_t strlength(const char * myStr)
{
    return strlen(myStr);
}

2 Comments

I think this kinda defeats the purpose of the OP's function though :P. still +1 for mentioning the conventional way.
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
2

You can try this also:-

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

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

   return c;
}

2 Comments

Please explain the concept/concepts behind this.
Got it when the *(s+c) expression returns 0 as it encounters a null terminating character.. The loop stops! right? Incredible!
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);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.