Department of Computer Science | Institute of Theoretical Computer Science | CADMO

Theory of Combinatorial Algorithms

Prof. Emo Welzl and Prof. Bernd Gärtner

// Programm: linecount.C // Zaehlt Zeilen in einem String #include #include unsigned int linecount(const std::string& s) // POST: Rueckgabewert ist Anzahl der Zeilen in s. { unsigned int c = 0; typedef std::string::const_iterator Scit; for (Scit i = s.begin(); i != s.end(); ++i) if (*i == '\n') ++c; return c; } int main() { // Lies eine ganze Datei auf einmal ein // --> bis eof = end of file std::string f; typedef std::istream::traits_type CT; std::getline(std::cin, f, CT::to_char_type(CT::eof())); std::cout << "Die Datei besteht aus " << linecount(f) << " Zeile(n)." << std::endl; return 0; }