how to develop a program that requests a phrase or string of text
Use scanf
:
char frase_o_cadena[256] = {0}; // 256 es un tamanyo arbitrario, puede ser insuficiente
scanf("%s", frase_o_cadena);
the times the letters were repeated
This is more complicated, in the first place you have not indicated whether you should be sensitive or insensitive to capital letters, nor do you indicate whether you should consider other characters.
First approach
The best way would be to first create a count buffer in which you accumulate the apparitions of each letter:
unsigned repeticiones['z' - 'a'] = {0}; // Esto deberian tener un tamanyo de 25 elementos
Then, you go through your string letter by letter and add one in each position of repeticiones
:
for (unsigned indice = 0, fin = strlen(frase_o_cadena); indice < fin; ++indice)
++repeticiones[frase_o_cadena[indice] - 'a'];
The trick of the previous code consists in that we obtain the index in which to write subtracting 'a'
( whose value is 97 ) to the corresponding letter in frase_o_cadena
, so a text like hola
would behave like this:
-
h
has a value of 104, subtracting 97 is left: 7 (8th letter of the alphabet).
-
o
has a value of 111, when subtracting 97 it remains: 14 (15th letter of the alphabet).
-
l
has value 108, subtracting 97 is left: 11 (12th letter of the alphabet).
-
a
has a value of 97, subtracting 97 is left: 0 (1st letter of the alphabet).
Note that this code does not count blank spaces or non-alphabetic characters, so when you write a space (
) whose value is 32, subtracting 97 is left in -65 and writing outside of recuento
!.
Case sensitive
To make it case sensitive, the array of repeticiones
must be twice as large:
unsigned repeticiones[('z' - 'a') * 2] = {0}; // Esto deberian tener un tamanyo de 50 elementos
Now we must look for a way to separate uppercase letters, knowing that the value of Z
(90) is lower than the a
(97) this is easy; once we know we can write in the corresponding position of repeticiones
:
for (unsigned indice = 0, fin = strlen(frase_o_cadena); indice < fin; ++indice)
if (frase_o_cadena[indice] > 'Z') // es minuscula?
++repeticiones[frase_o_cadena[indice] - 'a'];
else // es mayuscula!
++repeticiones[(frase_o_cadena[indice] - 'a') + ('z' - 'a')];
With this we get that positions 0 to 24 of the array repeticiones
contain the lowercase count, while positions 25 to 49 contain the uppercase count.
I do not care, tell it all!
If we want to take into account all the characters, it's much easier:
// Solo es valido para char de tamanyo 1
unsigned repeticiones[256] = {0};
for (unsigned indice = 0, fin = strlen(frase_o_cadena); indice < fin; ++indice)
++repeticiones[frase_o_cadena[indice]];
The value of each letter matches its position in the repeticiones
arrangement.
Other things to consider
- The trick of subtracting
a
to z
does not work for all character encodings, if you worked with EBCDIC (improbable, and better that it is) I would fail you.
- Nothing written works for variable-width characters .
- The sample code assumes that the characters lack tildes .