Strings in C++
We will learn how to work with strings in C++ in this article.
A string stores a sequence of characters. In C++, there are two types of strings:
1. C-style strings
2. Objects of the string class in the Standard C++ Library
C-style Strings
C++ continues to support C-style strings. Arrays are used to store a group of characters in C programming. C-style strings are 1-D char arrays with a null character ‘\0’ at the end.
1. Defining a C-style string
Syntax to declare a string:
char string_name[size];
Let’s initialize string str to store the word TechVidvan. This can be done in the following ways:
- char str[] = “TechVidvan”;
- char str[20] = “TechVidvan”;
- char str[] = {‘T’, ‘e’, ‘c’, ‘h’, ‘V’, ‘i’, ‘d’, ‘v’, ‘a’, ‘n’, ‘\0’};
- char str[11] = {‘T’, ‘e’, ‘c’, ‘h’, ‘V’, ‘i’, ‘d’, ‘v’, ‘a’, ‘n’, ‘\0’};
Example to read and display C-style string
#include <iostream> using namespace std; int main() { char word[20]; char line[100]; //read a word cout<<"Enter a word "; cin>>word; //display the word cout<<endl<<word<<endl; //read a line cout<<"Enter a line "; cin.get(line, 100); //display the line cout<<endl<<line; return 0; }
Output
TechVidvan
Enter a line Articles on C++ programming
Articles on C++ programming
Here, to read a line we used cin.get() function. It is used to read character arrays including whitespaces.
Syntax:
cin.get(string_name, size);
Object of std::string C++ class
In C++, we can represent strings as objects of std::string class. These objects are used to hold strings. The string class holds characters as a stream of bytes where we can access a single byte character.
Unlike character arrays, string objects do not have a fixed length and can be increased as needed.
Remember that to use string, we must include the <string> library as a header file in our code.
1. Defining a string object
Syntax to declare a string object:
string string_name;
Let’s initialize a string object,
string str = "TechVidvan";
Example to illustrate the use of string
#include <iostream> #include <string> using namespace std; int main() { string s1; //taking input of a string from user getline(cin, s1); //displaying the string cout<<"Entered string is "<<s1; return 0; }
Input
TechVidvan
Output
In the above example, we have used the getline() function to store the string entered by the user.
Some other functions to operate on strings
1. push_back() : We use this function to add a character at the end of a string.
Syntax:
string_name.push_back(‘character’);
2. pop_back() : We use this function to delete the last character of a string.
Syntax:
string_name.pop_back();
3. length() and size() : We use these functions to get the length of a string.
Syntax:
string_name.length();
Or
string_name.size();
Memory occupied by strings in C++
We know that data type char occupies 1 byte of memory space. A string in C++ thus occupies N bytes where, N = no. of characters in a string including the null character.
For example,
char s[ ] = "TechVidvan";
This string s will occupy (10 + 1) = 11 bytes of memory space. We can represent this in tabular form as shown below
While designing, instead of DATAFLAIR\0, it will be TECHVIDVAN\0 and index from 0 – 10.
We can use sizeof() operator to get the size of a string.
C++ Functions for strings
C++ offers various functions to operate on strings.
1. strcpy(string1, string2); : We use this function to copy string2 into string1.
2. strcat(string1, string2); : We use this function to concatenate both the strings.
3. strlen(string1); : We use this function to compute the length of a string.
4. strcmp(string1, string2); : We use this function to compare both the strings. It returns 0 when both are equal, <0 when string1<string2 and >0 when string1>string2.
5. strchr(string1, ch); : We use this function to get the pointer pointing to the first appearance of character ch in a string.
6. strrchr(string1, ch); : We use this function to get the pointer pointing to the last appearance of character ch in a string.
7. strstr(string1, string2); : We use this function to get the pointer pointing to the first appearance of string2 in string1.
8. strlwr(string1); : We use this function to convert all the characters of a string to lowercase.
9. strupr(string1); : We use this function to convert all the characters of a string to uppercase.
10. strncat(string1, string2, n); : We use this function to append first n characters of string2 at the end of string1.
11. strncpy(string1, string2, n); : We use this function to copy first n characters of string2 into string1.
12.strncmp(string1, string2, n); : We use this function to compare n characters of both the strings. It returns 0 when both are equal, <0 when string1<string2 and >0 when string1>string2.
13. strcmpi(string1, string2); : We use this function to compare both the strings without considering case sensitivity. It returns 0 when both are equal, <0 when string1<string2 and >0 when string1>string2.
14. strnicmp(string1, string2, n); : We use this function to compare n characters of both the strings ignoring case sensitivity. It returns 0 when both are equal, <0 when string1<string2 and >0 when string1>string2.
15. strdup(string1); : We use this function to get a pointer to a duplicate string of string1.
16. strndup(string1, n); : This function works similar to strdup except, it duplicates n characters of string.
17. strset(string1, ch); : We use this function to set all characters of a string to character ch.
18. strnset(string1, ch, n); : We use this function to set first n characters of a string to character ch.
To use these functions we need to include <string.h> header file in our program.
Example to illustrate the use of some C++ string functions
#include <iostream> #include <string.h> using namespace std; int main() { char s1[ ] = "TechVidvan"; char s2[ ] = "DataFlair"; int length = strlen(s1); cout<<"Length of string TechVidvan = "<<length<<endl; char s3[15]; strcpy(s3, s1); cout<<"Copied string = "<<s3<<endl; strcat(s1,s2); cout<<"Concatenated strings = "<<s1<<endl; if(strcmp(s1,s2)==0) cout<<"Strings are equal"; else cout<<"Strings are unequal"; return 0; }
Output
Copied string = TechVidvan
Concatenated strings = TechVidvanDataFlair
Strings are unequal
Passing string to a function
Example of strings as function parameters
#include <iostream> #include <string> using namespace std; void print(char s[], string str) { cout<<"Entered C-style string is "<<s<<endl; cout<<"Entered string object is "<<str; } int main() { char s1[50] = "TechVidvan"; string s2 = "Data Flair"; print(s1, s2); return 0; }
Output
Entered string object is Data Flair
Summary
In this article, we have learnt about strings in C++. A string is a sequence of characters. C++ offers us two types of strings, one in the form of character arrays and other as objects of std::string class. We discussed how to work with both the types of strings.We also learnt to pass strings to a function using example programs.