Friday, November 15, 2024
Google search engine
HomeData Modelling & AIstring::npos in C++ with Examples

string::npos in C++ with Examples

What is string::npos?

  • It is a constant static member value with the highest possible value for an element of type size_t.
  • It actually means until the end of the string.
  • It is used as the value for a length parameter in the string’s member functions.
  • As a return value, it is usually used to indicate no matches.

Syntax:

static const size_t npos = -1;

Where npos is a constant static value with the highest possible value for an element of type size_t and it is defined with -1.

Note: std::string::npos is a constant that holds the largest possible value of size_t type ( 18446744073709551615 on 64-bit systems ), which is an unsigned integer type. Hence, -1 corresponds to the actual value of std::string::npos.

Program 1: Below is the C++ program to illustrate the use of string::npos.

C++




// C++ program to demonstrate the use
// of string::npos
#include <bits/stdc++.h>
using namespace std;
 
// Function that using string::npos
// to find the index of the occurrence
// of any string in the given string
void fun(string s1, string s2)
{
    // Find position of string s2
    int found = s1.find(s2);
 
    // Check if position is -1 or not
    if (found != string::npos) {
 
        cout << "first " << s2 << " found at: " << (found)
             << endl;
    }
 
    else
        cout << s2 << " is not in"
             << "the string" << endl;
}
 
// Driver Code
int main()
{
    // Given strings
    string s1 = "neveropen";
    string s2 = "for";
    string s3 = "no";
 
    // Function Call
    fun(s1, s2);
 
    return 0;
}


Output

first for found at: 5

Explanation: In the above program string::npos constant is defined with a value of -1, because size_t is an unsigned integral type, and -1 is the largest possible representable value for this type.

What if the valid position for a substring is not found in a string?

Various member functions of the String class return the default value of std::string::npos if a valid position or index for a substring is not found in the string.

Below are the String Functions that return the value of std::string::npos in the case of failure:

  1. find()
  2. rfind()
  3. find_first_of()
  4. find_last_of()
  5. substr()
  6. erase()

Program 2: C++ Program to Illustrate that Some String Functions return the value of std::string::npos in case of failure.

C++




#include <iostream>
using namespace std;
 
int main()
{
    std::string str = "Hello, world!";
    // Returns std::string::npos because "abc" is not found.
    size_t position = str.find("abc");
 
    if (position == std::string::npos)
        cout << "Substring not found";
 
    else
        cout << position;
 
    return 0;
}


Output

Substring not found

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments