Introduction
Suppose you need to process a set of characters in your program if you have no idea about strings. You would declare a variable for every character you want to process but with strings, this cumbersome process can be eased.
In a naive way, strings are a group of characters or a plain set of characters or simple text. These characters can vary from alphabets to numbers to symbols.
In terms of C++, strings are a group of characters i.e. an array of characters terminated by ‘\0’.
Initialization of character arrays:
We can initialize character arrays in the following ways:
char str[4]=”TUF”; char str[]={‘T’,’U’,’F’,’\0’}; char str[8]={‘s’,’t’,’r’,’i’,’v’,’e’,’r’,’\0’};
Let’s see a simple program to get a word from the user
Code:
C++ Code
#include<iostream>
using namespace std;
int main(){
char str[50];
cout<<"Enter a word ";
cin>>str;
cout<<"\n str holds: "<<str;
return 0;
}
Creating string object:
We can also define strings by creating a string class object. Using string class gives flexibility in terms of size, unlike character arrays where the length of arrays needs to be pre-defined.
The string class also becomes useful in string manipulation.
We can declare a string using string class as follows
string str; string s;
The following code is to read a string from the user.
Code:
C++ Code
#include<iostream>
#include<string>
using namespace std;
int main(){
string s;
cout<<"Enter a string: ";
cin>>s;
cout<<"\ns holds: "<<s;
return 0;
}
String Functions
String class comes with a rich library of string functions which can come in handy in string manipulation and other processes.
We’ll see some of the string functions supported in C++
- string::length(): returns the length of the string
string s="takeuforward"; cout<<s.length(); // prints 12
- string::find(): returns the starting index of the character
string s="takeuforward"; cout<<s.find('a'); // prints 1
- string::substr(): accepts starting index as parameter and returns substring starting from that index to end.
string s="takeuforward"; cout<<s.substr(3); // prints euforward
- string::at(): accepts index as a parameter and returns character present at that index.
string s="takeuforward"; cout<<s.at(2); // prints k
- string::append(): accepts another string object, index, length, number of characters to copy as a parameter and pushes the defined parameter to the end of the string.
string s; string str="takeuforward"; s.append(str); cout<<s; // prints takeuforward
- string:: getline(): accepts istream object and string object as parameter and extracts characters from istream object and stores them into a string object until a delimiter character is found.
string s; cout<<"Enter the string: "; getline(cin,s); cout<<"\ns holds: "<<s;
- string::compare(): accepts string object as a parameter and returns 0 or another integer if string matches or not respectively.
string s="takeuforward"; string str="striver"; s.compare(str) ? cout<<"doesn't match" : cout<<"matches";
- string::clear(): clears the string and makes it an empty string
string s="takeuforward"; s.clear(); cout<<s; // prints “”
- string::front(): can be used to access the first character of the string.
string s="takeuforward"; cout<<s.front(); // prints t
- string::back(): can be used to access the last character of the string.
string s="takeuforward"; cout<<s.back(); // prints d
String class vs character array
Now we try comparing string class and character array in C++.
Character Array | String |
It is a sequence of characters terminating with ‘\0’ | A class defines objects that can be represented by a sequence of characters. |
Memory allocation is static. | Memory allocation is dynamic. |
Not many inbuilt functions for string manipulation. | Offers a rich library of functions for string manipulation. |
It is faster in terms of implementation. | It is slower in terms of implementation. |
Special thanks to Yash Mishra for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article