strlen function

Hello, I have this work assignment that I am suppose to finish, basically what I need to do is create a program that outputs the number of characters you input in to the program.

I would appreciate some advice or hints, I keep getting an error "cannot convert char to char**".

Any hints or advice would be greatly appreciated.

Thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

// TODO: function declaratin comes here
void strlen2(char* s[], int& i);
int main()
{
    char s[1000];
    while (cin.getline(s, 1000))
    {
        cout << "strlen2(\"" << s << "\") = " << strlen2(s) << endl;
    }

    return 0;
}
void strlen2(char* s[], int& i)
{
    while(s[i]!=0)
    {
        cout << s[i++];
    }
}
// TODO: function implementation comes here 
Your strlen2() is expecting a pointer to char array (or an array of c-strings) but s is a simple char array (one big c-string), plus it requires a second argument that's really not necessary.
A better prototype would be int strlen2(char* s). It shouldn't print the content of the strings, only count its characters until it reaches the null terminator and return that.
Last edited on
Thank you, that helped a lot. Though I seem to have some minor issue in my code yet again, now it's not outputting the correct numbers, e.g hello = 104 (but it should be 5, obviously).

any more hints?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

// TODO: function declaratin comes here
int strlen2(char* s);
int main()
{
    char s[1000];
    while (cin.getline(s, 1000))
    {
        cout << "strlen2(\"" << s << "\") = " << strlen2(s) << endl;
    }

    return 0;
}
int strlen2(char* s)
{
    int i = 0;
    while(s[i] != '\0')
    {
        return s[i];
        i++;
    }
    return 0;
}
// TODO: function implementation comes here 
On line 21 you are returning from the function as soon as you enter the loop (which has no chance of incrementing i and restarting). This means that, unless s[0] == '\0', the first thing that happens is that you return the first character of the string (s[i] with i = 0) casted to int.
The first character of "hello" is 'h', which is 0x68 or 104 in ASCII (http://www.cplusplus.com/doc/ascii/).

The reason it prints 104 instead of 'h' is because you returned an int. ostream::operator<<(int) will print a number instead of the character it represents. In the case where you returned a char, ostream::operator<<(char) is called which will print the character corresponding to the number.

EDIT
I noticed this part may be misunderstood
count its characters until it reaches the null terminator and return that.
I mean return the count, not the character.
Last edited on
Again, thank you maeriden for your input and patience, now, I think I am misunderstanding something here, it's getting late and I've been doing this for a while, but I've tried several different variations, and all I can come up with is output = 1, so I think it is basically only counting my first letter.

1
2
3
4
5
6
7
8
9
10
int strlen2(char* s)
{
    int i = 0;
    while(s[i] != '\0')
    {
        s[++i];
        return i;
    }
    return 0;
}

If I understood you correctly, but I got 1 after every input, or 0 if I left it blank.



That's because of that return inside the loop. It causes the function to exit immediately so the loop doesn't have a chance to repeat more than one time.
For any non-empty string, it will check if the first character is the terminator, then increment i (so now i = 1) and index into the string (but do nothing with the character), then return i (which is always 1).
You want to return only after you found the terminator, so you only need one return after the loop. The loop itself should only check if the character currently examined is the terminator, and, if it's not, increment the index to examine the following character during the next iteration.
Incidentally, the index is also the count of the characters excluding the terminator, so returning i is sufficient to make the function work. In the case where a string is empty ("\0", meaning it only contains the terminator) the condition of the loop is immediately false, i doesn't get incremented, and return i will return 0.

Again, thank you maeriden for your input and patience
No problem, that's why I hang around here. But if you are tired I don't recommend you work on this. It's a lot harder to make sense of things.
Last edited on
Hmm, I did try cout << i; instead, and it does what I want to some degree, but doesn't put it out in the desirable location, neither does it give me one number, but counts all of the characters up to the last one.

I don't think you should put cout << i; (except perhaps during debugging). Instead, after the strlen2() function exits from the while loop, put return i;
I feel so stupid, and I swear to god I think I did that already or at the very least something similar. But, nevertheless, thank you.

And maeriden for all of your explanations. Thanks guys.

Now on to the next assignment.
Topic archived. No new replies allowed.