char * word = NULL;
int count = 0;
word = strtok (str, " ,.");
while (word)
{
count++;
word = strtok (NULL, " ,.");
}
количество разделителей count — 1, если всё, что находится между словами считать разделителем.
Следующий вариант программы
#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;
int main(int argc, char* argv[])
{
int count = 0;
char str[257] = {'\0'};
char *buf = NULL;
cout << "Vvedite stroku: ";
cin.getline(str, 256);
cin.sync();
for (unsigned int i = 0; i < strlen(str); i++)
if (ispunct(str[i]))
count++;
buf = strtok(str, " ,.:!?");
while (buf)
{
count++;
buf = strtok(NULL, " ,.:!?");
}
cout << "Slojnost' = " << count << endl;
system("pause");
return 0;
}
Результат работы программы
