The objective is to enter the number of characters that the chain will have, and find the letter that only repeats once acum_menor
, then show its position. I did it by comparing the string with another one that contains the letters of the alphabet.
But at the moment of showing the position in which the letter that does not repeat is, I can not think of a way to do it, I have to capture the position of the letter whose repetition is less than 2 (I mean 1)
#include <iostream>
#include <string.h>
using namespace std;
int pos_letra= -1;
int main (){
int lon;
cin>>lon;
char* palabra = new char[lon];
cin>>palabra;
int acum, acum_menor=2, i, menor, letra;
char letras[] = "abcdefghijklmnopqrstuvwxyz";
for( letra = 0 ; letras[letra] ; letra++){
for( i = 0 , acum = 0 ; i < lon ; i++){
if( palabra[i] == letras[letra] )
acum++;
if (acum==1 && pos_letra < 0)
pos_letra=i;
}
if( acum != 0 ){
cout << letras[letra] << " se repite " << acum << endl;
if ( acum < acum_menor ){
menor = letra;
acum_menor = acum;
}
}
}
cout << endl<< letras[menor] << " es moss. se repite " << acum_menor << " vez / en los " << lon << " participantes "<< endl;
cout<<pos_letra<<" moss hallado";
return 0;
}
- Entry example:
"xvxx"
. - Example of output:
-
v se repite 1
. -
x se repite 3
.
-