C program to find out the largest sequence of consecutive one from a string

Problem Description:

Suppose, A string(maximum length is 100) of 1’s and 0’s are provided to you. your task is simple and it is to find out the length of largest sequence of consecutive one’s.

Source Code:

#include <stdio.h>

#include <string.h>

int main() {

    char s[101];

    printf(“Take a input string of 0’s and 1’s: “);

    scanf(“%s”, s);

    int l = strlen(s);

    int max_cnt = 0, curr_cnt = 0;

    for (int i = 0; i < l; i++) {

        if (s[i] == ‘1’) {

            curr_cnt++;

        } else {

            if (curr_cnt > max_cnt) {

                max_cnt = curr_cnt;

            }

            curr_cnt = 0;

        }

    }

    if (curr_cnt > max_cnt) {

        max_cnt = curr_cnt;

    }

    printf(“Length of longest sequence of consecutive ones: %d\n”, max_cnt);

    return 0;

}

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Proudly powered by WordPress | Theme: Lean Blog by Crimson Themes.