Working with text data-strings
Previous
|< Loops in the programming language C/C++ |
Data type char
It's actually an integer type that contains 8 bits of memory. It is mainly used to store character codes
char c = 'A';
printf ("% d,% c", c, c);
The output will be displayed: 65 A
So, the whole number that represents character A is shown first, and then I am the character. The conversion format "% d" (for integers) is used to display the code, and to display the character "% c".
char c = 'A';
printf ("% d,% c", c, c);
The output will be displayed: 65 A
So, the whole number that represents character A is shown first, and then I am the character. The conversion format "% d" (for integers) is used to display the code, and to display the character "% c".
C-style String character.
It was created within C language and continues to be supported within C ++. This string is actually a one-dimensional string of characters that is interrupted by the zero character '\ 0'.
char text [6] = {'H', 'e', 'l', 'l', 'o', '\ 0'}; maybe this way char text [] = "Hello"; |
|
Read more about strings in the C language on the website: C Strings
Text data in c ++
In C ++, there is a data type string that contains text. It is used like all other types, and some sort of string type is declared in the following way.
using namespace std;
string s = "This is some text.";
std::string s = " This is some text.";
using namespace std;
string s = "This is some text.";
std::string s = " This is some text.";
Enter string from keyboard
- To load a string to a space, a cin command from the iostream file is used
- ·To print a string on the screen, a cout command from the iostream file is used
Primer koda:
#include<iostream>
#include<string>
int main(){
string name, surname;
cin >> name>> surname;
cout << name<< ” “ << surname<< endl;
return 0;
}
Enter the full text line from the keyboard
string text;
getline(cin,text);
The first argument of the getline function is cin, which shows where the text comes from (in this case, from the console, that is, the keyboard).
The second argument is the string variable name in which you want to place the entered text.
The getline function reads the entire line until the user presses [Enter]. This is useful when the input string contains spaces.
getline(cin,text);
The first argument of the getline function is cin, which shows where the text comes from (in this case, from the console, that is, the keyboard).
The second argument is the string variable name in which you want to place the entered text.
The getline function reads the entire line until the user presses [Enter]. This is useful when the input string contains spaces.
Assigning a string
We can assign the value of one string to another.
string str1 = "Hello!";
string str2;
str2 = str1;
One string can be initialized to another as in the following example.
string str1 = "Hello!";
string str2 = str1;
string str1 = "Hello!";
string str2;
str2 = str1;
One string can be initialized to another as in the following example.
string str1 = "Hello!";
string str2 = str1;
"Merge" two strings
Two strings (or more) can be combined using the "+" sabotage operation as we did collect numbers.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name= "Nenad";
string surname= "Milenković";
s = s +” “+ p;
cout << s << endl;
return 0;
}
After executing the program the variable s will contain the string "Nenad Milenkovic".
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name= "Nenad";
string surname= "Milenković";
s = s +” “+ p;
cout << s << endl;
return 0;
}
After executing the program the variable s will contain the string "Nenad Milenkovic".
String length
To determine the length of a string, the length function of the string library is used
string str1;
str1.length()
Example program:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name_of_the_city;
int length_text;
cout << "Enter the city name: ";
getline(cin, name_of_the_city);
length_text= name_of_the_city.length() - 1; // We subtract 1 because of the space
cout << endl <<endl;
cout << "City: " << name_of_the_city<< endl;
cout << "The name of the city contains " << length_text<< " characters." << endl;
return 0;
}
Example of printing this program:
Enter the name of the city: Banatski Karlovac
City: Ibro Banatski Karlovac
The city's name contains 16 characters.
string str1;
str1.length()
Example program:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name_of_the_city;
int length_text;
cout << "Enter the city name: ";
getline(cin, name_of_the_city);
length_text= name_of_the_city.length() - 1; // We subtract 1 because of the space
cout << endl <<endl;
cout << "City: " << name_of_the_city<< endl;
cout << "The name of the city contains " << length_text<< " characters." << endl;
return 0;
}
Example of printing this program:
Enter the name of the city: Banatski Karlovac
City: Ibro Banatski Karlovac
The city's name contains 16 characters.
Comparison of strings
For comparison of strings, we use logical operators>, <, ==,! =,> =, <= Using which we can compare strings in the lexical order, ie, in alphabetical order.
Eg 'c' is in front of 'd' in lexical order. While 'aabc' is in front of 'abc' because the comparison is not done by the length of the string, but in the lexical order.
primer 1:
bool b;
b= (“abak” == “abak” ); //true
b= “Ab”<”AA”; //false because a little 'b' is higher in lexicographic order
// the capital letter 'A'.
Example 2: Examine the accuracy of the password @n0nimus_@n0nimus.
string password;
cin>> password;
if(password== “@n0nimus_@n0nimus”)
{
cout<< “The password is correct”<<endl;
}
else{
cout<<”The password is incorrect”;
}
Eg 'c' is in front of 'd' in lexical order. While 'aabc' is in front of 'abc' because the comparison is not done by the length of the string, but in the lexical order.
primer 1:
bool b;
b= (“abak” == “abak” ); //true
b= “Ab”<”AA”; //false because a little 'b' is higher in lexicographic order
// the capital letter 'A'.
Example 2: Examine the accuracy of the password @n0nimus_@n0nimus.
string password;
cin>> password;
if(password== “@n0nimus_@n0nimus”)
{
cout<< “The password is correct”<<endl;
}
else{
cout<<”The password is incorrect”;
}
Functions for searching in strings
We use the search for a character position or some part of a text within a larger text
find ()
For example, to find "appears" in the given text:
string str1 = "Observed in hypersymmetric proportions, galaxies represent only dots - and so is universal. We have not yet gone to any other galaxy, nor, as far as we know, an intelligent species from another galaxy came to us; however, one day, and that may end. "
find ()
For example, to find "appears" in the given text:
string str1 = "Observed in hypersymmetric proportions, galaxies represent only dots - and so is universal. We have not yet gone to any other galaxy, nor, as far as we know, an intelligent species from another galaxy came to us; however, one day, and that may end. "
int position, position2, position3;
position = str1.find ('is'); // 48
position2 = str1.find (66, 'is'); // 17
position3 = str1.rfind ('is'); // 33
position = str1.find ('is'); // 48
position2 = str1.find (66, 'is'); // 17
position3 = str1.rfind ('is'); // 33
The find function returns the position of the first occurrence of a given character (or if there is no such string in the strings the string: npos will return).
There is also a similar function rfind (reverse, rear search). The rfind function is the position of the last occurrence of a given character, or the string :: npos value.
The rfind function returns the last impression position before the position specified. It can be said that it searches for the first occurrence if we move from the given position to the zero position of the string (that is, moving backward).
There is also a similar function rfind (reverse, rear search). The rfind function is the position of the last occurrence of a given character, or the string :: npos value.
The rfind function returns the last impression position before the position specified. It can be said that it searches for the first occurrence if we move from the given position to the zero position of the string (that is, moving backward).
Substrings
We use the substring function from the header string
string text = "Observed in hypersymmetric proportions, galaxies represent only dots.";
string partText = text.substr (3, 11);
The variable part of the text contains the string "considered", while the variable text remains unchanged.
If we omit the other parameter, the substr function returns the podstring starting from the specified starting position to the end of the string. For example, the following block code assigns a string: "Are only dots representing?" variable podstring.
string text = "galaxies represent only dots?";
string podstring = text.substr (4);
The first parameter must indicate a valid existing position within the string (as with the find function), or the behavior of the function will be undefined. The second parameter, in combination with the first one, can cause the string's length to be exceeded. In this case, the returning podstring will contain all the characters from the specified starting position to the end of the string, as shown in the following example:
string text = "012345";
string fragment = text.substr (2.3); // OK
fragment = text.substr (6,2); // Not ok (1)
fragment = text.substr (3.10); // ok (2)
string text = "Observed in hypersymmetric proportions, galaxies represent only dots.";
string partText = text.substr (3, 11);
The variable part of the text contains the string "considered", while the variable text remains unchanged.
If we omit the other parameter, the substr function returns the podstring starting from the specified starting position to the end of the string. For example, the following block code assigns a string: "Are only dots representing?" variable podstring.
string text = "galaxies represent only dots?";
string podstring = text.substr (4);
The first parameter must indicate a valid existing position within the string (as with the find function), or the behavior of the function will be undefined. The second parameter, in combination with the first one, can cause the string's length to be exceeded. In this case, the returning podstring will contain all the characters from the specified starting position to the end of the string, as shown in the following example:
string text = "012345";
string fragment = text.substr (2.3); // OK
fragment = text.substr (6,2); // Not ok (1)
fragment = text.substr (3.10); // ok (2)
string text = "012345";
string fragment = text.substr (2.3); // OK
fragment = text.substr (6,2); // Not ok (1)
fragment = text.substr (3.10); // ok (2)
string fragment = text.substr (2.3); // OK
fragment = text.substr (6,2); // Not ok (1)
fragment = text.substr (3.10); // ok (2)
(1) This command has undefined behavior because the starting position is not within the given string.
(2) Returns all available characters starting from position 3. In this particular case, this command has the same effect as the fragment = text.substr command (3,3).
(2) Returns all available characters starting from position 3. In this particular case, this command has the same effect as the fragment = text.substr command (3,3).
Delete and insert substrings
The erase function has two input parameters that define the podstring (these are the initial position and the number of substrings) that are deleted from the string, as in the following example:
string text = "This is a text";
text.erase(0,7);
After executing the text.erase command (0.7), the string text contains "text". Podstring with 0 ('O' to 'This') and having 7 characters (including spaces) is deleted from the string, and the string is shortened (a good analogy with this operation is using the [Delete] button in the text editor) .
Instead of deleting the substring, we can substitute the substring with some other string as in the following example:
string text = "This is a test";
text.replace(4,2,"is not");
After executing the text.replace command (4.2, "is not"), the string text contains "This is not a test". Note that we substituted a two-character substring with a string of 4 characters. This is all allowed. The substring can be replaced with a string of arbitrary length. The replace function is a similar insert insertion option in text editors.
string text = "This is a text";
text.erase(0,7);
After executing the text.erase command (0.7), the string text contains "text". Podstring with 0 ('O' to 'This') and having 7 characters (including spaces) is deleted from the string, and the string is shortened (a good analogy with this operation is using the [Delete] button in the text editor) .
Instead of deleting the substring, we can substitute the substring with some other string as in the following example:
string text = "This is a test";
text.replace(4,2,"is not");
After executing the text.replace command (4.2, "is not"), the string text contains "This is not a test". Note that we substituted a two-character substring with a string of 4 characters. This is all allowed. The substring can be replaced with a string of arbitrary length. The replace function is a similar insert insertion option in text editors.
Previous
|< Arrays in C/C++ |
Next
Pointers in C/C++ >| |
Related articles
Strings - examples
Array - examples
Loops in programming language JAVA
Sorting array
Operators in C/C++ languages
Array - examples
Loops in programming language JAVA
Sorting array
Operators in C/C++ languages