Strings in C+++
Strings in C+++
C++ strings are sequences of characters stored in a char array. Strings are used to store words
and text. They are also used to store data, such as numbers and other types of information
Ways to Define a String in C++
Strings can be defined in several ways in C++. Strings can be accessed from the standard library
using the string class. Character arrays can also be used to define strings. String provides a rich
set of features, such as searching and manipulating, which are commonly used methods. Despite
being less advanced than the string class, this method is still widely used, as it is more efficient
and easier to use. Ways to define a string in C++ are:
1. C Style Strings
These strings are stored as the plain old array of characters terminated by a null character ‘\0’.
They are the type of strings that C++ inherited from C language.
Syntax:
char str[] = "GeeksforGeeks";
Example:
Output
GeeksforGeeks
2. std::string Class
These are the new types of strings that are introduced in C++ as std::string class defined inside
<string> header file. This provides many advantages over conventional C-style strings such as
dynamic size, member functions, etc.
Syntax:
std::string str("GeeksforGeeks");
Example:
// C++ program to create std::string objects
#include <iostream>
using namespace std;
int main()
{
string str("GeeksforGeeks");
cout << str;
return 0;
}
Output
GeeksforGeeks
One more way we can make strings that have the same character repeating again and again.
Syntax:
std::string str(number,character);
Example:
#include <iostream>
using namespace std;
int main()
{
string str(5, 'g');
cout << str;
return 0;
}
Output:
ggggg
Output
s1 = gfg
s2 = gfg
s3 = gfg
s4 = gfg
#include <iostream>
using namespace std;
int main()
{
string S = "Geeeks for Geeks";
cout << "Your string is= ";
cout << S << endl;
return 0;
}
Output
Your string is= Geeeks for Geeks
1. Using Cin
The simplest way to take string input is to use the cin command along with the stream extraction
operator (>>).
Syntax:
cin>>s;
Example:
string s;
cout<<"Enter String"<<endl;
cin>>s;
Output
Enter String
String is:
Output:
Enter String
GeeksforGeeks
String is: GeeksforGeeks
2. Using getline
The getline() function in C++ is used to read a string from an input stream. It is declared in the
<string> header file.
Syntax:
getline(cin,s);
Example:
string s;
cout << "Enter String" << endl;
getline(cin, s);
cout << "String is: " << s << endl;
return 0;
}
Output
Enter String
String is:
Output:
Enter String
GeeksforGeeks
String is: GeeksforGeeks
Different ways to access characters in a given String in C++
String class stores the characters as a sequence of bytes with the functionality of allowing access
to the single-byte character. There are several ways to access substrings and individual characters
of a string. The string class supports the following functions for this purpose:
1. operator[]
2. at()
3. substr()
4. find()
5. find_first_of()
operator[]
operator[] returns the reference to the character at the position specified as the argument.
Syntax-
char& operator[](size_t pos);
Here,
pos is the index of the character to be searched.
Below is the C++ program to implement the operator[] function-
Output
s
at()
at() function is used for accessing individual characters. With this function, character by
character can be accessed from the given string.
Syntax-
char& string::at (size_type idx)
Below is the C++ to implement at()
// Driver code
int main()
{
string s("GeeksForGeeks");
cout << s.at(4);
return 0;
}
Output
s
substr()
substr() function is used for retrieving a substring from a given string. This function takes two
values start and len.
string.h is the header file required for string functions.
The starting index is 0.
Syntax-
string substr (size_type start, size_type len);
Here,
start: Position of first character to be cancelled
len: Length of the sub-string.
Below is the C++ program to implement the substr() function-
Output
eeksF
find()
find() function is used to find the first occurrence of a substring in the specified string being
called upon.
It returns the index of the first occurrence of the substring in the string.
The default value of the starting position is 0.
If the substring is not found then the function returns -1.
Below is the C++ program to implement the find() function-
return 0;
}
Output
5
find_first_of()
find_first_of() function is used to find the first character that matches any of the characters
specified in the arguments. This function takes two arguments str and pos.
Syntax-
size_t find_first_of (const string& str, size_t pos = 0) const;
Here,
str is the string with characters to search for.
pos is the position of the first character in the string to be considered for search.
Below is the C++ program to implement find_first_of() function-
Output
4