Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cstring>
- using namespace std;
- int este_litera(char c) {
- if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
- return 1;
- return 0;
- }
- int numar_cuvinte(char sir[100]) {
- int n = strlen(sir);
- int am_cuvant = 0; // nu avem niciun cuvant deoarece suntem la inceputul
- //sirului
- int numar_cuvinte = 0;
- for (int i = 0; i < n; ++i) {
- if (este_litera(sir[i]))
- am_cuvant = 1;
- else if (am_cuvant == 1) {
- // s-a terminat cuvantul
- ++numar_cuvinte;
- am_cuvant = 0;
- }
- }
- if (am_cuvant == 1) // cazul in care sirul se termina cu o litera
- ++numar_cuvinte;
- return numar_cuvinte;
- }
- int main() {
- ifstream fin("date.in");
- char sir[100];
- fin.getline(sir, 100);
- cout<<numar_cuvinte(sir);
- }
- /*
- int getWordsNr(char *s)
- {
- char c;
- int nr = 0;
- char lastChar = 0;
- for (int i = 0; s[i]; i++)
- {
- c = s[i];
- if (lastChar)
- {
- if (!isalpha(c) && isalpha(lastChar))
- nr++;
- }
- lastChar = c;
- }
- return nr;
- } */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement