Can you explain this to me, please. I'm really confused!

I have been following various tutorials around the internet, getting a basic grasp of c++ and how it works.

I have written this code, which is me practicing the use of strcat and strlen.

When I first wrote the code, I wanted it to tell you the length of your first name, your last name and then your full name. However, because I used strcat every time I used strlen on "fullname" it would give me the number of letters plus an extra letter due to the space (" ") between the names being counted as another character.

So my original idea was to use ( strlen (fullname) - 1 ).

For some reason though, this was counted as an additional character, rather than taking it off.

So rather than being 12 letters (including the space) minus one making it 11. The total would come out as 13.

But when I use ( strlen (fullname) + 1 ) it then takes 1 off, making the strlen total 11.

Can anyone explain this to me? I'm utterly confused and sorry for the bad explanation, I hope you can understand what I'm trying to say.

Thank you!

TLDR; When I use (strlen (fullname) - 1) it adds a number to the total and when I use (strlen (fullname) + 1) it takes a character off the total. Why is this?

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
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <cstring>

using namespace std;

int main()

{
    char name[50];
    char lastname[50];
    char fullname[100];

    cout<<"What is your first name?\n";
        cin.getline (name,50);

    cout<<""<< name <<" is "<< strlen (name) <<" letters long.\n";

    cout<<"What is your last name?\n";
        cin.getline (lastname, 50);

    if ( strcmp ( lastname, "Boop" ) == 0 )
        cout<<"We have the same last name!\n";
    else
        cout<<"We don't have the same last name.\n";

    fullname[0] = '\0';
    strcat (fullname, name);
    strcat (fullname, " ");
    strcat (fullname, lastname);

    cout<<"So your full name is "<< fullname <<"!\n";
    cout<<""<< name <<" is "<< strlen (name) <<" letters long, "<< lastname <<" is "<< strlen (lastname) <<" letters long.\n";
    cout<<"That means that your full name has "<< strlen (fullname + 1) <<" letters in it!\n";
    cin.get();
}
Last edited on
It's because you put the +1 inside the parentheses. strlen (fullname + 1)
This tells strlen to start counting from the second character in the string.
You have put the +1 at the wrong spot.

To demonstrate what happens assume fullname stores the name "David Beckham". When you use the + operator on the array the array will be converted into a char pointer (char*) pointing to the first letter in the array. +1 will move the pointer one position so what you get is a char pointer that points to the 'a' instead of the 'D'. This will be treated as a string containing "avid Beckham" and that has one less character so that is why strlen returns a smaller value.

Subtracting (-1) works similar, but the pointer will move to the position before 'D'. We don't know what's being stored at that position in memory, and it's not really safe reading outside the array like that, but whatever character it is it will be counted as an extra character in the string by strlen (except if the character is a null character '\0', then it would be treated as an empty string).
Thank you Yay295 and Peter87! That makes perfect sense.

What would you suggest is the best way for me to ignore the extra character (the space)?

It works outside the parentheses like this:

"<< strlen (fullname) - 1 <<"

Is this the most efficient way to go about it?
Last edited on
If you know that you will always have one space, then yes. If you don't know how many spaces you'll have you'd have to count them first.
For this simple exercise there should only be one space.

Are you able to explain to me how I would go about calculating the spaces before outputting the result of strlen? (if in fact there were multiple spaces)

Or is this a topic better left for when I'm a little bit more advanced in my c++ knowledge.

Thank you again, I appreciate the help.
Last edited on
You would just need to have a loop iterate through each character in the string and increment a counter every time it found a space.
Topic archived. No new replies allowed.