STRING in C++
- WAYS TO DEFINE STRING IN C++
- HOW TO TAKE STRING INPUT IN C++
- C++ STRING FUNCTIONS
- POINTERS IN STRINGS
std::string Class These are new types of strings represented in C++ as a std::string class defined by header file. It offers many advantages over traditional C-style chords, such as dynamic size, limb functions, etc.
Syntax:
std::string str("Hello India");
for example,
#include <iostream>
using namespace std;
int main()
{
string str("Hello India");
cout << str;
return 0;
}
Output
Hello India
Ways to define a string in C++
Strings can be defined in C++ in several ways. Strings can be accessed from the standard library using the String class. Arrays of characters can also be used to define strings. String provides many functions such as search and manipulation which are commonly used methods. Although this method is less advanced than the String class, it is still widely used because it is more efficient and easier to use. The following are ways to define a string in C++:
Using the String Keyword
· Using
C-Style Strings.
1) Using string keyword
It is easier to define a
string with the string keyword instead of the array keyword because it is
easier to write and understand.
Syntax
string s = "Hello India";
string s("Hello India");
let us try understanding this by a C++ program
#include <iostream>
using namespace std;
int main()
{
string s = "Hello India";
string str("Hello India");
cout << "s = " << s << endl;
cout << "str = " << str << endl;
return 0;
}
Output
s = Hello India
str = Hello India
2) Using C-style string
Using C-style string library functions such as strcpy(), strcmp(), and strcat() to define
strings. This method is more complex and not as widely used as the other two, but it
can be useful when dealing with legacy code or when you need performance.
char s[] = {'s', 't', 'b', '\0'};
char s[4] = {'s', 't', 'b', '\0'};
char s[4] = "stb";
char s[] = "stb";
let us try a C++ program to understand C-style string
#include <iostream>
using namespace std;
int main()
{
char s1[] = { 'c', 'd', 'e', '\0' };
char s2[4] = { 'c', 'd', 'e', '\0' };
char s3[4] = "cde";
char s4[] = "cde";
cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2 << endl;
cout << "s3 = " << s3 << endl;
cout << "s4 = " << s4 << endl;
return 0;
}
Output
s1 = stb
s2 = stb
s3 = stb
s4 = stb
How to take string input in C++
Entering a string means getting a string from the user. in C++. We have different user inputs that depend on the string. The most common way is to type the cin keyword using the C++ extraction operator (>>). Ways to take a string as input:
· cin
· Getline
· stringstream.
1) Using cin
Syntax
cin>>s;
let us understand this by a C++ program
#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 : Hello India
String is : Hello India
2) Using get
line()
The C++ function getline()
is used to read a string from the input stream. It is declared
as <string> header file.
Syntax
getline(cin,s);
let us understand this by a C++ program
#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 the string: Hello India
String is: Hello India
3)
Using stringstream
Syntax
stringstream stringstream_object(string_name);
let us understand this by a C++ program
#include <iostream>
#include <stream>
#include<string>
using namespace std;
int main()
{
string s = " Hello Everyone ";
stringstream obj(s);
// string to store words individually
string temp;
// >> operator will read from the stringstream object
while (obj >> temp) {
cout << temp << endl;
}
return 0;
}
Output
Hello
Everyone
To pass a string to functions
Just as we pass an array to a function, C++ strings can be passed to
functions as character arrays. Here is a sample program.
Let us try by a C++ program
#include <iostream>
using namespace std;
void print_string(string s)
{
cout <<
"Passed String is: " << s << endl;
return;
}
int main()
{
string s =
"Hello India";
print_string(s);
return 0;
}
Output
The passed string is: Hello India
C++ String Functions
Function |
Description |
It is used to compare two string objects. |
|
It is used to find the length of the string. |
|
It is used to swap the values of two string objects. |
|
string substr(int pos,int n) |
It creates a new string object of n characters. |
It returns the length of the string in terms of bytes. |
|
It is used to resize the length of the string up to n characters. |
|
It replaces a portion of the string that begins at character position
pos and spans len characters. |
|
It adds new characters at the end of another string object. |
|
It is used to access an individual character at specified position
pos. |
|
It is used to find the string specified in the parameter. |
|
It is used to find the first occurrence of the specified sequence. |
|
It is used to search the string for the first character that does not
match with any of the characters specified in the string. |
|
It is used to search the string for the last character of specified
sequence. |
|
It searches for the last character that does not match with the
specified sequence. |
|
It inserts a new character before the character indicated by the
position pos. |
|
It finds the maximum length of the string. |
|
It adds a new character ch at the end of the string. |
|
It removes a last character of the string. |
|
It assigns new value to the string. |
|
It copies the contents of string into another. |
|
It returns the reference of last character. |
|
It returns the reference of first character. |
|
It returns the allocated space for the string. |
|
const_iterator cbegin() |
It points to the first element of the string. |
const_iterator cend() |
It points to the last element of the string. |
void clear() |
It removes all the elements from the string. |
const_reverse_iterator crbegin() |
It points to the last character of the string. |
const_char* data() |
It copies the characters of string into an array. |
bool empty() |
It checks whether the string is empty or not. |
string& erase() |
It removes the characters as specified. |
It returns a reference of the first character. |
|
It appends a new character at the end of the string. |
|
It assigns a new value to the string. |
|
char operator[](pos) |
It retrieves a character at specified position pos. |
It searches for the last occurrence of the string. |
|
It references the last character of the string. |
|
It points to the first character of the string. |
|
It reduces the capacity and makes it equal to the size of the string. |
|
It returns pointer to an array that contains null terminated sequence
of characters. |
|
It references the first character of the string. |
|
It reference the last character of the string. |
|
void reserve(inr len) |
It requests a change in capacity. |
allocator_type get_allocator(); |
It returns the allocated object associated with the string |
Let us try some other types of examples using string
Let us try to compare the elements
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char key[] = "mango";
char buffer[50];
do {
cout<<"What is my favorite fruit? ";
cin>>buffer;
} while (strcmp (key,buffer) != 0);
cout<<"Answer is correct!!"<<endl;
return 0;
}
Output
What is my favorite fruit? apple
What is my favorite fruit? banana
What is my favorite fruit? mango
The answer is correct!!
Let us try a concat example
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char key[25], buffer[25];
cout << "Enter the key string: ";
cin.getline (key, 25);
cout << "Enter the buffer string: ";
cin.getline(buffer, 25);
strcat(key, buffer);
cout << "Key = " << key << endl;
cout << "Buffer = " << buffer<<endl;
return 0;
}
Output
Enter the key string: Welcome to
Enter the buffer string: C++ Programming.
Key = Welcome to C++ Programming.
Buffer = C++ Programming.
Let us try a string copy example
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char key[25], buffer[25];
cout << "Enter the key string: ";
cin.getline(key, 25);
strcpy(buffer, key);
cout << "Key = "<< key << endl;
cout << "Buffer = "<< buffer<<endl;
return 0;
}
Output:
Enter
the key string: C++ Tutorial
Key =
C++ Tutorial
Buffer
= C++ Tutorial
Pointers and strings
Pointers in C++ are symbolic representations of addresses. They allow programs to simulate speech and to create and manipulate dynamic data structures. Using pointers, we get the first character of the string, which is the starting address of the string. As shown below, a given string can be accessed and printed using pointers.
Let us try with a C++ program to print string using pointers
#include <iostream>
using namespace std;
int main()
{
string s =
"Hello India";
// pointer variable
declared to store the starting
// address of the
string
char* p = &s[0];
// this loop will
execute and print the character till
// the character
value is null this loop will execute and
// print the characters
while (*p != '\0') {
cout
<< *p;
p++;
}
cout << endl;
return 0;
}
Output
Hello India
No comments:
Post a Comment