L6_T2

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
3.5 kB
3
Indexable
Never
#include <iostream>
#include <string>
using namespace std;


int StrLength(char*);

bool IsDigit(char);

void StrReverse(char*);
void StrComaLeftShift(char*);
void StrDigitRightShift(char*);
void StrWordReverse(char*);

int main() {
	const int STR_LEN = 25;

	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 isLastSecondWordIndex = true;
    bool isInTheSecondWord = false;
    
    int firstLetterIndex, lastLetterIndex;
    
    //find word location
    for(int i = len - 2; i > 0; i--)
    {
        if(str[i] == ' ')
        {
            if(isInTheSecondWord)
                isInTheSecondWord = false;
                
            if(isLastSecondWordIndex)
            {
               lastLetterIndex = i - 1;
               isLastSecondWordIndex = false;
               isInTheSecondWord = true;
            }
        }
        if(isInTheSecondWord)
        {
            firstLetterIndex = i;
        }
    }
    //reverse the word
    len = StrLength(str);
    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;
}