Min menu

Pages

VB

Texts in Cplus Plus C++ Strings

 texts inC++

In the world of programming, we say text"String"Whether it consists of a single letter, word, sentence or very large text.
From this point of view, we conclude that the text is a series of characters that do not have a specific size.

In the world of programming, we say "String", whether it consists of a single letter, word, sentence or very large text. From this standpoint, we conclude that the text is a series of letters that do not have a specific size.

How to define a text variable in C++

in languageC++The type was created stringspecifically to store text values ​​and is contained in the filestd.

In C++ the string type was created specifically for storing text values ​​and it is included in the std file.


So if you don't include the package stdin your program, you must define the text like thisstd::string.
In addition, the text value must be enclosed in quotation marks "", as in the following example.

Example
Main.cpp
#include <iostream>
	  
		  using namespace std;
	  
		  int main()
		  {
		  // "Hello World!" And its value is text s. Here we have defined a text variable named
		  string s = "Hello world!";
	  
		  // s Here we have printed the value of the variable
		  cout << s;
	  
		  return 0;
		  }
	

We will get the following result when running.

Hello world!
		

Important information The texts in the bad plus the plus 

The type stringis basically a class(Class)And not like other types of raw data that we knew earlier.
Also, when you declare a variable of its type stringyou are actually creating an object(Object)from him.

The string type is basically a class and not like the other metadata types that we were previously familiar with. Also, when you define a variable of its type, you are actually creating an object from it.

Do not worry at all if you do not understand the meaning of class and object now because you will learn that in boring detail later in the course.

The concept of merging texts inC++

text merge(Concatenation)It means placing a series of texts next to each other to display them as one text. And this is what you will need in any application.

For example, in the programs or websites that you use, you notice that when you create a new account, you are asked to enter your name in two stages as follows:
- Name(First Name).
- family name(Last Name).

After you create your account, you will notice that it has displayed your full name(Name + family name).
When you put the two names next to each other as if they were one text, the programmer actually only combined them and not retyped them again.


In the following example, we will define the variable firstNameto put the name in it, the variable lastNameto put the last name in it, and the variable fullNameto put the name and last name in it.

Example

Main.cpp
#include <iostream>
	  
		  using namespace std;
	  
		  int main()
		  {
		  // and we put a text in it that represents the name firstName here we created the variable
		  string firstName = "saddam";
	  
		  // and we put a text in it representing the lastName lastName here we created the variable
		  string lastName = "alawiri";
	  
		  // and we added between them an empty space lastName and the last name in the variable firstName and put the name in the variable fullName here we created the variable
		  string fullName = firstName + " " + lastName;
	  
		  // Next, the full name that we have merged and put in it will be displayed. fullName Here we have shown the value of the variable
		  cout << fullName;
	  
		  return 0;
		  }
	

We will get the following result when running.

Saddam alawiri
		

How is text stored inC++

Suppose we define stringits name sand give it a value.

Example
string s = "welcome to saddam.com";
		

Since we are using English letters, the text of the variable will be stored sin memory letter by letter and in the order as in this next picture.



Important terms and texts in C-Plus Plus

  • The number of text characters is calledLength.

  • The cell number is called .index.

  • The cell numbers are calledindicesorindexes.

  • If we take a part of the text, this part is calledsubstring.


You, as a programmer, can take advantage of the cell numbers to reach the text content as follows.

Important terms and texts in C-Plus Plus


Text technology information in C Plus Plus 

Dealing with the characters of other languages ​​such as the Arabic characters is not as smooth as dealing with the English characters, and the reason for this is that every Arabic character needs2ByteUntil it is stored in memory, while the English letter needs1ByteJust.

As a practical example, the word Helloconsidered consists of5ByteBecause it consists of 5 English letters.
While it سلامis considered to consist of8ByteBecause it consists of 4 letters and each letter needs2Byte.

Information technical texts in C Plus Plus Dealing with characters from other languages ​​such as Arabic characters is not as smooth as dealing with English characters. The reason for this is that every Arabic letter needs 2Byte to be stored in memory, while the English character needs 1Byte only. As a working example, Hello is 5Byte because it has 5 English letters. Whereas, Salam is considered to be 8Byte because it has 4 letters and each letter needs a 2Byte.


How to access text characters inC++

In the event that you want to pass over the characters of any text, the cells will be considered to have been numbered from left to right, starting from the number 0as we saw a while ago.

How to access text characters in C++


Now if we want to specify the number of the cell that we want to access, whether to display the character in it or to replace it, we must put the name of the variable that contains the text, then the symbol []and in it we specify the number of the cell we want to access.


In the following example, we display the first five characters in the text.

Example
Main.cpp
#include <iostream>
	  
		  using namespace std;
	  
		  int main()
		  {
		  // s Here we have defined a text variable named
		  string s = "Where is my book?";
	  
		  // s Here we have printed the first 5 characters of the variable
		  cout << s[0] << s[1] << s[2] << s[3] << s[4];
	  
		  return 0;
		  }
	

We will get the following result when running.

Where
		

hard idea string::nposinC++

This constant variable is present in the class string, and its idea is that when you try to search the text for a value and you do not find it, its value is returned to you to indicate that all the text has been searched and the value you are looking for was not found.

This constant variable is present in the class string, and its idea is that when you try to search the text for a value and do not find it, its value is returned to you to indicate that all the text has been searched and the value you are looking for has not been found.


Pay attention to an important point, which is that you do not have to save its value, it is enough just to know how it is used in the code.


In the following example, we created a variable of its type stringand name sand put a text in it.
Then we used the function find()to search for a word in the text placed in the variables.

To find out whether the word we want to search for was found in the text or not, we compared the value returned by the function find()after it finished searching with the value of the variablestring::npos.
If the value returned by the function find()is equal to the value of the variable string::npos, then the word to search was not found. If it is not equal to it then it has been found.

Example
Main.cpp
#include <iostream>
	  
		  using namespace std;
	  
		  int main()
		  {
		  // s Here we have defined a text variable named
		  string s = "I'm learning c++ from harmash.com";
	  
		  // result and store the search result in the variable "java" for the word s here we searched in the text in the variable
		  size_t result = s.find("java");
	  
		  // string::npos with the value of result here we have compared the value of
		  // If they are equal, then the word we searched for was not found
		  if (result == string::npos)
		  {
		  cout << "Word not found!";
		  }
		  // If they are not equal, it means that the word we were looking for has been found and the number of the first cell in which the word was found will be printed
		  else
		  {
		  cout << "Word is found at index " << result;
		  }
	  
		  return 0;
		  }
	

We will get the following result when running.

Word not found!
		

The class function stringinC++

The following table contains the stringmost commonly used class functions.

Function name and definition
int length() Returns a number representing the number of characters of the text that called it.
To be precise, the number you return represents from how manyByteText composed.
int size() Returns a number that represents the number of characters of the text that called it, just like a functionlength().
string substr(int pos=0, int len = npos) Returns text that is part of the text (Substring)who summoned her.
In place of the parameter poswe pass indexthe character from which we want to start transcribing the text.
Parameter location lenYou can pass a number representing how many characters you want to copy from the indexparameter posif you don't want to copy all the existing text.
int find(string str) Searching in the text that summoned her for the firstindexStarting from him there is the same text that we pass to her in place of the parameter strand she returns it.
int rfind(string str) Searches in the text that summoned her for anotherindexStarting from him there is the same text that we pass to her in place of the parameter strand she returns it.

string replace(int pos, int len, string replacement) Used to modify a specific part of the text that called it.
Parameter location We pospass indexthe character from which we want to start changing the text.
Parameter location We lenpass a number representing how many characters you want to change as of the indexparameterpos.
The location of the parameter We replacementpass the text to be added in place of the part to be deleted.
void swap(string str) Replace the content of the text that called it with the content of the text we pass in place of the parameterstr.
It also replaces the content of the variable we pass in place of the parameter strwith the content of the text that called it.
string append (const string str) It adds the value of the text we pass in place of the parameter strat the end of the text that called it and returns the resulting text when combined.
bool empty() Returns trueif the invoking text is not empty (ie, contains at least one character).
And return falseif not.
void clear() Clears all characters in the recalled text.
int compare (const string str) Compares the size of the text that called it with the size of the text we pass to it in place of the parameterstr.
Returns 0if they are the same size.
Returns 1if the recalled text has more characters.
Returns -1if the recalled script has a smaller number of characters.
string insert (int pos, const string str) Adds the value of the text we pass in place of the parameter strin the text that called it atIndexThe cell we specify in place of the parameter posand returns the resulting text when merging them together.

Explanation of class functions with examples from here

Operators that are used to compare texts inC++

The following table contains factors that can be used to compare text sizes.

the name of the employee his code Example Explanation of the code
Equal to == (a == b) Is the value aequal to the value b?
If the answer is yes, it will returntrue
Not equal to != (a != b) Is the value anot equal to the value b?
If the answer is yes, it will returntrue
Greater than > (a > b) Is the value agreater than the value b?
If the answer is yes, it will returntrue
Less than < (a < b) Is the value asmaller than the value b?
If the answer is yes, it will returntrue
Greater than
or equal to
>= (a >= b) Is the value agreater or equal to the value b?
If the answer is yes, it will returntrue
Less than
or equal to
<= (a <= b) Is the value asmaller or equal to the value b?
If the answer is yes, it will returntrue

Example

Main.cpp
#include <iostream>
	  
		  using namespace std;
	  
		  int main()
		  {
		  // s2 and a text variable named s1 here we have defined a text variable named
		  string s1 = "alawirisaddam";
		  string s2 = "google";
	  
		  // The print command s2 will be executed equal to the number of characters s1 if the number of characters is
		  if (s1 == s2)
		  cout << "s1 == s2" << endl;
	  
		  // The print command s2 will be executed greater than the number of characters s1 if it is the number of characters
		  if (s1 > s2)
		  cout << "s1 > s2" << endl;
	  
		  // The print command s2 will be executed greater than or equal to the number of characters s1 if the number of characters is
		  if (s1 >= s2)
		  cout << "s1 >= s2" << endl;
	  
		  // The print command s2 will be executed less than the number of characters s1 if it is the number of characters
		  if (s1 < s2)
		  cout << "s1 < s2" << endl;
	  
		  // the print command s2 will be executed less than or equal to the number of characters s1 if the number of characters is
		  if (s1 <= s2)
		  cout << "s1 <= s2" << endl;
	  
		  return 0;
		  }
	

We will get the following result when running.

s1 > s2
		  s1 >= s2
	
_________