C++ String Length - Numbers - the Code master

We will each you C++ String Length – Numbers manipulation, I am telling you that the + operators have two function addition and concatenation.

I am explaining both examples of addition and concatenation with numbers and strings.

So, see first example with numbers:

#include <iostream>
#include <string>
using namespace std;

int main(){
    int x = 10;
    int y = 15;
    int z = x + y;
    cout << z;
    return 0;
}
Output:
25
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"

So, Now I am explaining addition, concatenation with numbers, and string number.

#include <iostream>
#include <string>
using namespace std;

int main(){
    string x = "10";
    string y = "15";
    string z = x + y;
    cout << z;
    return 0;
}
Output:
1015

Remember: So, You can not add or concatenate one string number and integer

variable like this string z = “10” + 15; and its a wrong practice.

C++ String Length – Numbers

So, gays! here I am telling you how to find C++ String Length with functions. there are two function in c++ to find length of stings.

To get the length of a string and a text. we use the length() function, so the example below:

So, Example:

#include <iostream>
#include <string>
using namespace std;

int main(){
    string myString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    cout << "The length of a text string is: " << myString.length();
    return 0;
}

Then, the Output is:

The length of a text string is: 26

So, I am teaching you that you can use length() as well as size() function. because it has the same performance. it is just an alias of length(). It depends on your choice that you want to use length() or size in your programs.

Then, i perform a Example here:

#include <iostream>
#include <string>
using namespace std;

int main(){
    string myString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    cout << "The length of a text string is: " << myString.size();
    return 0;
}

I am gone a tell you that the header file <string.h> is an old face of header string file <string>. <string> is an updated header file. but today both perform equally object function.

So, I uploaded many more chapter on this web platform, and my recent lecture list is:

In this site, if you want to access to this complete Course for free please click on C++ Home. and for more updates Join Now! on Social media: FB Page: the Code Master and Instagram: TCMaster.

By Usama Arshad

It's me Usama Arshad working as a Lecturer C++. I am a student of BSIT.

Leave a Reply

Your email address will not be published. Required fields are marked *