C++ string::npos

C++ is a language that is used to reinforce the basic programming concepts and improve the programmers’ logical thinking. In C++, we deal with arrays and strings since they are crucial to any programming language. In arrays, we store the same kind of data and a string is like an array of characters that is capable to store more than one character in it. To find the length of the array, we should know its size that we define at the time of initialization of the array. In the case of strings, we sometimes need to check the whole string to find a character or word. To find the string size, the len() function is used. But if we want to check the string or find a value, how can we find a character or word in a string? For that purpose, we use the “string::npos” method which is a constant static value. The “static” word reserves the memory for the variable and “constant” tells the compiler that this value cannot be changed until the program execution.

The “string::npos” is a static constant that has the highest value of -1 with a “size_t” element that tells the compiler on the size of a string and tells how the compiler checks the character until the “size_t” is met. When the required element matches the value of a string, the compiler automatically changes from -1 to 0 by incrementing 1. “Npos” means no position, it shows the position, and is initially set to -1. When an element is found, it is incremented.

Library:

#include <bits/stdc++.h>

By importing this, we will be able to invoke all the methods of standard libraries of C++. We find the string length and compare the string with other strings. So, by integrating this header file, we don’t need to separately import the libraries. The “string::npos” is a technique which is used to find the input and output and it can be used without importing the “iostream”. By including the previously-mentioned line of code, we can use all the functions of standard libraries in our code.

Syntax:

static const size_t npos=-1;

variable_name != string::npos

We don’t need to write the first statement because it is auto-fed. This statement checks the string from the 0 index until the end to find the matching record from a string. We use the second statement where we want to check if that one string’s elements match the other string’s elements. If it matches, it returns true. Otherwise, the statement turns false. Without going inside the condition’s body, the compiler executes the next statement.

C++ Finds the String Elements by Including the Header File in the Code

Let’s learn how to use the “string::npos” method by importing the #include <bits/stdc++.h> header file.

Code:

#include <bits/stdc++.h>

using namespace std;
int main() {
  string str_0 = "the";
  string str_1 = "Elizabeth the queen";
  int is_found=str_1.find(str_0);
  if (is_found != string::npos){
cout<<str_0<< " Found at: " << (is_found) <<endl;
  }
}

In the first statement, we import the <bits/stdc++.h> header file so that we can employ any function of any standard library. Then, initialize two string type variables named “str_0” and “str_1”. This is done inside the main() function. Next, declare another variable which is “is_found” of integer type that saves the result. Use the .find() method to find the “str_0” string in “str_1”. If the required string is found, it returns the index to “is_found” where the value is found. Now, use the conditional “if” statement to check whether the “is_found” is equal to the “string::npos” or not. Here, the value of “string::npos” is -1. When the string is not found and the “is_found” has the value of -1, the condition becomes false. When a condition is true, the compiler executes the body of “if” that contains one “cout” statement to print the text and the index where the “str_0” value is found.

Output:

the Found at: 10

C++ Finds the String 1 in String 2 Using the “String::Npos” Method

Let’s find the string in another string with the help of the “string::npos” function.

Code:

#include <iostream>

using namespace std;
int main() {
  string str_0 = "My";
  string str_1 = "Color My Hair";
  int is_found=str_1.find(str_0);
  if (is_found != string::npos){
cout<<str_0<< " Found at: " << (is_found) <<endl;
  }
  else
cout<<"Not Found";
}

Here, integrate the <iostream> library so that we can access the “cout” method of this library. In the main() method, two string type variables, “str_0” and “str_1”, are declared. In the next statement, the “is_found” integer type variable is initialized. It contains the output when two strings are compared. The find() method is applied and it finds the “str_0” string in “str_1”. If it is found, the index where the value is found is returned to the “is_found”. Now, check that the “is_found” value matches the “string::npos” or not. For that purpose, we invoke the “if” statement. If the condition is met, the body of the “if” statement is carried out. Otherwise, the “else” statement is carried out. The body of the “if” has a message with the index where the required value is found in the string and the body of “else” contains a message of not found.

Output:

My Found at: 6

C++ Uses “String::Npos” by Creating a Function

Let’s construct a function and then call that function in the main() method to find the first string in the second one.

Code:

#include <bits/stdc++.h>

using namespace std;
void function_str(string str_0, string str_1)
{
    int got = str_0.find(str_1);
    if (got != string::npos) {
        cout<< str_1<< " Found at: "<< (got) <<endl;
    }
    else
        cout<< str_1 << " is not found ";
}
int main()
{
    string str_0 = "My Book is";
    string str_1 = "Book";
    string str_2 = "My";
    function_str(str_0, str_1);
    function_str(str_0, str_2);
    return 0;
}

First, the function_str() method is called. Here, we pass two arguments in it. Inside this function, define the “got” integer type variable and assign it with “str_0.find(str_1)”. This function determines the index of the value where the “str_1” is found in “str_0”. Next, use “if” to find whether “got” is equal to “string::npos” or not. The “if” body is employed if the condition is satisfied, and the “else” statement is employed if the condition is not satisfied. Now, invoke the main() method. Here, we declare the three string type variables as “str_0”, “str_1”, and “str_2”. In the next statement, call the function_str() method and pass two attributes. The method is processed and prints a message of found or not found on the console. The benefit of using this method is that it does not matter how many times we call this function; it will process each time we call it. Here, we call this method twice and pass the different arguments.

Output:

Book Found at: 3

My Found at: 0

Conclusion

We studied the “string::npos” method of the string to match one string with another. This methodology is also applied to check whether a specific element of one string is present in the other string or not. We started the article with a brief introduction to the strings and string::npos method. The important header files that are possibly imported in the code to call this method are also discussed. This article contains the syntax and coding examples of the “string::npos” in detail. This article brings you the usage of the “string::npos” method with practical application.



from https://ift.tt/koqBnuJ

Post a Comment

0 Comments