a String Length Function without using <cstring>

Pages: 12
Sep 20, 2012 at 6:33am
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. :(
Sep 20, 2012 at 6:39am
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 Sep 20, 2012 at 6:53am
Sep 20, 2012 at 7:50am
@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;
Sep 20, 2012 at 8:38am
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 Sep 20, 2012 at 8:52am
Sep 20, 2012 at 8:53am
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 Sep 20, 2012 at 1:36pm
Sep 20, 2012 at 11:02am
@vlad;
 
int myStrLen( const char[] )


you mean this?

 
int myStrLen( const char[] str )
Sep 20, 2012 at 1:35pm

@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 Sep 20, 2012 at 2:46pm
Sep 20, 2012 at 1:52pm
yeah, i need to learn more dude... :D i'm totally newbie... very basic...
Sep 20, 2012 at 1:54pm
*derp* I don't know why I didn't just return i.
Sep 20, 2012 at 2:29pm
*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 Sep 20, 2012 at 2:31pm
Sep 20, 2012 at 2:41pm
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?
Sep 20, 2012 at 2:59pm
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...
Sep 20, 2012 at 3:20pm
But there is no way that the value of i would have changed! :F
Sep 20, 2012 at 3:42pm
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)...
Sep 21, 2012 at 3:52am
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? :/
Sep 21, 2012 at 4:23am
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.
Sep 21, 2012 at 4:28am
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 Sep 21, 2012 at 4:30am
Sep 21, 2012 at 4:36am
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.
Sep 21, 2012 at 4:39am
There should only be one null-terminated character in a C-string, so just add one to the value being returned.
Last edited on Sep 21, 2012 at 4:40am
Sep 21, 2012 at 4:41am
Ok! That's so cool!
I have been working on this for 2 days. Thank you so much!
Pages: 12