محتوى الدرس
#include <iostream>
using namespace std;
/*
Count Words In String
"Hello Elzero Web School" -> 4
"Hello#Elzero#Web#School", '#' -> 4
*/
int word_count(string st, char sep = ' ')
{
int count = 0;
bool inWord = false;
for (int i = 0; i < st.length(); i++)
{
if (st[i] == sep)
{
if (inWord)
{
count++;
inWord = false; // Reset Flag
}
}
else
{
inWord = true; // Set Flag That Iam In A Word
}
}
if (inWord)
{
count++;
}
return count;
}
int main()
{
cout << word_count("Hello Elzero Web School ") << endl; // Should output 4
cout << word_count("Hello#Elzero#Web#School ", '#') << endl; // Should output 4
cout << word_count("Hello Elzero Web School") << endl; // Should output 4
cout << word_count("Hello Elzero Web School") << endl; // Should output 4
return 0;
}