fork download
  1. #include <iostream>
  2. #include <string>
  3. //Дана строк S, вывести количество слов начинающихся 'W'//
  4. int main()
  5. {
  6. using namespace std;
  7. setlocale(LC_ALL, "Russian");
  8. string S;
  9. cout << "Введите строку: ";
  10. getline(cin, S);
  11. system("cls");
  12. cout << "Вы ввели строку: \n";
  13. cout << S << endl;
  14. int i = 0, count = 0;
  15. char last = S[S.size() - 1];
  16. while (i < S.size())
  17. {
  18. int q = 1;
  19. if ((S[i] == ' ' && S[i + 1] == 'W') || (S[i] == 'W' && S[i+1] != ' '))
  20. {
  21. while (S[i + q] != ' ')
  22. {
  23. q++;
  24. if ((i + q) > S.size())
  25. {
  26. break;
  27. }
  28. }
  29. if (S[i + q - 1] == 'W')
  30. {
  31. count++;
  32. }
  33. if (last == 'W')
  34. {
  35. count++;
  36. break;
  37. }
  38. }
  39. i++;
  40. }
  41.  
  42. cout << "Кол-во слов на букву W: " << count << endl;
  43. //system("pause");
  44. return 0;
  45. }
  46.  
Success #stdin #stdout #stderr 0s 5004KB
stdin
W WWW and WWW
stdout
Введите строку: Вы ввели строку: 
W WWW and WWW
Кол-во слов на букву W: 2
stderr
sh: cls: not found