YANA FINAL AIP L6_T2
unknown
c_cpp
2 years ago
4.2 kB
7
Indexable
#include <iostream> using namespace std; int StrLength(char* str); void StrShiftLeft(char*, int, int); void StrShiftRight(char*, int, int); void DeleteAllSemicolons(char*); void StrWordShiftRight(char*); void StrReverseWord(char*); bool IsVowel(char); bool IsConsonant(char); bool IsDividor(char); int main() { const int STR_LEN = 60; char* str = new char[STR_LEN]; cout << "Input string: " << endl; cin.getline(str, STR_LEN); cout << str << endl; DeleteAllSemicolons(str); cout << "строка после удаления точек с запятой: " << str << endl; StrWordShiftRight(str); cout << "строка после сдвига третьего слова, начинающегося на согласную букву на один индекс вправо: " << str << endl; StrReverseWord(str); cout << endl << "строка после отзеркаливания четвёртого с конца слова начинающегося на гласную букву: " << str << endl; return 0; } int StrLength(char* str) { int len; for (len = 0; str[len] != '\0'; len++); return len; } void StrShiftLeft(char* str, int firstIndex, int lastIndex) { for (int i = firstIndex; i < lastIndex; i++) { str[i] = str[i + 1]; } } void StrShiftRight(char* str, int firstIndex, int lastIndex) { char temp = str[lastIndex]; for (int i = lastIndex; i <= lastIndex && i > firstIndex; i--) { str[i] = str[i - 1]; } str[firstIndex] = temp; } bool IsDividor(char c) { char dividors[] = { ' ', '.', ',', '/', ':', ';', '?', '!' }; for (int i = 0; dividors[i] != '\0'; i++) { if (c == dividors[i]) return true; } return false; } void DeleteAllSemicolons(char* str) { for (int i = 0; str[i] != '\0'; i++) { if (str[i] == ';') { StrShiftLeft(str, i, StrLength(str)); } } } void StrWordShiftRight(char* str) { bool insideWord = false; int wordCount = 0; int firstLetterIndex = -1, lastLetterIndex; if (IsConsonant(str[0])) wordCount = 1; for (int i = 0; str[i] != '\0'; i++) { if (IsDividor(str[i])) { insideWord = false; } if (IsDividor(str[i]) && IsConsonant(str[i + 1])) { insideWord = true; wordCount++; } if (insideWord && firstLetterIndex == -1 && wordCount == 3) { firstLetterIndex = i + 1; } if (insideWord && wordCount == 3) { lastLetterIndex = i; } } StrShiftRight(str, firstLetterIndex, lastLetterIndex); } void StrReverseWord(char* str) { int len = StrLength(str); bool insideWord = true; int dividorCount = 0; int firstLetterIndex = 0, lastLetterIndex; for(int i = len - 1; i > 0; i--) { if (IsDividor(str[i]) && IsVowel(str[i + 1])) { dividorCount++; //cout << i << endl; //cout << dividorCount << endl; } if (dividorCount == 4 && firstLetterIndex == 0) { firstLetterIndex = i + 1; break; } } for (int i = firstLetterIndex; str[i] != '\0'; i++) { if (IsDividor(str[i])) insideWord = false; if (insideWord) lastLetterIndex = i; } char temp; for (; firstLetterIndex < lastLetterIndex; firstLetterIndex++, lastLetterIndex--) { temp = str[firstLetterIndex]; str[firstLetterIndex] = str[lastLetterIndex]; str[lastLetterIndex] = temp; } } bool IsConsonant(char c) { const char CONSONANTS[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z' }; for (int i = 0; CONSONANTS[i] != '\0'; i++) { if (c == CONSONANTS[i]) return true; } return false; } bool IsVowel(char c) { const char VOWELS[] = { 'a', 'e', 'i', 'o', 'u', 'y' }; for (int i = 0; VOWELS[i] != '\0'; i++) { if (c == VOWELS[i]) return true; } return false; }
Editor is loading...