-6

I have been working on my C assignment where I try to replicate strlen() function without actually using it. This is the code I have been trying to get working. However, somehow the main function does not reflect what's happenning in mystrlen() function. Can you please tell me why it does not work as the strlen() function?

#include <stdio.h>

int mystrlen(char *input_string) {
/* This function returns the length of the input string */
/* WRITE FUNCTION CODE HERE! */
    char str1[50];
    int abcd = 0;
    scanf("%s", str1);
    int m;
    for(m=0; str1[m]; m++){
        abcd ++;
    }
    return 0;
}

int main(int argc, char **argv) {
    int length;
    if (argc!=2) {
        printf("Usage: strlen <input_string_with_no_space_inside_it>\n\n");
    return 1;
    }
    length = mystrlen(argv[1]);
    printf("The length is: %d characters.\n",length);
    return 0;
}

2 Answers2

1

This:

return 0;

should be:

return abcd;

That said, abcd is a terrible name for this variable, and the function makes little sense as a strlen() replacement. It doesn't touch its argument, and calls scanf() to read input from the user, which you really don't want a strlen() replacement to do.

Here's one way of writing it:

size_t mystrlen(const char *s)
{
  size_t len = 0;
  if(s != NULL)
  {
    while(*s != '\0')
    {
      ++len;
      ++s;
    }
  }
  return len;
}

Improvements include:

  • Proper size_t-typed return value (lengths are sizes, and cannot be negative) so size_t is proper and what the real strlen() uses.
  • Doesn't do any input-reading.
  • Uses the input argument.
  • Computes length and returns it.

It also handles being given NULL, as a bonus.

A matching main() that does what yours did could be:

int main(int argc, char **argv)
{
  if (argc != 2)
  {
    printf("Usage: strlen <input_string_with_no_space_inside_it>\n\n");
    return 1;
  }
  const size_t length = mystrlen(argv[1]);
  printf("The length is: %zu characters.\n", length);
  return 0;
}

This basically centers around the int-to-size_t change. Also note that in shells supporting quoting, you can run your program like this:

$ ./strlen "hello this is a string with spaces in it"

and it will pass that entire quoted string (sans quotes, of course) in argv[1].

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 1
    @nicomp Okay, can you bit a more specific about what problem you're seeing? The code in `main()` looks sane so I didn't replicate it. – unwind Sep 07 '17 at 09:59
  • there's plenty of insanity in the main(). – nicomp Sep 07 '17 at 10:02
  • 1
    @nicomp could you be more specific? Apart from poor formatting the `main` function looks perfectly ok for me. – Jabberwocky Sep 07 '17 at 10:05
  • @MichaelWalz See the answer posted below by another user: "First of all, you try to pass a string, and then you invoke scanf function in mystrlen. Delete this scanf. Secondly, your function always returns zero instead of value of variable abcd." – nicomp Sep 07 '17 at 11:46
  • @nicomp the comments of __this__ answer should be only about __this__ answer and not about other answers. I still don't understand your comment : _"there's plenty of insanity in the main()."_ – Jabberwocky Sep 07 '17 at 11:48
  • @MichaelWalz Never mind. You seem to only want to argue rather than study what I took the time to write to you. – nicomp Sep 07 '17 at 11:51
  • @nicomp just trying to understand _"there's plenty of insanity in the main()."_. – Jabberwocky Sep 07 '17 at 11:53
  • funny thing is that main function is entirely written by the professor. I can only make changes within the mystrlen() function. That's why I am having difficulty with this assignment... – Orhan Gazi Yalçın Sep 07 '17 at 12:28
  • @OrhanGaziYalçın Not all professors read SO. :) Switch it back down to `int` instead of `size_t` to gain compatibility if you can't change `main()`. – unwind Sep 07 '17 at 13:24
  • Works like a charm! My main lack of knowledge was your last point: I did not know that I could pass a string right after ./strlen; therefore, I have been unsuccessfully trying to scan it :) I have updated your code with int variables and it works in harmony with the original code! Despite receiving many negative comments from the other users, you were kind and very helpful. I deeply appreciate that. @unwind – Orhan Gazi Yalçın Sep 07 '17 at 14:09
  • @OrhanGaziYalçın Thanks, glad I could help. Not sure what happened with this particular question, it was quite the downvote-sink there for a while. – unwind Sep 07 '17 at 14:47
0

First of all, you try to pass a string, and then you invoke scanf function in mystrlen. Delete this scanf. Secondly, your function always returns zero instead of value of variable abcd.

  • 3
    This is rather a comment than an answer, why don't you elaborate a bit more? – Jabberwocky Sep 07 '17 at 09:59
  • 2
    While this might be a valuable hint to solve the problem, a good answer also demonstrates the solution. Please [edit] to provide example code to show what you mean. Alternatively, consider writing this as a comment instead. – Toby Speight Sep 07 '17 at 11:40