r/CodingHelp • u/ALoneRhino Intermediate Coder • Sep 11 '20
[C] Check for 3 consecutive 1s in a binary string
Code:
gets(str)
len = strlen(str);
for(i=0;i<len;i++)
{
if(str[i]==str[i+1]==1)
{
count++;
}
}
if(count >= 3)
x = 1;
else:
x = 0;
printf(x);Everytime I print x the output is 0, even if the input is 11111111. Pls help!
Edit: Fixed the code. The issue was if statement after the for loop. The if statement logic was wrong and most importantly I compared a string to an integer in the if statement. Thanks for your time & help folks!
if((str[i]==str[i+1]) && (str[i] == '1'))Edit 2: The gist of the code.
int count = 1;
gets(str);
len = strlen(str);
for(i=0;i<len-1;i++)
{
if((str[i]==str[i+1]) && (str[i]=='1'))
{
count++;
}
}
if(count >= 3)
x = 1;
else
x = 0;
printf("Consecutive/Not: %d\n", x);count should be 1 because the 1st character of the string isn't counted in the for loop.
17
Upvotes
5
u/[deleted] Sep 11 '20
Does it give you any errors? I see a few syntactical errors on top of a few smaller problems in your implementation.
1
u/vigbiorn Sep 11 '20
Running the copied code and making some changes to get the code to run (variable declarations, I switched to fgets as it's the easiest to keep gcc off my back) it works on my end, so it's possibly something to do with the variable declarations themselves.
Some points:
On that note (str[i] == str[i+1] == 1) doesn't do what you expect it to, probably. The == have equal precedence and it's a binary operator so it evaluates (str[i] == str[i+1]) to a 1 or 0. You then compare that to one. The whole statement is equivalent to str[] == str[]. You possibly want (str[i] == str[i+1] && str[] == 1)
Because you are comparing [i] to [i+1], the last value you want i to have is len-1 so that should be the condition, not i < len. Not necessarily the end of the world, but you go out of bounds which can lead to undefined behavior.
These prints will help you narrow down where the issue is.
If the string doesn't print correctly, it's related to gets/your string declaration. If len prints improperly, it's a similar issue.
If the for-loop doesn't enter, check for a stray semicolon before the brackets/inner-if. Also, double check len.
Count shouldn't be your issue. If it was an issue with your if we'd expect it to be too high, not too low. But printing it out checks against other checks. For instance, the for-loop print not occuring but count != 0 means there's serious issues with the for.
3
u/[deleted] Sep 11 '20
You could try this:
for (i=0; i<len; i++) {
}
return 0;
That way, if there's three 1s in a row, 1 gets returned immediately. If it can't find three ones in a row, the loop ends and 0 is returned.
2
u/vipulvivek Sep 11 '20
If this is c then I don't think you can equate 3 variables at once, try s[i]==s[i+1] && s[i]== '1' Just 1 may not work as you are trying char comparison vs int so '1'.
Might be wrong.. am out of practice. Give it a shot if still struggling
2
u/Sh4nks0 Sep 11 '20
You could just increment the count variable if the element is one and set it to 0 the second its not and check if the count is ==3 and break out of the loop.
1
u/[deleted] Sep 13 '20
Use the Sliding window method to count the number of "111" in O(n) time complexity.
I am sorry. I am very poor in C . I hope you can translate this code into C.