Understanding Strings In c++

Advertisement

String is a datatype which is mostly used in programming or in your codes. String is basically a series of characters. The “CHAR” datatype only contains one character per variable. Strings are easy to manipulate. In c++ the string datatype cannot be used by default, to use string and its functions in your codes we have to include a header file “string” in our code or program.

Declaration & Initialization:

To initialize a string variable following syntax is used.

in this line str is name of variable.

Getting Size of String:

we can get the size of string by using the function size() defined in string library.

 

Converting String to Char:

While processing strings we often needed to convert it into char array. We can do it as follows.

in this .c_str function is used to convert it into stream of character.

Following is the fully compiled code.

OUTPUT

c string

Comments

  1. By emy

    Reply

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>