6 like 0 dislike

We wish to write a program countletter.cpp that takes a filename and a single character on the command line and prints the number of occurrences of that character in the file (case sensitive).

For example, if foo.txt contains

this is a test

then:

input:

foo.txt t

output:

foo.txt contains 3 of t

 Please finish the code below. Remember:

- We do not have any spaces in the answer.

- There are 2 times try only. It's better if you can get 100% correct. If you are not, we'll check for you by ourself!

#include <iostream>
#include <$1>                                     // Line 1
using namespace std;
int main()
{
    $2 filename[20], letter;                       // Line 2 
    cout << "What file would you like to open?";
    cin >> $3;                                 // Line 3
    cout << "\nWhat letter would you like to count?";
    cin >> letter;
    ifstream infile(filename);
    if (!infile)
        cout << $4 << " not found\n";             // Line 4
    else
    {
       int count = 0;
       char c;
       while (infile.get(c))
          if ($5)                            // Line 5
             ++count;
       cout << filename << " contains " 
            << $6 << " of " << letter << "\n";     // Line 6
    }
    return 0;
}

 

asked in Final Exam by (9.7k points)
reshown by | 25 views
0 0
fstream
char
filename
letter
letter
count
0 0
fstream
char
filename
"file"
c==letter
count

52 Answers

1 like 0 dislike
Hidden content!
* **

[2]:char
**** * *
** ***** ** ** *
** **
*******
answered by (11.8k points)
0 0
Your answer may difference, but if it's correct, you will get reward!
0 like 0 dislike
Hidden content!
* *
**
*** * *
*** **
* == c
* *
answered by (-155 points)
0 like 0 dislike
Hidden content!
* * *
* *** *
* * ** * t
answered by (157 points)
reshown by
0 like 0 dislike
Hidden content!
*** **
******
*** ** *
**** *
* * == c
** * *
answered by (-74 points)
0 like 0 dislike
Hidden content!
* * *** *
***
** *
*
** ** ** * * *****

[6]:count
answered by (10.8k points)
0 like 0 dislike
Hidden content!
* *****
** ***
* ****
**** *
*** * *** **

[6]:count
answered by (38 points)
0 like 0 dislike
Hidden content!
***** *

[2]:char
* ** **
* ** * *
**** *
**
answered by (-359 points)
0 like 0 dislike
Hidden content!
****
*
answered by (-605 points)
0 like 0 dislike
Hidden content!
** **
* * *
** *
* * *
** == letter
*** *
answered by (-74 points)
0 like 0 dislike
Hidden content!
* ****

[2]:char
*
** ** **
** * **
* *
answered by (-121 points)
Get it on Google Play Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.68.65.36
©2016-2018

Related questions

3 like 1 dislike
8 answers
[Exercise] Essay (Open question) - asked Jun 22, 2016 in Final Exam
Available when: 2016-06-22 14:30:00 - Due to: 2016-06-22 17:00:00
| 31 views
7 like 0 dislike
25 answers
[Exercise] Coding (C) - asked Jun 22, 2016 in Final Exam
Available when: 2016-06-22 14:30:00 - Due to: 2016-06-22 17:00:00
| 33 views
7 like 0 dislike
44 answers
[Exercise] Multichoice - asked Jun 21, 2016 in Final Exam
Available when: 2016-06-22 14:30:00 - Due to: 2016-06-22 17:00:00
| 17 views
6 like 0 dislike
32 answers
[Exercise] True/False - asked Jun 21, 2016 in Final Exam
Available when: 2016-06-22 14:30:00 - Due to: 2016-06-22 17:00:00
| 29 views
5 like 1 dislike
24 answers
[Exercise] Coding (C) - asked Jun 21, 2016 in Final Exam
Available when: 2016-06-22 14:30:00 - Due to: 2016-06-22 17:00:00
| 34 views
2,974 questions
57,433 answers
29,089 comments
938 users