Question

Can someone help me write a C++ program such that: 1. It counts the number of...

Can someone help me write a C++ program such that:

1. It counts the number of alphabetic characters in a text file; this mean it ignores any character that is not alphabetic

2. Prompts the user to write the name of the file

3. Count's the number of A's, B's, C's, ..and so on

For the sake of this program, both capital 'A' and lowercase 'a' and treated as equal.

An output would something like this

"A's = 10

B's = 25

C's = 24

D's = 54

etc..."

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

//Code

#include<iostream>

#include<fstream>

using namespace std;

int main(){

              //initializing an array to store counts of all alphabets

              //also assigning value 0 as default

              int counts[26]={0};

              //array to store file name

              char filename[50];

              //prompting and reading filename

              cout<<"Enter filename: ";

              cin>>filename;

              //opening file in read mode

              ifstream inFile(filename);

              //checking if file opened correctly

              if(!inFile){

                           //error

                           cout<<"File not found!"<<endl;

                           return 0;

              }

              char ch;

              //looping and reading all characters one by one

              while(inFile>>ch){

                           //checking if character is an alphabet

                           if(ch>='A' && ch<='Z'){

                                         //upper case letter, subtracting by 'A' to get a value between 0 and

                                         //25, which we can use as an index value to access counts array

                                         int index=ch-'A';

                                         //incrementing counter at index 'index'

                                         counts[index]+=1;

                           }else if(ch>='a' && ch<='z'){

                                         //lower case letter, subtracting by 'a' here to get a value (0-25)

                                         int index=ch-'a';

                                         counts[index]+=1;

                           }

              }

              //closing input file

              inFile.close();

             

              char c='A';

              //looping and printing count of each character

              cout<<endl;

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

                           cout<<c<<"'s = "<<counts[i]<<endl;

                           c++; //next character

              }

              return 0;

}

/*OUTPUT*/

Enter filename: dataa.txt

A's = 33

B's = 7

C's = 16

D's = 6

E's = 34

F's = 7

G's = 4

H's = 19

I's = 17

J's = 0

K's = 2

L's = 10

M's = 10

N's = 18

O's = 22

P's = 9

Q's = 1

R's = 18

S's = 24

T's = 36

U's = 10

V's = 0

W's = 4

X's = 1

Y's = 1

Z's = 0

Add a comment
Know the answer?
Add Answer to:
Can someone help me write a C++ program such that: 1. It counts the number 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 program: Write a program that assigns and counts the number of each alphabetic character in...

    C program: Write a program that assigns and counts the number of each alphabetic character in the Declaration of Independence and sorts the counts from the most used to the least used character. Consider upper and lower case letters the same. The frequency of each character should be accumulated in an array. USE POINTERS to increment counts for each letter, sort your array, and display the results. Output should consist of two columns: (1) each letter 'a'-'z' and (2) the...

  • In this program, you will be using C++ programming constructs, such as overloaded functions. Write a...

    In this program, you will be using C++ programming constructs, such as overloaded functions. Write a program that requests 3 integers from the user and displays the largest one entered. Your program will then request 3 characters from the user and display the largest character entered. If the user enters a non-alphabetic character, your program will display an error message. You must fill in the body of main() to prompt the user for input, and call the overloaded function showBiggest()...

  • C Program In this assignment you'll write a program that encrypts the alphabetic letters in a...

    C Program In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters Your program must compile and run from the command line. The program executable must be named “vigenere” (all lower...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • Word count. A common utility on Unix/Linux systems. This program counts the number of lines, words...

    Word count. A common utility on Unix/Linux systems. This program counts the number of lines, words (strings of characters separated by blanks or new lines), and characters in a file (not including blank spaces between words). Write your own version of this program. You will need to create a text file with a number of lines/sentences. The program should accept a filename (of your text file) from the user and then print three numbers: The count of lines/sentences The count...

  • You're to write a C++ program that analyzes the contents of an external file called data.txt....

    You're to write a C++ program that analyzes the contents of an external file called data.txt. (You can create this file yourself using nano, that produces a simple ASCII text file.) The program you write will open data.txt, look at its contents and determine the number of uppercase, lowercase, digit, punctuation and whitespace characters contained in the file so that the caller can report the results to stdout. Additionally, the function will return the total number of characters read to...

  • In this lab, you will write a C program to encrypt a file. The program prompts...

    In this lab, you will write a C program to encrypt a file. The program prompts the user to enter a key (maximum of 5 bytes), then the program uses the key to encrypt the file. Note that the user might enter more than 5 characters, but only the first 5 are used. If a new line before the five characters, the key is padded with 'a'; Note that the new line is not a part of the key, but...

  • Write a C program that reads characters from a text file, and recognizes the identifiers in...

    Write a C program that reads characters from a text file, and recognizes the identifiers in the text file. It ignores the rest of the characters. An identifier is a sequence of letters, digits, and underscore characters, where the first character is always a letter or an underscore character. Lower and upper case characters can be part of the identifier. The recognized identifier are copied into the output text. b. Change the C program in exercise 2, so that all...

  • Write a program in C++ that prompts the user to input the name of a text...

    Write a program in C++ that prompts the user to input the name of a text file and then outputs the number of words in the file. You can consider a

  • Write a complete Python program with prompts for the user for the main text file (checks...

    Write a complete Python program with prompts for the user for the main text file (checks that it exists, and if not, output an error message and stop), for any possible flags (including none), and for any other input that this program may need from the user: split has an option of naming the smaller files head_tail list the first 10 lines (default) and the last 10 lines (default) in order of the given text file flag: -# output #...

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
Active Questions
ADVERTISEMENT