6
Consider a string composed of several subsequences.
for example: cccaaaabbbbxdddddddaaannn.
The smallest subsequence is the letter x, with only one element; the largest subsequence is the letter d, with 9 elements. Make an algorithm to read a string and show which letter occurs in the largest subsequence and its size, as well as the letter that occurs in the smallest subsequence and its size.
Ex.:
Input: aaabbbbaa;
Output: larger b, size 4; smaller a, size 2.
I managed to get the code to register and show correctly the largest subsequence but in the second if
I can’t figure out the correct logic to register and show which least repeated character subsequence.
#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;
}
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site
– Maniero