DEV Community

Mubasshir Ahmed
Mubasshir Ahmed

Posted on

2 2

String find C++

Problem Link: https://codeforces.com/problemset/problem/96/A
We can solve this problem using the string find() function.

#include<bits/stdc++.h>
using namespace std ;
int main()
{
    string s ;
    cin>>s;
    string s1 = "0000000";
    string s2 = "1111111";
    size_t found  = s.find(s1);
    size_t found1 = s.find(s2);
    if(found!=string::npos || found1!=string::npos){
        cout<<"YES"<<endl;
    }
    else {
        cout<<"NO"<<endl;
    }
    return 0;
}

Enter fullscreen mode Exit fullscreen mode

here "size_t" is the signed integer data type. "size_t" can store the maximum size of a theoretical object of any type.
reference: https://en.cppreference.com/w/c/types/size_t
Here we check substring isfound or not in the given string.
Then if the string found then print Yes.

string::npos is a constant representing a non-position. it returns my function find when pattern was not found.

Top comments (2)

Collapse
 
sandordargo profile image
Sandor Dargo

size_t is unsigned, the size of a container cannot be negative

Collapse
 
pgradot profile image
Pierre Gradot

For God's sake, don't use #include<bits/stdc++.h> : stackoverflow.com/Questions/318160...

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post