2017-03-04 1 views
-5

시도했지만 중간 단어를 뒤로 인쇄 할 수 없습니다!문자 배열의 중간 단어를 뒤로 바꾸는 방법

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;   
} 

후방이 드로우,

+0

는 예를 들어 줄 수 있는가? –

+0

예. 입력 =이 단어를 뒤로; output = this는 뒤로 물러나십시오 –

+0

자바에서 공간을 구분 기호로 사용하여'split()'메소드를 사용할 수 있습니다. 그런 다음 배열의 중간에 액세스하여 역순으로하고 새로운 내용으로 문자열을 다시 빌드하십시오^_ ^ –

답변

0
#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; 
} 
관련 문제