Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++, you can temporarily cast a variable to another data type. For example, i

ID: 3711821 • Letter: I

Question

In C++, you can temporarily cast a variable to another data type. For example, if you have an integer value, you can change it to a decimal value by casting. This can also be applied to character data. A character variable can be case as an integer and vise versa.
For example: int value (65); chat data (‘Z’);
So, (char) value will give you the character A and (int) data will give you the number 90.
Write a code that will read from a .txt data file of song lyrics. You can choose any song you like as long at the lyrics are appropriate. Copy and paste the lyrics to a Notepad file. You need to create an array of 26 integer values. 26 should be created as constant. Read in the data file and count how many of each letter there is. Each character should be converted to uppercase.
Example Output: A 5 B 8 C 9 Etc............ In C++, you can temporarily cast a variable to another data type. For example, if you have an integer value, you can change it to a decimal value by casting. This can also be applied to character data. A character variable can be case as an integer and vise versa.
For example: int value (65); chat data (‘Z’);
So, (char) value will give you the character A and (int) data will give you the number 90.
Write a code that will read from a .txt data file of song lyrics. You can choose any song you like as long at the lyrics are appropriate. Copy and paste the lyrics to a Notepad file. You need to create an array of 26 integer values. 26 should be created as constant. Read in the data file and count how many of each letter there is. Each character should be converted to uppercase.
Example Output: A 5 B 8 C 9 Etc............
For example: int value (65); int value (65); chat data (‘Z’);
So, (char) value will give you the character A and (int) data will give you the number 90.
Write a code that will read from a .txt data file of song lyrics. You can choose any song you like as long at the lyrics are appropriate. Copy and paste the lyrics to a Notepad file. You need to create an array of 26 integer values. 26 should be created as constant. Read in the data file and count how many of each letter there is. Each character should be converted to uppercase.
Example Output: A 5 B 8 C 9 Etc............

Explanation / Answer

#include<iostream>

#include<fstream>

#include<iomanip>

using namespace std;

int main()

{

    // open file in read mode

    ifstream in("input.txt");

   

    int arr[26];

   

    int i;

   

    // initialize array to 0

    for( i = 0 ; i < 26 ; i++ )

      arr[i] = 0;

   

    // loop untill file ends

    while( !in.eof() )

    {

        string str;

        in>>str;

       

        for( i = 0 ; i < str.length() ; i++ )

        {

            // get the ascii value of current character

            int ascii = (int)str[i];

           

            // if current character is upper case

            if( ascii >= 65 && ascii <= 90 )

                arr[ ascii - 65 ]++;

            // if current character is lower case

            else if( ascii >= 97 && ascii <= 122 )

                arr[ ascii - 97 ]++;

        }

    }

   

    // open file in write mode

    ofstream out("output.txt");

   

    for( i = 0 ; i < 26 ; i++ )

    {

        out<<setw(5)<<(char)( i + 65 )<<" : ";

        out<<arr[i]<<endl;

   }

   

    return 0;

}

------------------input.txt------------

Ay
Fonsi
DY
Oh
Oh no, oh no
Oh yeah
Diridiri, dirididi Daddy
Go
Sí, sabes que ya llevo un rato mirándote
Tengo que bailar contigo hoy (DY)
Vi que tu mirada ya estaba llamándome
Muéstrame el camino que yo voy (Oh)
Tú, tú eres el imán y yo soy el metal
Me voy acercando y voy armando el plan
Solo con pensarlo se acelera el pulso (Oh yeah)
Ya, ya me está gustando más de lo normal
Todos mis sentidos van pidiendo más
Esto hay que tomarlo sin ningún apuro
Despacito
Quiero respirar tu cuello despacito
Deja que te diga cosas al oído
Para que te acuerdes si no estás conmigo
Despacito
Quiero desnudarte a besos despacito
Firmo en las paredes de tu laberinto
Y hacer de tu cuerpo todo un manuscrito (sube, sube, sube)
(Sube, sube)
Quiero ver…

------------------------------output.txt--------------------------

A : 53
B : 10
C : 17
D : 35
E : 68
F : 2
G : 7
H : 11
I : 36
J : 1
K : 0
L : 24
M : 20
N : 31
O : 64
P : 13
Q : 10
R : 32
S : 44
T : 30
U : 30
V : 7
W : 0
X : 0
Y : 21
Z : 0