Latest L6_T2
unknown
c_cpp
2 years ago
3.7 kB
9
Indexable
#include <iostream> #include <string> using namespace std; int StrLength(char*); bool IsDigit(char); bool IsConsonant(char); bool IsDividor(char); void StrReverse(char*); void StrComaLeftShift(char*); void StrDigitRightShift(char*); void StrWordReverse(char*); int main() { const int STR_LEN = 40; char* str = new char[STR_LEN]; cout << "Input string: " << endl; cin.getline(str, STR_LEN); StrComaLeftShift(str); cout << endl << "Coma shifted: "<<str << endl; StrDigitRightShift(str); cout << endl << "first digit group right shifted: "<<str << endl; StrWordReverse(str); cout << endl << "second word(starting from last) reversed: "<< str << endl; return 0; } int StrLength(char *str) { int len; for (len = 0; str[len] != '\0'; len++); return len; } void StrComaLeftShift(char *str) { int len = StrLength(str); int firstComaIndex = -1, lastComaIndex = 0; //find coma indiсes for(int i = 0; str[i] != '\0'; i++) { if (str[i] == ',') { if (firstComaIndex == -1) firstComaIndex = i; lastComaIndex = i; } } //shift for (int i = firstComaIndex; i < lastComaIndex; i++) { for(int j = firstComaIndex; j < len; j++) { str[j] = str[j + 1]; } } } void StrDigitRightShift(char *str) { bool isFirstDigit = true, isLastDigit = false; int firstDigitIndex, lastDigitIndex; //find substring location for(int i = 0; str[i] != '\0'; i++) { if(IsDigit(str[i])) { if(isFirstDigit) { firstDigitIndex = i; isFirstDigit = false; isLastDigit = true; } if(isLastDigit) { lastDigitIndex = i; } } else { isLastDigit = false; } } //shift the substring char temp = str[lastDigitIndex]; for(int i = lastDigitIndex; i <= lastDigitIndex && i > firstDigitIndex; i--) { str[i] = str[i - 1]; } str[firstDigitIndex] = temp; } void StrWordReverse(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]) && IsConsonant(str[i + 1])) { dividorCount++; } if (dividorCount == 2 && 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 IsDigit(char c) { const char DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; for(int i = 0; DIGITS[i] != '\0'; i++) { if(c == DIGITS[i]) return true; } return false; } 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 IsDividor(char c) { char dividors[] = { ' ', '.', ',', '/', ':', ';', '?', '!' }; for (int i = 0; dividors[i] != '\0'; i++) { if (c == dividors[i]) return true; } return false; }
Editor is loading...