Consider a string composed of several subsequences.
For example: cccaaaabbbbxdddddddddaaannn.
The smallest substring is the letter x, with only one element; the largest subsequence is that of letter d, with 9 elements. Make an algorithm to read a string and show which is the letter that occurs in the largest subsequence and its size, as well as the letter that occurs in the smallest subsequence and its size.
Ex.:
-
Admission: aaabbbbaa;
-
Output: greater b, size 4; less than, size 2.
I was able to get the code to register and show the highest subsequence correctly but in the second if
I can not figure out the correct logic to register and show the smallest subsequence of repeated characters.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#define n 50
int main()
{
setlocale(LC_ALL,"portuguese");
char cmaior, cmenor;
char v[n];
int i, temp = 1, maior=temp, menor=temp;
printf("\nDigite a string: ");
gets(v);
for(i = 0;i < (strlen(v)-1); i++)
{
if(v[i] == v[i+1])
{
temp++;
}
else
{
temp = 1;
}
if ( temp > maior )
{
maior = temp;
cmaior = v[i];
}
if ( temp <= menor )
{
menor = temp;
cmenor = v[i];
}
}
printf("\n Maior: %c, tamanho %d; Menor: %c, tamanho %d\n",cmaior,maior, cmenor, menor);
return 0;
}