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 !
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 !