Good afternoon) Help to write a program that counts the number of words in each sentence.

#include <iostream> #include <stdio.h> #include <string.h> using namespace std; main() { setlocale(LC_ALL, "Russian"); char str [80]; cout << "Enter string:\n"; cin.getline(str,80); int len = strlen(str); int i, count = 1, line =1; for (i = 0; i < len; i++){ if (str[i] == ' '){ count++; } else if (str[i] == '.'){ count =0; line++; } } cout << "count =" << count << " v " << line << " predlojenie"; return 0; 

This is what I could write), but it counts only for the last sentence.

  • You bet - you reset the counter when you reach the point, without displaying information ... - Harry
  • @Dasha Novikova How is the word defined? Are its borders set only by spaces or also by punctuation? - Vlad from Moscow
  • @VladfromMoscow Well, I decided that with a space, because after the punctuation mark, a space follows vseravno - Dasha Novikova

3 answers 3

You have an incorrect algorithm. You count not the number of words, but the number of spaces in the sentence. In addition, the entered string may contain several sentences, and the last sentence may not end with a period. In this case, it is simply ignored. In addition, if the next sentence starts immediately after a period without an intermediate space, then again you will get the wrong result.

The program might look like this:

 #include <iostream> int main() { while (true) { const size_t N = 100; char s[N]; std::cout << "\nEnter a sentence (ENter - exit): "; if (!std::cin.getline(s, sizeof(s)) || s[0] == '\0') break; size_t n = 0; for (const char *p = s, *first; *p; ) { while ( *p == ' ' || *p == '\t' ) ++p; if (n == 0) first = p; ++n; while (*p && !(*p == ' ' || *p == '\t' || *p == '.')) ++p; if ( *p == '\0' || *p == '.') { while (*p == '.') ++p; std::cout << "\nThere are " << n << " words in the sentence \""; std::cout.write( first, p - first ) << "\".\n"; n = 0; std::cout << "\nPress a key to continue "; std::cin.get(); } } } return 0; } 

Approximate dialogue with the program:

 Enter a sentence (ENter - exit): One two. Three four five. There are 2 words in the sentence "One two.". Press a key to continue There are 3 words in the sentence "Three four five.". Press a key to continue Enter a sentence (ENter - exit): 

    Dasha, I fully agree with my colleagues above))) ... but there is one "but." Kohl you learn not just C, but C + + - start to learn correctly. A "right" is the use of all the features of the language. Including the standard library STL. Here is one of the solutions to the task:

     #include <iostream> #include <regex> // // Подсчет слов в предложениях // // вынес в using'и просто чтобы немного "разрядить" код, хотя обычно так не делаю using std::cout; using std::endl; using std::pair; using std::regex; using std::vector; using std::string; using std::smatch; using std::regex_match; typedef vector<pair<string,int>> ResType; void WordsCount(string T, ResType &Res) { smatch Matches; regex RegexpDot("^.*?(.+?)\\.\\s*(.*)$"); regex RegexpWord("^.*?([a-zA-Z]+)\\s*(.*)$"); vector<string> Lines; while(regex_match(T, Matches, RegexpDot)) { Lines.push_back(Matches[1]); T=Matches[2]; } for(const auto &i:Lines) { int N = 0; string Tmp = i; while(regex_match(Tmp, Matches, RegexpWord)) { Tmp=Matches[2]; N++; } Res.push_back({i,N}); } } int main() { try { ResType Res; string Text = "mama mila ramu. Rama vusmert' zadolbala mamu. 128 raz."; WordsCount(Text,Res); for(const auto &i:Res) cout << "\"" << i.first << "\": " << i.second << endl; } catch (std::regex_error& Err) { std::cout << "Засада: " << Err.what(); } return 0; } 

    Here you can see the result of the work. Good luck :)

    • Well, you are just a newbie with regulars on the head :) - VladD
    • Scared once, once figured out - then you can’t pull you by the ears))) I started with Perl so once, so I don’t speak from the ceiling) - Majestio
    • IMHO regulars - bust. Enough samopisnogo finite state machine. - pavel
    • well, then ++ should be removed from the label, let the "tse" remain without pluses - Majestio

    And if the last cycle to do so?

     for (i = 0; i < len; i++){ if (str[i] == ' '){ count++; } else if (str[i] == '.'){ cout << "count =" << ++count << " v " << line << " predlojenie"; count = 0; line++; } }