Sunday, February 25, 2018

C program : To output the length of the longest run in the string.

In a string, a "run" is a substring with consisting of consecutive
occurrences of the same character. For example, the string
"mississippi" contains the following runs - "ss", "ss" and "pp".

In this question, given a string, you have to output the length of the
longest run in the string.

Sample Input
abbaaacccc

Sample Output
4

 #include<stdio.h>
#include<string.h>

void main()
{
  int max = 1;
   int current = 1;
   int i;
     char s[100];
  scanf("%s",s);
 
 

   for (i = 1; i < strlen(s); i++) {
      if (s[i - 1] == s[i]) {    /* the run continues */
          current++;
          max = current > max ? current : max;
      } else {    /* the run was broken */
          current = 1;
      }
   }
   printf("%d",max);
    
    }

Thanks
Happy Computing !

1 comment:

  1. hello sir,
    how to make a c programme of following question.

    A graph is abstractly a collection of vertices which are labelled by
    non-negative integers, and a collection of edges. A graph called an
    undirected graph if we talk of merely the presence of an edge between
    vertices i and j, rather than its direction.

    For example, the following is a graph:


    In this problem, you are given the edges in an undirected graph. An
    edge is a pair of non-negative integers (i, j) which indicates that
    the vetex i is connected to vetex j by an edge.

    Afterwards, you will be given a vertex number n. You have to output
    the list of vertices which are connected n by an edge, in the order in
    which the edges were input.

    Input

    You are given the following.

    1. The first line contains an integer, E, between 1 and 1000

    2. This is followed by E lines, where each containing a pair of
    numbers i and j where i and j are both non-negative integers <=
    34,000. No edge will be listed more than once.

    3. The last line contains a non-negative integer n <= 34,000. n is
    assured to be a vertex listed in one of the E lines in part (2).

    Output

    You have to output the list of nodes to which n has an edge, in the
    order in which the edges were input, one line for each vertex.

    ReplyDelete

How can I run a C++ program directly from Windows?

How-can-I-run-a-C-program-directly-from-Windows