0% found this document useful (0 votes)
0 views10 pages

Strings in C+++

The document provides an overview of strings in C++, detailing how they can be defined using C-style strings or the std::string class. It explains various methods for inputting strings, including using 'cin' and 'getline', as well as ways to access and manipulate string characters through functions like operator[], at(), substr(), find(), and find_first_of(). Examples of code snippets demonstrate the usage of these string functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views10 pages

Strings in C+++

The document provides an overview of strings in C++, detailing how they can be defined using C-style strings or the std::string class. It explains various methods for inputting strings, including using 'cin' and 'getline', as well as ways to access and manipulate string characters through functions like operator[], at(), substr(), find(), and find_first_of(). Examples of code snippets demonstrate the usage of these string functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

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:

 Using String keyword


 Using C-style strings

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:

// C++ Program to demonstrate strings


#include <iostream>
using namespace std;
int main()
{
char s[] = "GeeksforGeeks";
cout << s << endl;
return 0;
}

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

// C++ Program to demonstrate C-style string declaration


#include <iostream>
using namespace std;
int main()
{
char s1[] = { 'g', 'f', 'g', '\0' };
char s2[4] = { 'g', 'f', 'g', '\0' };
char s3[4] = "gfg";
char s4[] = "gfg";

cout << "s1 = " << s1 << endl;


cout << "s2 = " << s2 << endl;
cout << "s3 = " << s3 << endl;
cout << "s4 = " << s4 << endl;
return 0;
}

Output
s1 = gfg

s2 = gfg

s3 = gfg

s4 = gfg

Another example of C-style string:

#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

How to Take String Input in C++


String input means accepting a string from a user. In C++. We have different types of taking
input from the user which depend on the string. The most common way is to take input
with cin keyword with the extraction operator (>>) in C++. Methods to take a string as input are:
 cin
 getline

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:

// C++ Program to demonstrate string input using cin


#include <iostream>
using namespace std;
int main() {

string s;

cout<<"Enter String"<<endl;
cin>>s;

cout<<"String is: "<<s<<endl;


return 0;
}

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:

// C++ Program to demonstrate use of getline function


#include <iostream>
using namespace std;
int main()
{

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-

// C++ program to implement


// the operator[]
#include <iostream>
using namespace std;
// Driver code
int main()
{
string str("GeeksforGeeks");
cout << str[4];
return 0;
}

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()

// C++ program to implement


// at()
#include <iostream>
using namespace std;

// 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-

// C++ program to implement


// the substr() function
#include <iostream>
using namespace std;
// Driver code
int main()
{
string s("GeeksForGeeks");
cout << s.substr(1, 5);
return 0;
}

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-

// C++ program to implement


// the find() function
#include <iostream>
using namespace std;
// Driver code
int main()
{
string s("GeeksForGeeks");
cout << s.find("For");

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-

// C++ program to implement


// the find_first_of() function
#include <iostream>
using namespace std;
// Driver code
int main()
{
string s("GeeksForGeeks");
cout << s.find_first_of('s');
return 0;
}

Output
4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy