Загрузка данных


Задание 1
#include <iostream>
using namespace std;

int main()
{
    string punct = ".,!?:;";
    string str = "";
    string strTemp = "";
    
    cout<<"Введите строку:"<<"\n";
    getline(cin, str); 
    
    for (int i = str.length(); i > 0; i--)
    {
        for (int j = 0; j <= punct.length(); j++)
        {
            if (punct[j] == str[i]) 
            {
                strTemp = str[i] + strTemp;
            }
        }
    }
    
    
    if  (strTemp.length() <= 1)
    {
        cout<<"-1"<<"\n";
    }
    else
    {
        cout<<"Знаки препинания:"<<strTemp<<"\n";
    }
    
    return 0;
}


Задание 2
#include <iostream>
using namespace std;

int main()
{
    string str = "";
    string strTemp = "";
    
    cout<<"Введите строку:"<<"\n";
    getline(cin, str); 
    
    for (int i = str.length(); i >= 0; i--)
    {
        if (str[i] != ' ') 
        {
            strTemp = str[i] + strTemp;
        }
    }
    
    cout<<"Строка без пробелов:"<<strTemp<<"\n";
    
    return 0;
}


Задание 3
#include <iostream>
using namespace std;

int main()
{
    int count = 0;
    string str = "";
    
    cout<<"Введите строку:"<<"\n";
    getline(cin, str); 
    int number = str.length() - 1;

    while (number >= 0 && str[number] == ' ') 
    {
        number--;
    }

    while (number >= 0 && str[number] != ' ') 
    {
        if (isalpha(str[number]))
        {   
            count++;
        }
        number--;
    }
    
    cout<<"В полседнем слове "<<count<<" букв\n";
    
    return 0;
}




Задание 4
#include <iostream>
using namespace std;

int main()
{
    int count = 0;
    string str = "";
    string tempstr = "";
    
    cout<<"Введите строку:"<<"\n";
    getline(cin, str); 
    int number = 0;
    
    for (int i = 0; i <= str.length(); i++)
    {
        if (str[i] != ' ' && isalpha(str[i]))
        {
            tempstr = tempstr + str[i];
        }
        else
        {
            if (tempstr.length() > 0)
            {
                cout<<tempstr<<"\n";
            }
            tempstr = "";
        }
    }
    
    return 0;
}


Задание 5
#include <iostream>
#include <cctype>

using namespace std;

int main()
{
    int number = 0;
    int counter = 0;
    string str = "";
    string tempstr = "";
    string newstr = "";
    
    cout<<"Введите строку:"<<"\n";
    getline(cin, str); 
    cin>>number;
    
    for (int i = 0; i <= str.length(); i++)
    {
        if (str[i] != ' ' && isalpha(str[i]))
        {
            tempstr = tempstr + str[i];
        }
        else
        {
            if (tempstr.length() > 1)
            {
                counter = counter + 1;
                if (number == counter) 
                {
                    str.insert(i, tempstr);
                    break;
                }
            }
            tempstr = " ";
        }
    }
    cout<<str;
    
    return 0;
}


Задание 6
#include <iostream>
#include <string>

using namespace std;

int main() {
    string str;
    cout<<"Введите строку: \n";
    getline(cin, str);

    int totalWords = 0;
    string temp = "";

    for (int i = 0; i < str.length(); i++) 
    {
        if (str[i] != ' ') 
        {
            temp += str[i];
        } 
        else 
        {
            if (temp.length() > 0) 
            {
                totalWords++;
            }
            temp = "";
        }
    }
    if (temp.length() > 0) totalWords++;

    int currentWord = 0;
    temp = "";
    
    for (int i = 0; i < str.length(); i++) 
    {
        if (str[i] != ' ') 
        {
            temp += str[i];
        } 
        else 
        {
            if (!temp.empty()) 
            {
                currentWord++;

                if (currentWord == totalWords - 1) {
                    
                    char first = temp[0];
                    temp[0] = temp[temp.length() - 1];
                    temp[temp.length() - 1] = first;
                    
                    str.insert(i, " " + temp); 
                    str.erase(i - temp.length(), temp.length()); 
                    break; 
                }
                temp = "";
            }
        }
    }

    cout<<"Результат: "<<str;

    return 0;
}