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

Write a program as a character counter, which is able to tell you how many chara

ID: 3686895 • Letter: W

Question

Write a program as a character counter, which is able to tell you how many characters a text file contains. This program (1) opens a text file; (2) read the file character by character and count the number of character (including the space character); (3) print out the number of characters this file has; and (4) close the file. Write a program to scan a text file and print out the number of 'e' or "E' the file has. Write a program to print out all the odd numbers between 1 to 200. One number per line please.

Explanation / Answer

ANSWER No. 1. ===============================================

#include <iostream>

#include <fstream>

using namespace std;

int main() {

ifstream fin;

fin.open("my-input-file.txt", ios::in);

char my_character ;

int number_of_lines = 0;

while (!fin.eof() ) {

fin.get(my_character);

                cout << my_character;

                                if (my_character == ' '){

                                                ++number_of_lines;

                                }

                }

cout << "NUMBER OF LINES: " << number_of_lines << endl;

}

Compilation g++ read-characters.cpp -o read-characters

ANSWER No. 2. ===============================================

#include <iostream>

#include <cctype> // re. tolwer, isalpha

const int NUM_LETS = 26;

int main()

{

    // initial all to 0 //

    int count[NUM_LETS] = { 0 }; // this is all that's needed here

    //now the interactive bit

    std::cout << "Enter a line of text : " << std::flush;

    std::string line;

    std::getline( std::cin, line );

    for( size_t i = 0; i < line.size(); ++ i )

    {

        int curChar = tolower( line[i] ); // letters "are int's" in C/C++ //

        if( isalpha(curChar) )

        {

            ++ count[curChar - 'a']; // get int in range 0..25 //

        }

    }

    for( size_t i = 0; i < NUM_LETS; ++ i )

  {

        if( count[i] ) // just show counts of letters that are there //

            std::cout << "There were " << count[i]

                      << " occurences of " << static_cast< char >(i + 'a')

                      << " "; //add 'a' for the same reason as above, though we have to cast it to a char.

    }

    std::cout << "Press 'Enter' to continue/exit ... " << std::flush ;

    std::cin.get();

    return 0;

}

ANSWER No. 3. ===============================================

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote