-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
58 lines (49 loc) · 1.27 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <string>
#include <fstream>
int main()
{
std::string filename;
std::cout << "Please enter filename(with extension): ";
std::cin >> filename;
std::ifstream file(filename);
if (!file)
std::cerr << filename << " could not be opened!" << std::endl;
else
{
unsigned long numChars = 0;
unsigned long numWords = 0;
unsigned long numLines = 0;
char c = '\n';
bool isWord = false;
while (file.get(c))
{
if (c == '\n')
++numLines;
else
++numChars;
//Adaptation to umlauts is missing!
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
isWord = true; //Word beginning, or c is in a word
else
{
if (isWord)
++numWords; //Word end exceeded
isWord = false;
}
}
if (c != '\n') //The last line does not contain a final \n. still count
++numLines;
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) //The last character is in a word that has not been counted yet
++numWords;
std::cout << '\n';
std::cout << "Number of characters: " << numChars << '\n';
std::cout << "Number of words: " << numWords << '\n';
std::cout << "Number of lines: " << numLines << '\n';
std::cout << '\n';
}
std::cout << "Press Enter to continue . . . ";
std::cin.ignore();
std::cin.get();
return 0;
}