Table of contents
1.
Introduction
2.
What is string__npos in C++?
3.
Syntax
3.1.
Use Cases of String::npos
4.
Library
5.
Example
5.1.
Implementation
5.2.
C++
6.
Uses of string__npos
6.1.
Substrings Search
6.2.
Character Existence  
6.2.1.
Implementation
6.3.
C++
6.4.
Extracting Substrings
6.4.1.
Implementation
6.5.
C++
7.
Frequently Asked Questions
7.1.
What is size_t?
7.2.
What is the value and use of string::npos in C++?
7.3.
What does string :: NPOS include?
8.
Conclusion
Last Updated: Dec 30, 2024
Medium

string::npos in C++

Author Jay Dhoot
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Do you know how we can find a character or word in a string? Do you know that string__npos in C++ helps in finding a character or word in a string? 

string__npos in C++

In this article, we will learn about the string__npos in C++. We will also see a coded example using the string::npos method to find whether one string is contained inside another.

What is string__npos in C++?

It is a static member constant value with the maximum possible value for an element of value size_t. The static keyword helps reserve the memory, while the constant keyword makes it a read-only variable, i.e., its value can not be changed. It means "until the end of the string" when this value is used as the value for a len (length) parameter in the string's member functions. The constant is defined with a value of -1 as size_t is an unsigned integral type value which is the maximum representable value for this type.

In simple words, npos is similar to no-position, whose return value indicates that no matches were found in the string. Therefore, if a true value is returned, it means that the matches are found at no position.

Syntax

static const size_t npos = -1;
You can also try this code with Online C++ Compiler
Run Code


Here, npos is a constant static value with the maximum possible value of size_t. It is defined/stored with a value of -1.

Use Cases of String::npos

static const size_t npos=-1;
variable != string::npos
You can also try this code with Online C++ Compiler
Run Code


The above code helps in finding the matching records from a string. It checks the string from the 0th index till the end of the string. The second statement is a conditional statement that, if it holds true, i.e., if matches are found, then goes inside the condition’s body, while if it is false, then the compiler executes the next statement.
 

*Note: The first statement is already fed/present in the compiler. We don’t have to write it. It is just written for representation in the above code.

Library

#include <bits/stdc++.h>
You can also try this code with Online C++ Compiler
Run Code

 

By including the above library in the code, we can use string::npos. string::npos can be used without including "iostream" and is used to find the input and the output. We find the length of the string and then compare the string with other strings.

Example

The below program finds the string s1 in string s2 using the “String::Npos” Method

Implementation

  • C++

C++

// Library file
#include <bits/stdc++.h>
using namespace std;


// Function to check the index of the occurrence
// of any string in the given string using
// string::npos
void check(string s1, string s2)
{
// Finding position of string s2
int pos = s1.find(s2);


// Condition Check if pos is -1 or not
if (pos != string::npos) {
cout<<s2<<" found at index "<<pos<<" in the string "<<s1<<endl;
}


else
{
cout<<s2<<" is not present in the string "<<s1<<endl; 
}

}


int main()
{
   // Defining strings
string s1 = "Coding Ninjas";
string s2 = "Ninjas";
string s3 = "Be a Ninja";


// Function call on string s1 and s2
cout<<"Checking if "<<s1<<" contains "<<s2<<endl;
check(s1, s2);
cout<<endl;

// Function call on string s1 and s3
cout<<"Checking if "<<s1<<" contains "<<s3<<endl;
check(s1, s3);
cout<<endl;


return 0;
}
You can also try this code with Online C++ Compiler
Run Code


Output

Output

 

Explanation

The check function in the above code helps in finding the matching strings. Using the .find() method, we find whether string s1 contains s2 or not. If string s1 contains s2, it returns the index in s1 where s2 is found, or it has the value -1. Using the conditional statement, we check whether the variable "pos" is equal to string::npos or not. If pos is -1 (i.e., no match is found), the else block statements are executed as the value of string::npos is -1. Otherwise, the statements inside the if block are executed.

Uses of string__npos

Some of the uses of string__npos in C++ are as follows:

Substrings Search

The value of string__npos helps in indicating whether a substring is present inside a string or not. As seen in the above example, “Ninjas” is a substring of “Coding Ninjas” while “Be a Ninja” is not.

Character Existence  

string__npos helps in finding whether a character is present in a string or not. The find() function helps in finding the position of the first occurrence of the character (if it exists, else it returns -1). For example,

Implementation

  • C++

C++

#include <bits/stdc++.h>
using namespace std;


// Code to find the index of the occurrence
// of any character in the given string using
// string::npos
int main()
{
   // Defining string s
string s = "Coding Ninjas";
char character='i';
int a=s.find(character);

if(a==string::npos)
{
cout<<"Character '"<<character<<"' is not present in "<<s<<endl;
}
else
{
cout<<"Character '"<<character<<"' is present at index "<<a<<endl;
}
cout<<endl;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code


Output

Output

Extracting Substrings

The string__npos helps in extracting substring from a bigger substring with the help of substr() function. The substr() function takes the starting position and the length as arguments. If we want to extract a substring from the starting position of a string till the end of the string, we can pass string__npos as the length argument. For example,

Implementation

  • C++

C++

#include <bits/stdc++.h>
using namespace std;


// Code to extract substring
// from a given string with
// string::npos
int main()
{
   // Defining string s
string s = "Coding Ninjas";
string sub=s.substr(7,string::npos);

//Substring from 7th position till the end of the string
cout<<sub<<endl;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code


Output

Output

Frequently Asked Questions

What is size_t?

size_t is an unsigned integral data type representing objects' size in bytes. It is widely used in various libraries to represent counts and sizes. The size_t data type is never negative.

What is the value and use of string::npos in C++?

string::npos is a static member with a constant value of -1. npos is similar to no-position, whose return value indicates no matches were found in the string. Therefore, if a true value is returned, it means that the matches are found at no position.

What does string :: NPOS include?

std::string::npos is a constant in the C++ Standard Library that represents the maximum possible value for an index in a string. It is used to indicate that a substring or character was not found within the string.

Conclusion

In this article, we have discussed the string::npos method in C++. To learn more about string functions, refer to the article below. We hope this article has helped you understand the string::npos method in C++. If this article helped you in any way, then you can read more such articles on our platform,
 

Live masterclass