Advertisement
STANAANDREY

nr cuv

Aug 28th, 2019
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. int este_litera(char c) {
  7.   if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
  8.     return 1;
  9.   return 0;
  10. }
  11.  
  12. int numar_cuvinte(char sir[100]) {
  13.   int n = strlen(sir);
  14.   int am_cuvant = 0; // nu avem niciun cuvant deoarece suntem la inceputul
  15.   //sirului
  16.   int numar_cuvinte = 0;
  17.   for (int i = 0; i < n; ++i) {
  18.     if (este_litera(sir[i]))
  19.       am_cuvant = 1;
  20.     else if (am_cuvant == 1) {
  21.       // s-a terminat cuvantul
  22.       ++numar_cuvinte;
  23.       am_cuvant = 0;
  24.     }
  25.   }
  26.   if (am_cuvant == 1) // cazul in care sirul se termina cu o litera
  27.     ++numar_cuvinte;
  28.   return numar_cuvinte;
  29. }
  30.  
  31. int main() {
  32.   ifstream fin("date.in");
  33.   char sir[100];
  34.   fin.getline(sir, 100);
  35.   cout<<numar_cuvinte(sir);
  36. }
  37.  
  38. /*
  39. int getWordsNr(char *s)
  40. {
  41.     char c;
  42.     int nr = 0;
  43.     char lastChar = 0;
  44.     for (int i = 0; s[i]; i++)
  45.     {
  46.         c = s[i];
  47.         if (lastChar)
  48.         {
  49.             if (!isalpha(c) && isalpha(lastChar))
  50.                 nr++;
  51.         }
  52.         lastChar = c;
  53.     }
  54.     return nr;
  55. } */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement