2017-11-28 12 views
-5

ポインタを使って逆順にいくつかの単語を出力する必要があります。逆順に単語を印刷できません

たとえば、「He​​llo World」と指定すると、出力は「World Hello」になります。

これは私がこれまでにやっていることですが、それは何も印刷されません。

void swap(char *ar){ 

    int tmp1[10], tmp2[10], j = 0, k = 0, a, b, z,i; // must use pointers 

    for (i=0; i<MAX; i++) 
    { 
     if(ar[i] == ' ') //if there's a space, store the characters before the space to another array and after the space to another 
     { 
      for(j = 0; j<i; j++) // tmp1 
       tmp1[j] = ar[j]; 

      for(j = 0; j<MAX-i; j++) //MAX - i would be range of j 
       for(k = i+1; k<MAX; k++) //tmp2 
        tmp2[j] = ar[k]; 
     } 
    } 
    i = 0; 
    while(1) 
    { 
     while(tmp2[i] != '\0' && i<sizeof(tmp2)) // finding the size of tmp2 
      a++; 
    } 
    for(j=0; j<a; j++) // overwriting the original array 
     ar[j] = tmp2[j]; 
    j++; // incrementing j so that there would be a space between the new array 
    z = j; 
    i=0; 

    while(1) //finding the size of tmp1 
    { 
     while(tmp1[i] != '\0' && i<sizeof(tmp1)) 
      b++; 
    } 
    //idk pls help 
    for(z=0 ; z<MAX; z++) //overwriting the original array 
     for(k = 0; k<b; k++) 
      ar[z] = tmp1[k]; 
     //idk pls help 

    for (i = 0; i < MAX; i++) // outputting the required result 
     printf("%c", *(ar+i)); 
    printf("\n"); 
}//eg if input is Hello World the output should be World Hello//idk pls help 

主な機能はword1私はあなたの問題があると思いchar word1[] = {'h','e', etc)

+0

あなたが直面している問題は何ですか? – Dalton

+2

あなたの問題は 'while(1)'ループ... "無限ループ"だと思います。 – Dalton

+0

何も印刷されていません –

答えて

0

あるswap(word1)としての機能を呼び出しますwhile (1)ループ...

無限ループ...

私はこのことを願って助けて。

+0

hmmmだから私は代わりに何をすべきですか? –

+0

私はあなたの問題になるかもしれないことを教えています...あなたが学んでいる間に、もう少し勉強して、別の解決策を試してみる必要があります。私が何をすべきかを言うなら、あなたはよく学ばないでしょう。 – Dalton

0

strtokを使用して文字列を分割し、最初の単語の前に2番目の単語を印刷するのはなぜですか?あなたが今やっていることは非常に不安定で、6ループと10システムコール、1ループと1システムコールを行うことができます!あなたが望むなら、あなたにコードを書くことができます、私に知らせてください!

void swap(char *ar){ 
    int i, z; 
    for(z = 0; ar[z]; z++) //find the index of the space 
     { 
     if (ar[z] = ' ') 
      break; 
     } 
    for(i = z + 1; ar[i]; i++) 
     printf("%c",ar[i]); //print from space to end of string 
    printf(" "); 
    for(i = 0; i < z; i++) 
     printf("%c",ar[i]); //print from start of string to space 
    printf("\n"); 
    } 
+0

クラスで学んだことがないので、strtokを使うことはできません。 –

+1

別の配列にも格納する必要がありますか?なぜ今すぐ印刷しないのですか?ループ条件をMAXで使用しないでください。ループを停止する代わりにar [i]!= Nullを使用してください。 – MinasA1

+0

別の配列に格納する必要はありません...あなたは私のために書くことができますか? –

関連する問題