1

My professor is having us make our own version of the functions mystrcat, mystrlen and mystrcopy. I have a problem where the word is returning the wrong amount of bytes. Or, rather, I think it is.

The phrase: "HELLO WORLD!" , if I am not wrong, should return 13 bytes? Like in:

const char char_literal[] =
   { 'H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D', '!', '\0' };
  //  1    2    3    4    5    6    7    8    9    10   11   12   13 

I wrote the function myStrLen() that returns ({ 'H', 'E', 'L', 'L', 'O', ' '}) as 6, but because it is alone, there should be a '\0' at the end right?
So should I be returning i + 1 or is 6 correct?

int myStrLen(char stringInput[]){
    for(int i = 0; ; i++){
        if(stringInput[i] == '\0'){
            return i;
Vincent
  • 99
  • 5
  • _"So should I be returning i + 1 or is 6 correct?"_ Totally depends on how to use `i` or `i + 1` further. –  Feb 14 '18 at 19:07
  • What is your expectation for `myStrLen("")` ? 0 or 1 ? – Jarod42 Feb 14 '18 at 19:07
  • 5
    `strlen()` doesn't count the null terminator. – Barmar Feb 14 '18 at 19:07
  • 2
    Tell your professor he should be teaching `std::string`. – Ron Feb 14 '18 at 19:08
  • @Ron I'd support that! –  Feb 14 '18 at 19:10
  • 2
    No need to ask us. You could simply compare your results against the value returned by the actual `strlen()`. Or, even better, [read the manual for `strlen`](http://en.cppreference.com/w/c/string/byte/strlen). – 001 Feb 14 '18 at 19:11
  • 2
    Disagree. The instructor should be honest about teaching C. – user4581301 Feb 14 '18 at 19:11
  • @user4581301 Well, if they claim to teach c++ (and most of them seem to do) :-P ... –  Feb 14 '18 at 19:12
  • 2
    @TheDude They're teaching computer science *using* C++. Are you claiming that you can't use a high-level language when teaching low-level concepts? – Barmar Feb 14 '18 at 19:13
  • @TheDude I am just passing a word through the function and having it return it's size as strlen would if you used it as a function. – Vincent Feb 14 '18 at 19:13
  • @user4581301 Haha, nice one. So true. – Ron Feb 14 '18 at 19:13
  • @Jarod42 I would expect 0 since it is empty, but I could potentially see 1 as it still holds null? – Vincent Feb 14 '18 at 19:14
  • 1
    @Barmar _"Are you claiming that you can't use a high-level language when teaching low-level concepts?"_ No, but _"low level concepts"_ belong to the _advanced classes' stuff_, not the _beginner class_ ones. –  Feb 14 '18 at 19:14
  • @Barmar AHH, I understand. So when I declared my word size as a const int, it accounts for the null; however, thats just for the size and not for strlen – Vincent Feb 14 '18 at 19:17
  • 1
    Are you learning about C++ or "C with classes"? – Justin Randall Feb 14 '18 at 19:17
  • The official answer, according to the syllabus is C++. I am in my second semester, so the class above intro – Vincent Feb 14 '18 at 19:18
  • 3
    Supplement your formal education with these [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). C++ is a world of joy. It's just the academia didn't get the memo yet. – Ron Feb 14 '18 at 19:20
  • 2
    I think these C++ courses taught in this manner are designed for students to turn to Java or Python instead of C++. – PaulMcKenzie Feb 14 '18 at 19:21
  • @Vincent I'm not sure what you mean by word size. If you're talking about the `sizeof` operator, it doesn't tell you the string length, it tells you how much memory is used for the whole variable or array. That includes the null terminaor, and padding bytes in other data types. – Barmar Feb 14 '18 at 19:25
  • @Vincent Well, if you insist using raw arrays/c-style character literals as it's a requirement not to fail that class: What are you considering a _"word"_? Should that word include the whitespace character or not. Should the determined string length be `0` if there are only whitespaces? Should the `'\0'` counted? How will you use the result of your function? –  Feb 14 '18 at 19:25
  • 2
    Sounds to me like a decent time to teach C++'s C-like underbelly. You need to know this stuff in order to survive the coming Algorithms and Data Structures course. Assuming this isn't the Algorithms and Data Structures course. If it is, draw the linked list before you start coding. Seriously. Pencil and paper. Save you tonnes of debugging. – user4581301 Feb 14 '18 at 19:25
  • You should make the signature of your version the same as `strlen` (at least that would be IMO a good start). i.e. `size_t myStrlen(const char* str);` – Justin Randall Feb 14 '18 at 19:28
  • 1
    @user4581301 I bet a cup of coffee that one of the next requirements will be "you cannot use std::string". – PaulMcKenzie Feb 14 '18 at 19:28
  • So what I did was just run it against the function I am trying to recreate with my own code as the assignment states and it seems to match. If you guys are confused on my word, im sorry. Thanks for the help! Here is the source code I was referring to. https://pastebin.com/sTBzmFjY – Vincent Feb 14 '18 at 19:29
  • @PaulMcKenzie I'm not going to take that bet, but if the goal of the assignment is to teach how to make a `std::string` the restriction makes sense. Otherwise... Meh. But I try to save my disrespect for "Welcome to CS101. First class, so let's do POINTERS!" I'm a teach logic first kinda guy. – user4581301 Feb 14 '18 at 19:34
  • @Vincent I got confused first when you mentioned words. I'd recommend not to call strings of characters in a programming context "word", since "word" refers to the natural unit of data in a particular processor architecture. The word size is what is refered to by 32bit or 64bit architecture. On a 32-bit system a word is 4 bytes. It has nothing to do with strings. Just say "string". – Max Vollmer Feb 14 '18 at 19:35

1 Answers1

4

If your professor tells you to write a function that behaves exactly as strlen, then it shall not count the string termination character:

std::size_t strlen( const char* str );

Returns the length of the given byte string, that is, the number of characters in a character array whose first element is pointed to by str up to and not including the first null character. ...

So returning i is correct, besides the fact that the "original" strlen takes a const input parameter and returns a size_t...

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58