• Have Any Question?
  • contact@eitworld.com

cpp strings





C++ Strings

Strings in C++ is the sequence of characters is known as String used for manipulating any text such as words normally strings are used.Every C++ Strings is terminated with ” (NULL) character but it is optional. Each character of string occupies one byte in memory.The characters of array are stored in contiguous memory locations. Here is an example which shows the memory map of a string:

c++ string

At eitworld.com you can learn C++ Strings in simple and easy way with lots of examples.

C++ provides two types of string representations:

  • The C-style character string.
  • The string class type introduced with Standard C++.

C -Style Character string

    • The C-style character string originated within the C language and continues to be supported within C++.
    • As C in C++ also Array of characte’rs is known as String. To manipulate any text such as words normally strings are used.
    • Every string is terminated with ” (NULL) character.
    • Here is an example of string:
char name[] = {‘M’,’A’,’N’,’O’,’J’,”};
    • Each character of string occupies one byte in memory.
    • It is not compulsory to write ” in string, compiler itself put the ” character at the end of the string.
    • The characters of array are stored in contiguous memory locations. Here is an example which shows the memory map of a string:

strings

    • A String can also be initialized as follows:
Char name[]=”MANOJ”;
  • Here is an simple example of string:

Example


#include<iostream.h>
#include<conio.h>

int main()

{

char name[7]=”MANOJ”;

cout<<“nYour name is:”<<name;

getch();

return 0;

}

The output of the above program is as follows:

Output

Your name is: MANOJ

C -String Standard Functions

    • C++ language also supports several standard functions for manipulating the string like C language.
    • Following table contains the various String function with their description:

c_strings

Following example shows the use of some of the string functions listed above in the table:

Example


#include<iostream.h>
#include<string.h>

#include<conio.h>

int main()

{

char s1[10],s2[10],s3[10];

cout<<“nEnter string1:”;

cin>>s1;

cout<<“nEnter string2:”;

cout>>s2;

cout<<“nlength of first string:”<<strlen(s1);

cout<<“nlength of second string:”<<strlen(s2);

cout<<“ncopy first string into third string:”;

strcpy(s3,s1);

cout<<s3;

cout<<“nconcat first and second string:”;

strcat(s1,s2);

cout<<s1;

cout<<“nConvert first string in lower case letters:”;

strlwr(s1);

cout<<s1;

cout<<“nConvert second string in upper case letters:”;

strupr(s2);

cout<<s2;

getch();

return 0;

}

 

The output of the above program is as follows:

Output

Enter string1: RaDhA

Enter string2:ShArmA

length of first string:5

length of second string:6

copy first string into third string:RaDhA

concat first and second string:RaDhAShArmA

Convert first string in lower case letters:radhasharma

Convert second string in upper case letters:SHARMA

String Classes

  • The standard C++ library provides a string class type that supports all the operations mentioned above, additionally much more functionality. We will study this class in C++ Standard Library but for now let us check following example:
  • At this point, you may not understand this example because so far we have not discussed Classes and Objects. So can have a look and proceed until you have understanding on Object Oriented Concepts.

Example


#include <iostream>
#include <string>

using namespace std;

int main ()

{

string str1 = “Hello”;

string str2 = “World”;

string str3;

int len ;

// copy str1 into str3

str3 = str1;

cout << “str3 : ” << str3 << endl;

// concatenates str1 and str2

str3 = str1 + str2;

cout << “str1 + str2 : ” << str3 << endl;

// total lenghth of str3 after concatenation

len = str3.size();

cout << “str3.size() : ” << len << endl;

return 0;

}

 

When the above code is compiled and executed, it produces result something as follows:

Output

str3 : Hello

str1 + str2 : HelloWorld

str3.size() : 10



Real Time Web Analytics