-1

here is a simple code for testing a string contains only 0 and 1 or not ok and not in code means it contains only 0 and 1 or not respectively. this is giving output as not ok how?

#include<iostream>
using namespace std;
void digi(string s)
{
    bool flag1=true;
    bool flag2=true;
    int l=s.size();
    for(int i=0;i<l;i++)
    {
        cout<<s.at(i)<<endl;
        if(s.at(i)!='0')
        flag1=false;
        if(s.at(i)!='1')
        flag2=false;
    }
    if(flag1==true&&flag2==true)
    cout<<"ok";
    else
    cout<<"not ok";
}
main()
{
    string s;
    cin>>s;
    digi(s);
}

mr. abhi
  • 221
  • 1
  • 2
  • 7
  • 4
    What is your question? Is there something wrong with your code? – Blaze Dec 10 '19 at 08:52
  • Many things wrong in your code on first glance. Using `string` without including ``; `int l=s.size()` may cause undefined behaviour, as `string::size` most likely returns `size_t`; the signature of `main` is non-standard and therefore wrong. As to why the algorithm doesn't work, that's secondary. – DeiDei Dec 10 '19 at 08:54
  • this code is giving output as not ok – mr. abhi Dec 10 '19 at 08:56
  • @mr.abhi For what input? – Lukas-T Dec 10 '19 at 08:57
  • input is 1101 means input string containing 01 should give output as ok but it is giving as ok only how?@churill – mr. abhi Dec 10 '19 at 09:00
  • 2
    Check your logic. You only need one flag to know if any character is not 0 _**OR**_ 1. – acraig5075 Dec 10 '19 at 09:01
  • Ya you're always going to get at least one of them false in your current logic – lenz Dec 10 '19 at 09:03
  • what should i do if i have to check that string should contain 0 and 1 only@acraig5075 – mr. abhi Dec 10 '19 at 09:06
  • now i get to know my logic is wrong but what i should do so that it checks that string contains only 0 or 1@calvinBroadus – mr. abhi Dec 10 '19 at 09:07

4 Answers4

0

Let's take a closer look at this:

if(s.at(i)!='0')
    flag1=false;
if(s.at(i)!='1')
    flag2=false;

One of those two conditions will always be true, thus for any given non-empty string either flag1 or flag2 or both will become false. Thus if(flag1==true&&flag2==true) will be false for any non-empty string.

You don't actually need any flags here. You can just look for the first character, that is neither '0' nor '1' and then break. Like this:

for(size_t i = 0; i < s.length(); i++)
{
    if(s.at(i) != '0' && s.at(i) != '1')
    {
        std::cout << "not Ok";
        return; // at this point the check failed, we can stop here.
    }
}

std::cout << "Ok";

But there is an even simpler solution. You can use find_first_not_of, to find the first character that is neither '0' nor '1'.

if(s.find_first_not_of("01") == std::string::npos)
{ /* there are only 0's and 1's */ }        
else
{ /* there are characters, other than 0 and 1 */ }
Lukas-T
  • 11,133
  • 3
  • 20
  • 30
  • thanks for ur support...its working but can u pls tell me what are these std::cout i just used to write cout – mr. abhi Dec 10 '19 at 09:12
  • @mr.abhi I think [this question](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) will provide an in depth explanation for this :) – Lukas-T Dec 10 '19 at 09:14
0

Here this works. I just changed it to a single flag. You don't need two.

#include <string>
#include<iostream>
using namespace std;
void digi(string s)
{
    bool flag=true;
    int l=s.size();
    for(int i=0;i<l;i++)
    {
        cout<<s.at(i)<<endl;
        if((s.at(i)!='0') && (s.at(i)!='1')) flag=false;
    }
    if(flag)
    cout<<"ok";
    else
    cout<<"not ok";
}
int main()
{
    string s;
    cin>>s;
    digi(s);
}
lenz
  • 2,193
  • 17
  • 31
0

The logic of the function is incorrect.

The flag1 is set to false then the current character is not equal to '0'. It can be equal to the valid value '1' but the flag is set to false.

The same situation takes place with the flag2 that is set to false when the current character is not equal to '1'.

So for a valid string the function can output "not ok".

The function should not output anything. What it should do is to return a Boolean value.

Its parameter shall be a constant reference to string.

You can use the standard algorithm std::all_of to check a string.

Here is a demonstrative program

#include <iostream>
#include <string>
#include <iterator>
#include<algorithm>
#include <cstring>

bool consists_from( const std::string &s, const char *t = "01" )
{
    return std::all_of( std::begin( s ), std::end( s ),
                        [=]( const auto &c )
                        {
                            const char *p = strchr( t, c );
                            return p && *p;
                        } );
}

int main() 
{
    std::cout << ( consists_from( "011001" ) ? "ok" : "not ok" ) << '\n';;
    std::cout << ( consists_from( "01A1001" ) ? "ok" : "not ok" ) << '\n';

    return 0;
}

Its output is

ok
not ok
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Based on your comment you need something like as below code.

#include<iostream>
using namespace std;
void digi(string s)
{
    bool flag1;
    bool flag0;
    for(int i=0; i<s.size(); i++)
    {
        cout<<s.at(i)<<endl;
        if(s.at(i)!='0' && s.at(i)=='1' )//check for '1'
            flag1=true;
        else if(s.at(i)!='1' && s.at(i)=='0')//check for '0'
            flag0=true;
        else{//for other 
            flag1 =false;
            flag0 = false;
        }
    }
    if(flag1 && flag0)
        cout<<"ok";
    else
        cout<<"not ok";
}
main()
{
    std::string s;
    cin>>s;
    digi(s);
}
Bharat Vankar
  • 317
  • 2
  • 13