Length function

Hey guys, So Im in an intro c++ class. And in one of the assignments i was asked to subtract the first and last letter of my.

I did it using this function.
cout << name.substr(1, 12) << endl;

But he said I should have used the .length function, so it could be used with any name and not just mine. Ive been messing around with it and havent been able to figure out how to do it. Any help would be appreciated.
Note that substr is short for substring. It doesn't do subtraction.

In C++ letters (characters) are integers so to substract them you use the minus operator (-), the same way you do with numbers.
Last edited on
I understand that i just shortened the string. But im not understanding how to display the name variable without the first and last character using the length function. Ive done name.lenght()-2 and that leaves me with the amount of characters i expect. But im having trouble getting it to display the name without the desired characters.
This is how I would have done it, and how I think he wants you to do it.

1
2
3
4
string name;
cin >> name;

cout << name.substr(1, name.length() - 2)  << endl;


a string works like an array. If I enter Tarik as my name. What will be printed out is ari, this is why.

the substring function takes 2 parameters. Where to start, and for how many characters to go. Remember it's like an array, so by starting at 1 and not 0, we jump over the first letter, so that's gone now. Now we want the last letter gone.

If I type in the name Tarik, then name.lenght() will be equal to 5, because Tarik is 5 characters.
So when the second parameter is name.lenght() - 2 will be equal to 3. Meaning, we want it to start at 1 and go on for 3 characters.

Tarik becomes ari. Starts at the second letter 1, goes on for 3 letters a,r,i.

This way, using the lenght function it works for all words, try it out for yourself. The way you did it

cout << name.substr(1, 12) << endl;

Doesn't work for all words, only for those that are 13 characters exactly, no more no less.
Thank you!!! But this brings me to another question.
name.substr(1, name.length() - 2) << endl; Having the 1 in the substr makes sense to me, but then why length() -2. Why wouldn't length()-1 work? Ive tried it and know it dosent. But Im not sure I understand why.
Because of the way string works, remember how I told you it's like an array?

Substr function takes 2 parameters, one of them is when to start, in this case 1, and the second parameter how long to go for.

If again, we use Tarik as the name, so name = Tarik; And we want to print out all the letters minus the first and last one.

Since a string can be used as an array, if we want to access the letter T, we would do name[0];

That's why we start at 1, because name[1] = a. So now we got it printing out a. Now, for how many characters do we want this to go on for? This is where the name.lenght() - 2 comes in

Tarik is 5 character. Meaning name.lenght() = 5;

If we would have name.lenght() - 1 as you suggested, it would mean it would be equal to 4 right? 5-1 is 4. So we're telling it to go on for 4 letters.

Tarik. starting at 1, a and going 4 letters, that gives us arik. the k is still there.

The reason it's minus 2 and not minus 1 is that, since the string is like an array, so it starts at 0. Tarik is 5 characters, so it's an array from 0-4. Name.lenght() = 5. The last character is in name[4]. So we need - 2 to get to name[3].

I hope I made it more clear :)
Last edited on
Thank You!!!! Very helpful!
Topic archived. No new replies allowed.