Question
hi, please help in c++.
I dont understand how to do this, and a lot of the ways on the internet is confusing me, i am a beginner.
with all steps and explantions, will rate!
Write a program that determines the frequency of each char in an input file. Your program should . read in an unknown number
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Ans)

this is counting using ascii encoding from 'a'-'z' i.e 26 charctors are their

assuming alphabet[0] is counting for 'a' likewise others a[25] counting for 'z'

in below function we are reducing current char i.e str[i] - 'a' it will give the

difference between 'a' and given char which is equal to char in array

for example :

assume current chars is c then, 'c'-'a' =2 in ascii and in alphabet[2] will be incremented

alphabet[0] counting for a alphabet[1] for b and alphabet[2] for c

this is way its working

Code:

#include <iostream>

#include <string>

#include <fstream>

using namespace std;

int count[26];                        // to store frequency of every alphabet

char* toCharArray(string out){           //to convert output string to char array

    char* array;

    array = new char[out.size() + 1];

    for (int i = 0; i < out.size(); i++)

    {

        array[i] = out[i];

    }

    return array;

}

int main() {        // main function

    string filename,outfile;

    cout << "Enter File name:";

    cin >> filename;

    cout << "Enter Full name and path of outfile:";

    cin >> outfile;

    ifstream file;

    file.open((filename.c_str()));            //if file does not exist   

    if (!file)

    {

        cout << "Cannot Open file";

        return 0;

    }

    string out,temp;

    out = "";

    while (!file.eof())             //reading all charctors from file

        {

            getline(file,temp);

            out.append(temp);

          

        }

    file.close();

    char *str = toCharArray(out);

//please read this for better understanding   

/*

this is counting using ascii encoding from a-z 26 charctors are their

assuming alphabet[0] is counting for 'a' likewise others a[25] counting for 'z'

in below function we are reducing current char i.e str[i] - 'a' it will give the

difference between 'a' and given char which is equal to char in array

for example :

assume current chars is c then, 'c'-'a' =2 in ascii and in alphabet[2] will be incremented

alphabet[0] counting for a alphabet[1] for b and alphabet[2] for c

this is way its working

*/

    int i = 0, alphabet[26] = {0}, j;

    while (str[i] != '\0') {                       //counting frequency of every alphabet

        if (str[i] >= 'a' && str[i] <= 'z') {

            j = str[i] - 'a';

            ++alphabet[j];

        }

        ++i;

    }

    ofstream output;

    output.open(outfile.c_str());           //opening output file

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

    {

        if (alphabet[i] == 0)           //we are skipping char whose count is zero

        {                               //we will not put them into file    

            continue;

        }

        output << char(i + 'a') << " " << alphabet[i] << "\n";

   }

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
hi, please help in c++. I dont understand how to do this, and a lot of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Programming question Problem: 5. Write a program that reads in a list of integers into...

    C++ Programming question Problem: 5. Write a program that reads in a list of integers into an array with base type int. Provide the facility to either read this array from the keyboard or from a file, at the user's option. If the user chooses file input, the program should request a file name. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is to be...

  • Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...

    Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...

  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

  • Programming Exercise 4.9 Write a script named numberlines.py. This script creates a program listing from a...

    Programming Exercise 4.9 Write a script named numberlines.py. This script creates a program listing from a source program. This script should: Prompt the user for the names of two files. The input filename could be the name of the script itself, but be careful to use a different output filename! The script copies the lines of text from the input file to the output file, numbering each line as it goes. The line numbers should be right-justified in 4 columns,...

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • C++ Write a program that reads students’ names followed by their test scores from the given...

    C++ Write a program that reads students’ names followed by their test scores from the given input file. The program should output to a file, output.txt, each student’s name followed by the test scores and the relevant grade. Student data should be stored in a struct variable of type StudentType, which has four components: studentFName and studentLName of type string, testScore of type int and grade of type char. Suppose that the class has 20 students. Use an array of...

  • This assignment tests your ability to write C programs that handle keyboard input, formatted console output,...

    This assignment tests your ability to write C programs that handle keyboard input, formatted console output, and input/output with text files. The program also requires that you use ctype functions to deal with the logic. In this program, you will input from the user two characters, which should both be letters where the second letter > the first letter. You will then input a file character-by-character and output those letters that fall between the two characters (inclusively) to an output...

  • need help on C++ Discussion: The program should utilize 3 files. player.txt – Player name data...

    need help on C++ Discussion: The program should utilize 3 files. player.txt – Player name data file – It has: player ID, player first name, middle initial, last name soccer.txt – Player information file – It has: player ID, goals scored, number of penalties, jersey number output file - formatted according to the example provided later in this assignment a) Define a struct to hold the information for a person storing first name, middle initial, and   last name). b) Define...

  • Write a C program which computes the count of the prime and perfect numbers within a...

    Write a C program which computes the count of the prime and perfect numbers within a given limit Land U, representing the lower and upper limit respectively. Prime numbers are numbers that have only 2 factors: 1 and themselves (1 is not prime). An integer number is said to be perfect number if its factors, including 1 (but not the number itself), sum to the number. Sample Inputi: 1 100 Sample Outputs: Prime: 25 Perfect: 2 Sample Input2: 101 10000...

  • Hi, it's C++ question. Only can use <iostream> and <fstream>libraries Please help me, thanks Question Description:...

    Hi, it's C++ question. Only can use <iostream> and <fstream>libraries Please help me, thanks Question Description: For this project you will write a program to: a) read-in the 10 first names from a file (the file is a priori given to have exactly 10 entries, of a maximum length of 8 letters each) into a 2-dimensional character array, b) output the names to the terminal with each one preceded by a number indicating its original order in the list, c)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT