Strings

Strings in C++ :

In C++, string class is provided to work with strings which are considered objects of string class rather than array of characters.

Syntax for declaring strings :
string str_name;
 

Assigning string to a string object :

string string_name=”string_value“;
string str1=str2;
string str1(“string_value“);
string str1(str2);
NOTE : 

  • We cannot assign a character array , a string object.
  • Strings created using string class, are not terminated with null character

Above code returns error as there is a type mismatch in assignment of values.

String class has made some operations simpler like concatenation, comparisions simpler.

Concatenation :

+ functon is overloaded to perform concatenation i.e. appending one string to another.
e.g.

append() : string& append (const string& str);

It can also be used to append strings.

Comparision :

== operator can be used to compare two strings. If two strings are equal, then 1 is returned else 0.
e.g.

compare function: 

It can also be used to compare two strings.

Returns a signed integral indicating the relation between the strings:
  •  0 ,if both are compared to be equal
  • <0 ,Either the value of the first character that does not match is lower in the compared string, or all compared characters match but the compared string is shorter.
  • >0 .Either the value of the first character that does not match is greater in the compared string, or all compared characters match but the compared string is longer.
e.g.

OUTPUT :

Length of strings :

size() or length() can be used to get the size of a string.
e.g.

OUTPUT :

5
5

Accessing elements :

  • To access an individual character at any position within string, we can use that index to access it. like str[5] for sixth position character.
  • at() can also be used to do same.
Other common functions of a string class are :

empty : 

  • to check if string is empty .
  • Returns a boolean value

e.g.

OUTPUT :

erase : 

string& erase (size_t pos = 0, size_t len = npos);
e.g.

OUTPUT :
hello!
hello

back : 

  • Returns a reference to the last character of the string.
  • This function shall not be called on empty strings.
e.g. 
OUTPUT :
Hello World!

front :

  • Returns a reference to the first character of the string.
  • This function shall not be called on empty strings.
e.g.
OUTPUT :
hello World

find() :  

int find(string pattern, int pos);
  • The position of the first character of the first match.
  • If no matches were found, the function returns string::npos.
When pos is specified, the search only includes characters at or after position pos, ignoring any possible occurrences that include characters before pos.

substr() :

  • Returns a newly constructed string object with its value initialized to a copy of a substring of this object.
  • The substring is the portion of the object that starts at character position pos and spans len characters (or until the end of the string, whichever comes first).
e.g.

OUTPUT :

copy :

size_t copy (char* s, size_t len, size_t pos = 0);

  • Copies a substring of the current value of the string object into the array pointed by s. This substring contains the len characters that start at position pos.
  • The function does not append a null character at the end of the copied content.
  • It returns the no. of characters copied into array.
e.g. 
OUTPUT: 

 

 
 

Leave a Reply

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