Skip to content

Instantly share code, notes, and snippets.

@vlaleli
Created April 30, 2025 13:11
Show Gist options
  • Select an option

  • Save vlaleli/5430a7891d8ec72f211bb14ca4b1ee43 to your computer and use it in GitHub Desktop.

Select an option

Save vlaleli/5430a7891d8ec72f211bb14ca4b1ee43 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
// Порахувати кількість цифр у рядку
int countDigits(const char* str) {
int count = 0;
while (*str) {
if (isdigit(*str)) {
count++;
}
str++;
}
return count;
}
// Порахувати кількість входжень слова
int countWordOccurrences(const char* str, const char* word) {
int count = 0;
int wordLen = strlen(word);
const char* temp = str;
while ((temp = strstr(temp, word)) != nullptr) {
bool before = (temp == str) || !isalnum(*(temp - 1));
bool after = !isalnum(*(temp + wordLen));
if (before && after) {
count++;
}
temp += wordLen;
}
return count;
}
// Порахувати кількість речень
int countSentences(const char* str) {
int count = 0;
while (*str) {
if (*str == '.' || *str == '!' || *str == '?') {
count++;
}
str++;
}
return count;
}
// Порахувати кількість точок і ком
void countDotsAndCommas(const char* str, int& dots, int& commas) {
dots = 0;
commas = 0;
while (*str) {
if (*str == '.') {
dots++;
}
if (*str == ',') {
commas++;
}
str++;
}
}
// Перевернути весь текст
void reverseText(char* str) {
int len = strlen(str);
for (int i = 0; i < len / 2; ++i) {
swap(str[i], str[len - i - 1]);
}
}
int main() {
// Створення рядка у купі
const char* initialText = "Hello world! This is a test text. Does it work correctly? 1234.";
int length = strlen(initialText) + 1;
char* text = new char[length];
strcpy(text, initialText);
cout << "Text:\n" << text << "\n\n";
// Порахувати кількість цифр
cout << "Number of digits in the text:" << countDigits(text) << endl;
// Порахувати кількість входжень слова
char word[100];
cout << "\nEnter a search term:";
cin.getline(word, 100);
cout << "The number of occurrences of the word \"" << word << "\": " << countWordOccurrences(text, word) << endl;
// Порахувати кількість речень
cout << "\nNumber of sentences in the text:" << countSentences(text) << endl;
// Порахувати кількість точок і ком
int dots, commas;
countDotsAndCommas(text, dots, commas);
cout << "\nNumber of points:" << dots << endl;
cout << "Number of cells:" << commas << endl;
// Перевернути текст
reverseText(text);
cout << "\nInverted text:\n" << text << endl;
// Звільнення пам'яті
delete[] text;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment