Created
October 3, 2024 16:47
-
-
Save Darklega228/d68305f0fc07d95b79e8a64bdcf71ea9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <Windows.h> | |
using namespace std; | |
//Задание 1 | |
/*int main() | |
{ | |
setlocale(0, ""); | |
char str[256], ch; | |
int count = 0; | |
cout << "Введите строку: "; | |
cin.getline(str, 256); | |
cout << "Введите символ для поиска: "; | |
cin >> ch; | |
cout << "Индексы вхождений символа '" << ch << "': "; | |
for (int i = 0; i < strlen(str); i++) | |
{ | |
if (str[i] == ch) | |
{ | |
cout << i << " "; | |
count++; | |
} | |
} | |
cout << "\n" << "Количество совпадений: " << count << "\n"; | |
}*/ | |
//Задание 2 | |
/*bool isVowel(char ch) | |
{ | |
ch = tolower(ch); | |
return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'); | |
} | |
bool isConsonant(char ch) | |
{ | |
return isalpha(ch) && !isVowel(ch); | |
} | |
int main() | |
{ | |
setlocale(0, ""); | |
char str[256]; | |
int words = 0, vowels = 0, consonants = 0, punctuation = 0, digits = 0, others = 0; | |
cout << "Введите строку: "; | |
cin.getline(str, 256); | |
bool inWord = false; | |
for (int i = 0; i < strlen(str); i++) | |
{ | |
if (isspace(str[i])) | |
{ | |
inWord = false; | |
} | |
else { | |
if (!inWord) | |
{ | |
words++; | |
inWord = true; | |
} | |
if (isVowel(str[i])) vowels++; | |
else if (isConsonant(str[i])) consonants++; | |
else if (ispunct(str[i])) punctuation++; | |
else if (isdigit(str[i])) digits++; | |
else others++; | |
} | |
} | |
cout << "Всего символов в строке: " << strlen(str) << "\n"; | |
cout << "Слов: " << words << "\n"; | |
cout << "Гласных букв: " << vowels << "\n"; | |
cout << "Согласных букв: " << consonants << "\n"; | |
cout << "Знаков пунктуации: " << punctuation << "\n"; | |
cout << "Цифр: " << digits << "\n"; | |
cout << "Других символов: " << others << "\n"; | |
}*/ | |
// Задание 3 | |
/*int main() | |
{ | |
setlocale(0, ""); | |
char str[256]; | |
int wordCount = 0, charCount = 0; | |
cout << "Введите строку: "; | |
cin.getline(str, 256); | |
bool inWord = false; | |
for (int i = 0; i < strlen(str); i++) | |
{ | |
if (isspace(str[i])) | |
{ | |
inWord = false; | |
} | |
else | |
{ | |
if (!inWord) | |
{ | |
wordCount++; | |
inWord = true; | |
} | |
charCount++; | |
} | |
} | |
if (wordCount > 0) | |
{ | |
cout << "Средняя длина слова: " << (float)charCount / wordCount << "\n"; | |
} | |
else | |
{ | |
cout << "Нет слов в строке." << "\n"; | |
} | |
}*/ | |
// Задание 4 | |
/*bool isPalindrome(const char* str) | |
{ | |
int left = 0, right = strlen(str) - 1; | |
while (left < right) | |
{ | |
while (!isalpha(str[left]) && left < right) left++; | |
while (!isalpha(str[right]) && left < right) right--; | |
if (tolower(str[left]) != tolower(str[right])) | |
return false; | |
left++; | |
right--; | |
} | |
return true; | |
} | |
int main() | |
{ | |
setlocale(0, ""); | |
char str[256]; | |
cout << "Введите строку: "; | |
cin.getline(str, 256); | |
if (isPalindrome(str)) | |
cout << "Строка является палиндромом." << "\n"; | |
else | |
cout << "Строка не является палиндромом." << "\n"; | |
}*/ | |
// Задание 5 | |
void toLowerCase(char* str) | |
{ | |
for (int i = 0; i < strlen(str); i++) | |
{ | |
str[i] = tolower(str[i]); | |
} | |
} | |
int countOccurrences(const char* str, const char* word) | |
{ | |
int count = 0; | |
const char* p = strstr(str, word); | |
while (p) | |
{ | |
count++; | |
p = strstr(p + strlen(word), word); | |
} | |
return count; | |
} | |
int main() | |
{ | |
setlocale(0, ""); | |
char str[256], word[50]; | |
cout << "Введите строку: "; | |
cin.getline(str, 256); | |
cout << "Введите слово для поиска: "; | |
cin.getline(word, 50); | |
toLowerCase(str); | |
toLowerCase(word); | |
int occurrences = countOccurrences(str, word); | |
cout << "Слово '" << word << "' встречается " << occurrences << " раз(а)." << "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment