a String Length Function without using <cstring>

Pages: 12
There is a very beginner assignment. It suppose to be a C++ program, not a C program.
C++ has one, but C has no "string" data type. So i want to use arrays of characters to achieve the same functionality. It should be a C function to compute string length, prototype: int myStrLen(const char[])
And also the program should search for the null character, '\0'.
No pointer and no <cstring>.

Can anyone help me out? I have been stuck for 2 hours. :(
Here is one I quickly wrote up in C++. http://codepad.org/fuExNzpO

You have the right idea; have a variable that increments by one every time another character is read. When the character is a null-terminated character, the loop exits and the function returns the length of the string.
Last edited on
@xhtmlx: is that a legal syntax? imo, for me, i'm just use:
1
2
for (int i = 0; str[i] != '\0'; ++i, ++length)
   continue;
Why the length variable ? Just return i.

here is the code from K&R:

1
2
3
4
5
6
7
8
/* strlen: return length of s */
int strlen(char s[])
{
int i;
while (s[i] != '\0')
++i;
return i;
}
Last edited on
I would prefer the following code

1
2
3
4
5
6
int myStrLen( const char str[] )
{
   int i = 0;
   while ( str[i] ) ++i;
   return ( i );
}
Last edited on
@vlad;
 
int myStrLen( const char[] )


you mean this?

 
int myStrLen( const char[] str )

@chipp

@vlad;
int myStrLen( const char[] )
you mean this?
int myStrLen( const char[] str )


Yes, it is a typo. I copied the declaration of the function from the original message and did not take into account that the identifier of the parameter is missed.:) Thanks, I updated my previous message.

But you also made a typo the same way as I did.:) Instead of

int myStrLen( const char[] str )

as you suggested shall be

int myStrLen( const char str[] )

:)
Last edited on
yeah, i need to learn more dude... :D i'm totally newbie... very basic...
*derp* I don't know why I didn't just return i.
*derp* I don't know why I didn't just return i.

wasn't that i is being out of scope? if you want to return i;, write this:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int strLength(const char str[]) {
    int i = 0;
    for(i = 0; str[i] != '\0'; ++i) ;
    return i;
}

int main(void) {
    const char *str = "derp";
    std::cout << strLength(str);
    return 0;
}
Last edited on
Yeah, I know i is out of scope. I could have moved it outside of the for loop. Any reason why you assign 0 to i and then assign it again to i?
yes, you noticed it... good... :) i did it on purpose... it said that initialization is good programming practice... so i did initialized it (assign value after declaration) and then assign in again, when it's about to used...
But there is no way that the value of i would have changed! :F
well, maybe the purpose is avoiding your variable having a garbage value... my opinion, it's useful on long source code... but, in my opinion once again, it is good to initialize it because we don't know how long our code is going to be...

maybe the others can explained it better (or correctly)...
Thank you for all your helps! I really appreciate!!

I wrote like this:

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

//prototype
int myStrLen(const char[]); //return length of null-terminated string stored in char array

int main()
{
const char *str = "Hello";
cout<<" "<< myStrLen(str);
}

int myStrLen(const char str[])
{
int i = 0;
for(i = 0; str[i] != '\0'; i++) ;
return i;
}


But one thing is it didn't calculate \0 actually, when i input "derp", it displayed 4, "Hello" output is 5. But it suppose to count null terminator, should i just plus 1 or what? :/
Hi Zoe,

The initial int i = 0; isn't necessary, as it is done in the for loop. Why do you want to count the null terminator in the string? The null is there so that the computer knows where the end of the string is.

I should say to please use code tags - the <> button on the right - it makes reading the code much easier as it formats it properly.

Hope this helps.
The initial int i = 0; isn't necessary, as it is done in the for loop. Why do you want to count the null terminator in the string? The null is there so that the computer knows where the end of the string is.

He wouldn't be able to return i if he removed that. The for loop ends before the null-terminated character is counted as a character of the string. His code is fine and functions properly, so there is little to nothing he could improve upon.
Last edited on
Sorry TheIdeasMan, its my first time use Forum, I am a very new beginner.


And thank you again! xhtmlx.
And is that a possible to count null terminator? I am not sure whether its a requirement or not yet, i am asking.
There should only be one null-terminated character in a C-string, so just add one to the value being returned.
Last edited on
Ok! That's so cool!
I have been working on this for 2 days. Thank you so much!
Pages: 12