如何将char阵列的中间单词更改为向后

How to change the middle word of a char array to backwards

本文关键字:单词更 中间 char 阵列      更新时间:2023-10-16

我已经尝试过,但是我可以向后打印中间字!

ex。输入=这个词向后;输出=此滴水向后

char chai[80];
int cont=0;
cout << "Enter odd string words to reverse the one of the middle: ";
cin.getline(chai,80);
for(int x=0; x< strlen(chai) ;++x)
{
    if(chai[x]==' ')
    ++cont;          
}

任何建议都将受到赞赏,

#include <iostream>
#include <string.h>
using namespace std;
 int main()
 {
    char chai[80];
    cout<<"introduce your phrase with an odd number of words"<<endl;
    cin.getline(chai,80);
    int cont=0;
    char c;
    cout<<endl;
    cout<<"Frase:"<<endl;
    for(int x=0;x<=strlen(chai)-1;++x)//count the words
    {
        if(chai[x]==' ')
        {
            ++cont;
            cout<<chai[x];//spaces will help to find the word in the middle
        }
        else
            cout<<chai[x];
    }
    cout<<endl;
    cout<<endl;
    if(cont%2!=0)
    {
        cout<<"It is necessary to introduce an odd number of words";
        return 0;
    }
    cout<<endl;
    cout<<endl;
    int aux=cont/2;
    int cont_dos=0;
    int cont_tres=0;
    cout<<"***It would be:***"<<endl;
    for(int i=0;i<=strlen(chai)-1;++i)
    {
        if(chai[i]==' ')
        {
            cont_dos++;
            if(cont_dos==aux)
            {
                for (int j=strlen(chai)-1;j!=0;--j)
                {
                    if(chai[j]==' ')
                    {
                        cont_tres++;
                        cout<<chai[j];
                    }
                    if(cont_tres==aux)
                    {
                        cout<<chai[j];
                        ++i;
                        if(cont_tres>aux)
                        {
                            ++i;
                            cout<<chai[i];
                            if(i==strlen(chai)-1)
                            {
                                return 0;
                            }
                        }
                    }
                    //if(cont_tres>aux)
                }
            }
            else
                cout<<chai[i];
        }
        else
            cout<<chai[i];
    }

    return 0;
}