C++ String

WHAT ARE STRINGS?

  • The string is an object of the string class and it is defined in the header file i.e. <string.h>.The string class has many constructors that are called to create a string object.
  • It represents the sequence of characters. The operations which can be performed on strings are comparison, concatenation, conversion, etc.
  • The one dimensional or single dimensional array of characters is known as Strings and is terminated by null character ‘\0’.

FOR EXAMPLE,

‘Hello’ is a string of 5 characters.

HOW TO DEFINE STRINGS IN C++?

As in C programming, the collection of characters is stored in the form of arrays and they are supported by C++ programming too. They are known as C-strings. The C-strings arrays are of char type and is terminated by null (\0) character. So, let us see how strings are defined in C++.

FOR EXAMPLE:

                     char str_arr[ ] = “Hello”;

In the above code you can see that, str_arr is a string and it contains 6 characters. In the C++ programming language, the array holds the null character value which is added to the end of the string automatically.

LET US HAVE A LOOK AT THE EXAMPLE:

1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
using namespace std;
int main( )
 
{ char str_arr[ ] = “Hello”;
 
cout<<str_arr;
 
return 0;
 
}
Output :
Hello

FUNCTIONS IN STRINGS

C++has a wide range of functions that manipulate the strings. Let us have a look on them:

STRCAT:

  •  It is a string concatenate function. It will concatenate or join the second string with the first string at the end.

LET US HAVE A LOOK AT THE EXAMPLE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include<cstring>
 
using namespace std;
 
int main( )
 
{ char s1[10] = “Hello”;
 
char s2[30] = “Good Morning”;
 
strcat(s1,s2);
 
cout<<“The new string after concatenation is:” <<s1<<endl;
 
return 0;
 
}
Output :
The new string after concatenation is:HelloGood Morning

STRCPY:

  • It is a string copy function. It will copy the first string into second string.

LET US HAVE A LOOK AT THE EXAMPLE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<cstring>
using namespace std;
int main( )
 
{ char s1[10] = “Hello”;
 
char s2[10];
 
strcpy(s2,s1); //copy the first string in second string
 
cout<<“The new string is:”<<s2<<endl;
 
return 0;
 
}
Output :
The new string is:Hello

STRCMP:

  • It is a string compare function. In this function it compares the two strings if the strings are equal it will return 0 or else if the strings are not equal it will return 1.

LET US HAVE A LOOK AT THE EXAMPLE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
#include<cstring>
using namespace std;
 
int main()
 
{
 
char s1[10] = “Hello”, s2[10] = “morning”;
 
cout<<“\nEnter first string : “<<s1;
 
cout<<“\nEnter second string : “<<s2;
 
if(strcmp(s1, s2)==0)
 
{ cout<<“\nThe strings are equal”;
 
}
 
else
 
{ cout<<“\nThe strings are not equal”;
 
}
 
return 0;
 
}
Output :
Enter first string : Hello
Enter second string : morning
The strings are not equal

STRLEN:

  •  It is a string length function. It will return the length of a string.

LET US HAVE A LOOK AT THE EXAMPLE:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
 
#include<cstring>
using namespace std;
int main( )
 
{ char s1[10] = “Hello”;
 
int length;
 
length= strlen(s1); //it provide the length of the string
 
cout<<“The length of string is:” <<length<<endl;
 
return 0;
 
}
Output :
 The length of string is:5

STRCHR:

  • In this it will take the two arguments string and character. It will searches for the character from the given string.

LET US HAVE A LOOK AT THE EXAMPLE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
 
#include <cstring>
 
using namespace std;
 
int main()
 
{
 
char s1[ ] = “Hello World”;
 
char ch = ‘l’;
 
if (strchr(s1, ch))
 
cout << ch << ” is present in the string \”” << s1 << “\””;
 
else
 
cout << ch << ” is not present in the string \”” << s1 << “\””;
 
return 0;
 
}
Output :
l is present in the string “Hello World”

STRSTR:

  •  It will take two arguments string and destination. It will search the first occurrence of string in the first string.

LET US HAVE A LOOK AT THE EXAMPLE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char s1[ ] = “Hello World”;
char destination[ ] = “World”;
char *p = strstr(s1, destination);
if (p)
cout << “‘” << destination << “‘ is present in the string \”” << s1 << “\” at position” << ps1;
else
cout << destination << “is not present in the string\”” << s1<< “\””;
return 0;
}
Output :
‘World’ is present in the string “Hello World” at position 6