C++ String Class and its Properties

Vranda Agarwal
2 min readJan 6, 2021

--

Strings are objects that represent sequences of characters. This class is called std:: string. String class stores the characters as a sequence of bytes with a functionality of allowing access to single byte character. The string is terminated by a null character ‘\0’.

In order to perform string manipulations, it is mandatory to use the header file supported by the C/C++ standard library, namely : #include<string.h>

In case of strings, memory is allocated dynamically. More memory can be allocated at run time on demand. As no memory is pre allocated, so no memory is wasted.

Strings are immutable in nature i.e. un-modifiable. Once a string is initialized, it cannot be redefined later if needed.

Applications of string are :

o Strings are used for creating password and username.

o Whenever you pull any text out of database, you need a string.

Some of the basic operators are :

· Assignment operator(=) : string str = “Hello”. This operator assign Hello to str.

· Concatenate operator(+) : This function concatenate or adds up the two strings.

string s1=”Hello”; string s2=”World”;

string s3 = s1+s2;

Value of s3 if “Hello World”.

· Index operator([]) : Return character at first position of string. i.e. str[0].

There are some functions which can be operated on string :

· strlen(s1) : This function finds the length of the string s1.

· strcpy(s1,s2) : Copy string s2 into string s1.

· strcat(s1,s2) : Concatenate string s2 onto the end of string s1.

· strcmp(s1,s2) : Return 0 if s1 and s2 are same, less than 0 if s1<s2, greater than 0 if s1>s2.

· clear() : This function clear all the characters from the string and make the string empty.

· empty() : Checks whether string is empty or not. This function returns a Boolean value.

· toupper(s1) : This function convert lower caps to upper caps. It accepts a single parameter.

char s1=”h”;

toupper(s1);

Now, s1 = “H”;

· tolower(s1) : This function convert upper caps to lower caps. It accepts a single parameter:

char s1=”H”;

tolower(s1);

Now, s1 = “h”;

If you enjoyed this blog post, share it with your friends and do give it a clap.

--

--

No responses yet