2013-01-24 67 views
1

我雖與strlen()功能建立一個名副其實的len找到最後一個字符,打印出來,然後減1就不算在我的代碼工作:如何在C向後打印Word

#include <stdio.h> 
#include <string.h> 
#define SIZE 4 
int main(void) 

{ 
    int index; 
    char wordToPrint[SIZE]; 
    printf("please enter a random word:\n"); 
    for (index = 0; index < SIZE; index++) 
    { 
     scanf("%c", &wordToPrint[index]); 
    } 

    int len = strlen(wordToPrint); 
    for (index = 0; index < SIZE; index++) 
    { 
     printf("%c", wordToPrint[len]); 
     --len; 
    } 

    return 0; 
} 

輸入爲 「近紅外」

輸出爲:

?? 
r 

什麼是錯的,最後的程序?

tnx

+0

什麼是輸入和什麼是輸出生成..? –

+0

「它不工作」是你可以想出的最好的錯誤描述?! –

+0

@KerrekSB對不起,編輯後的I/O。 – MNY

回答

2

看看爲下面的代碼:

end = strlen(wordToPrint) - 1; 
for (x = end; x >= 0; --x) { 
    printf("%c", wordToPrint[x]); 
} 
+0

@SumitSungh謝謝你的分配,明白了:)你能告訴我爲什麼打印我也是'輸入'字符? 「?rin」 – MNY

5

C數組索引從零開始。

想想這是如何影響字符串長度與最後一個字符索引之間的關係的。

而且,你的字閱讀的方式是很奇怪的,它應該僅僅是:

scanf("%s", wordToPrint); 

,或者更好:

fgets(wordToPrint, sizeof wordToPrint, stdin); 

有沒有需要循環,並在讀取一個字符一次。以上將根據輸入的數量給你不同的長度。

第二個建議不會停留在空白處,所以如果你這樣做,你可能應該更換wordline爲清晰起見。

+0

謝謝!理解:) @unwid – MNY

2

反向迭代需要由一個偏移:

正向:

for (size_t i = 0; i != N; ++i) { putchar(word[i]); } 

向後:

for (size_t i = 0; i != N; ++i) { putchar(word[N - i - 1]); } 
//             ^^^^ 

這是因爲長度N的陣列具有有效索引in the range [0, N)

+0

謝謝,明白了。 – MNY

0

這段代碼是錯誤的:

int len = strlen(wordToPrint); 
for (index = 0; index < SIZE; index++) 
{ 
    printf("%c", wordToPrint[len]); 
    --len; 
} 

它的打印字符的4至1,但要打印字符從3比0。

你應該做的是這樣的:

for (index = len - 1; index >= 0; index--) 
    printf("%c", wordToPrint[index ]); 
1
int len = strlen(wordToPrint); 
for (index = 0; index < SIZE; index++) 
{ 
    printf("%c", wordToPrint[len]); 
    --len; 
} 

return 0; 

假設你有字奧托這樣 int len = strlen(wordToPrint);將設置len 4,但當你在printf("%c", wordToPrint[len]);中使用它時,你的數組越界越界wordToPrint[4]其中數組中的最後一個索引是wordToPrint[3],因爲它從0開始索引

0

除了其他答案,當你知道單詞有SIZE字符時爲什麼使用strlen()?

+0

我猜他的觀點是給出一個最大長度,所以「焦油」仍然是一個有效的輸入 – h4lc0n

+0

是的,但然後輸入循環也有問題。並且在字符串末尾沒有空字符來應用strlen()。 – anumi

+0

同意,已經回答了有關這個問題 – h4lc0n

2

除了其他海報說的話,你應該考慮給予和額外的字節字符串:

char wordToPrint[SIZE + 1]; 

,然後設置

wordToPrint[4] = '\0'; 

在這種情況下,有人輸入一個4字母詞(如'blue'),你的字符串看起來像{'b','l','u','e'}但沒有空字符。

Strlen和其他函數依賴於最後找到一個空值。

0

這裏是一個基於發佈一個工作代碼:

#include <stdio.h> 
#include <string.h> 
#define SIZE 4 
int main(void) 

{ 
int index; 
char wordToPrint[SIZE]; 
printf("please enter a random word:\n"); 
//Why you need a loop to read a word? 
//for (index = 0; index < SIZE; index++) 
//{ 
// scanf("%c", &wordToPrint[index]); 
//} 

scanf("%s",wordToPrint); 

int len = strlen(wordToPrint); 

//Modify the loop like this: 
for (index = len; index >= 0 ; index--) 
{ 
    printf("%c", wordToPrint[len]); 
} 
printf("\n");//Add this line for clearing the stdout buffer and a more readable output. 
return 0; 
} 

注:它始終是一個很好的做法,memset的()或bzero對()的陣列使用它之前。

1

編寫這樣的代碼,因爲在wordToPrint [長度],len爲+1,則wordToPrint []陣列的最後一個索引,以便其表示garbadge /空值

#include <stdio.h> 
#include <string.h> 
#define SIZE 4 
int main(void) 

{ 
    int index; 
    char wordToPrint[SIZE]; 
    printf("please enter a random word:\n"); 
    for (index = 0; index < SIZE; index++) 
    { 
     scanf("%c", &wordToPrint[index]); 
    } 

    int len = strlen(wordToPrint); 
    for (index = 0; index < SIZE; index++) 
    { 
     --len; 
     printf("%c", wordToPrint[len-1]); 
    } 

    return 0; 
} 
1

這是整個程序,爲你的問題。

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

char *strrev(char *); // Prototype 

/* The following function is by me, to reverse a string, because strrev is not available in GCC */ 

char *strrev(char *str) 
{ 
     char *p1, *p2; 

     if (! str || ! *str) 
      return str; 
     for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2) 
     { 
      *p1 ^= *p2; 
      *p2 ^= *p1; 
      *p1 ^= *p2; 
     } 
     return str; 
} 

/*================================================================Begin Main======================================================================================*/ 

int main() 
{ 
    char sentence[100], rev_sentence[100], c; 

    int j = 0, i = 0, m = 0; 

    sentence[i] = ' ';     // The first char in the sentence should be a space to reverse this first word 
    i++; 
    printf("Enter a sentence : "); 
    while((c = getchar()) != '\n') 
    { 
     sentence[i] = c; 
     i++; 
    } 
    sentence[i] = '\0'; 


    printf("Reversed word is: "); 


    for(i = strlen(sentence) - 1 ; i >= 0; i = i - 1) 
    { 
     if(sentence[i] == ' ') 
     { 
      rev_sentence[j] = '\0'; // because strrev fun reverses string ntil it encounters a first \0 character 
      strrev(rev_sentence); 
      printf("%s ", rev_sentence); 
      for(m = 0; m < 100; m++) 
      rev_sentence[m] = 0; 
      j = 0; 
      continue; 
     } 
     rev_sentence[j] = sentence[i]; 
     j++; 
    } 

    rev_sentence[j] = '\0'; 

} 

該程序反轉整個句子或單詞如果u只輸入1個字,然後按回車,希望你喜歡它,瞭解它

0

律」,黑客爲了好玩:)

#include <stdio.h> 
#include <stdlib.h> 

#define LENGTH 32 

int main(void) 
{ 
    // allocate a zero-filled buffer of LENGTH bytes 
    // we add LENGTH so that we start writing right *after* the buffer 
    char *word = LENGTH + calloc(LENGTH, 1); 

    // 0xFF will limit our buffer so that we do not write out of bounds 
    *(word - LENGTH) = 0xFF; 

    // order is important: we are decrementing the pointer and **then** 
    // reading a character from the standard input, we compare it to the 
    // line feed character so that we stop reading characters when return 
    // key is pressed 
    while ((*--word = fgetc(stdin)) != '\n' && *word != 0xFF); 
    printf("The word is: %s", word); 

    // tidy up! 
    free(word); 
    return 0; 
} 

當然,在預測時,不會在「現代」系統中工作,該系統實現了線緩衝控制檯,而不是允許用戶一次手動讀取一個字符(儘管可以使用平臺特定的API來實現。 )