Good evening! You need to write a program that counts the number of spaces, full stops and commas in the file. I tried to do this by creating an array of type char, but the teacher did not approve)) I do not quite understand how you can accomplish this task without creating an array, but using ifstream / ofstream and related functions. Is it possible to somehow save my program by making minor changes so that it works correctly?)) And I don’t really want to rewrite everything all over again .. ((Thanks in advance!

#include <iostream> #include <string.h> #include <cstdlib> #include <stdio.h> using namespace std; int main () { char a[1000]; cout << "Enter your text: " << endl; gets (a); int spc = 0, dt = 0, km = 0; int lng = strlen(a); for (int d = 0; d < lng; d++) { if (a[d] == ',') km++; } for (int c = 0; c < lng; c++) { if (a[c] == '.') dt++; } for (int b = 0; b < lng; b++) { if (a[b] == ' ') spc++; } if (spc >= 1) { cout << "The number of spaces in the text is: " << endl << spc << endl; } else { cout << "There are no spaces in the text" << endl; } if (dt >= 1) { cout << "The number of dots in the text is: " << endl << dt << endl; } else { cout << "There are no dots in the text" << endl; } if (km >= 1) { cout << "The number of commas in the text is: " << endl << km << endl; } else { cout << "There are no commas in the text" << endl; } } 
  • If you want less code, then strsep() your friend. - 0andriy

5 answers 5

 #include <iostream> #include <fstream> #include <algorithm> #include <string> int main(int argc, char *argv[]){ std::ifstream file(argv[1]); size_t km = 0, dt = 0, spc = 0; if(file.is_open()){ std::string line; while(getline(file, line)){ km += count(line.begin(), line.end(), ' '); dt += count(line.begin(), line.end(), ','); spc += count(line.begin(), line.end(), '.'); } }else{ std::cerr << "Erroe file open!" << std::endl; return -1; } std::cout << km << " " << dt << " " << spc << std::endl; return 0; } 

So each character is considered separately.

    Of course you can. For example:

      int x; while ( (x = cin.get()) != -1) switch (x){ case ',': km++; break; case '.': dt++; break; case ' ': spc++; break; } 

    Code about, just fine-tuned to your requirements.

    • Why not just char x; while (cin.get(x)) ... char x; while (cin.get(x)) ... ? - avp
    • @avp then it seems that the letter E does not read chtoli ... Habit) - pavel
    • Checked (even in Windows g ++ 3.4.5). Normally reads up to the end of the file both ёЁ and яЯ - avp

    Very minor changes:

     #include <algorithm> #include <iostream> #include <iterator> #include <cstring> int main() { std::noskipws(std::cin); std::cout << std::count_if(std::istream_iterator<char>(std::cin), std::istream_iterator<char>(), [](auto ch) -> bool { return ::strchr( ",. ", ch); }) << "\n"; } 

    This is if you need a total count.

       #include <iostream> #include <fstream> int main(int argc, char *argv[]) { const char *fileName = "C:/avia.txt"; std::ifstream file(fileName); std::map<char, int> chars; while(!file.eof()) { char ch; file.get(ch); switch(ch) { case ' ': case '.': case ',': chars[ch]++; break; } } for(auto it = chars.begin(); it != chars.end(); it++) { std::cout << "Char: '" << it->first << "' count: " << it->second << std::endl; } return 0; } 
       #include <iostream> #include <fstream> #include <string> int main(int argc, char *argv[]){ std::ifstream file(argv[1]); size_t col = 0; if(file.is_open()){ std::string characters{" .,"}; std::string line; while(getline(file, line)){ std::string::size_type pos = 0; while((pos = line.find_first_of(characters, pos)) != std::string::npos) ++col, ++pos; } }else{ std::cerr << "Error open file!" << std::endl; return -1; } std::cout << col << std::endl; return 0; } 

      The full version, which counts the total number of characters ",."