2016-04-04 40 views
2

我被卡在一段我一直在工作的代碼上。我需要使用argc和argv []參數接收N個輸入。然後,N個輸入將允許用戶輸入那麼多句子。對於每一個句子,我的代碼都應該反轉句子中的每個單詞。目前,我的代碼將採用N值和句子,但不會打印相反的句子。相反,它會打印一個空行。在C編程中反轉數組

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <ctype.h> 
#define SIZE 80 

void get_input(char *line){ 
fgets(line, SIZE, stdin); 
char *ptr = strchr(line, '\n'); 
if (ptr){ 
    *ptr = '0'; } 
} 

void reverse(char *line){ 
char copy[SIZE]; 
char word[SIZE]; 
memset(copy, 0, SIZE); 
int line_len = strlen(line); 
int word_len = 0; 
int i; 
for(i=line_len; i<=0; --i){ 
    if(line[i] == ' ' && word_len > 0){ 
     memset(word, 0, SIZE); 
     strncpy(word, line + i + 1, word_len); 
     strcat(copy, word); 
     strcat(copy, " "); 
     word_len = 0; 
    }else if(isalnum(line[i]) || line[i] == '\'') 
      {word_len++;} 
    } 
    if(word_len>0){ 
    memset(word, 0, SIZE); 
    strncpy(word, line, word_len); 
    strcat(copy, word);} 
    strcpy(line, copy); 
} 

int main(int argc, char *argv[]){ 
int N = (int)strtol(argv[1], NULL, 10); 
if(N<0){ 
     printf("ERROR: Please provide an integer greater than or equal to 0\n"); 
     return 0; 
     } 
if(N>SIZE){ printf("ERROR: Please provide an integer less than or equal to 80\n"); 
      return 0; 
      } 
    char line[SIZE]; 
    int i; 
    for(i=0;i<N;i++){ 
         get_input(line); 
         reverse(line); 
         printf("%s\n", line); 
        } 
return 0; 
} 

示例輸入:

狐狸躍過日誌

實施例所需的輸出:

日誌上跳下狐狸的

電流輸出:

+2

「我的代碼做一切正常,但不打印反向句相反,它打印一個空行。」 - 因爲這是唯一的事情是應該做的,它確實*不*正確地做一切? – immibis

+0

在我的文章中進行了編輯 – Shaunbaum

回答

0

一個簡單的愚蠢的錯誤:

for(i=line_len; i<=0; --i){ 

這是什麼環呢?那麼,首先它將i設置爲line_len。然後它循環i小於或等於零,並且在每個循環結束時它將i減1。

如果line_len爲零,將會循環很長時間(直到i溢出),否則它將根本不會循環(因爲i將大於0)。

你可能意思是>=而不是<=

你也可能意思是i=line_len-1而不是i=line_len,但很難說。

+0

是的,這就是我一直想念這一切的時間謝謝 – Shaunbaum

0

EDITS:將for循環中的i < = 0更改爲i> = 0。

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <ctype.h> 
#define SIZE 80 

void get_input(char *line){ 
fgets(line, SIZE, stdin); 
char *ptr = strchr(line, '\n'); 
if (ptr){ 
    *ptr = ' '; } 
} 

void reverse(char *line){ 
char copy[SIZE]; 
char word[SIZE]; 
memset(copy, 0, SIZE); 
int line_len = strlen(line); 
int word_len = 0; 
int i; 
for(i=line_len; i>=0; --i){ 
    if(line[i] == ' ' && word_len > 0){ 
     memset(word, 0, SIZE); 
     strncpy(word, line + i + 1, word_len); 
     strcat(copy, word); 
     strcat(copy, " "); 
     word_len = 0; 
    }else if(isalnum(line[i]) || line[i] == '\'') 
      {word_len++;} 
} 
    if(word_len>0){ 
    memset(word, 0, SIZE); 
    strncpy(word, line, word_len); 
    strcat(copy, word);} 
    strcpy(line, copy); 
} 

int main(int argc, char *argv[]){ 
int N = (int)strtol(argv[1], NULL, 10); 
if(N<0){ 
     printf("ERROR: Please provide an integer greater than or equal to 0\n"); 
     return 0; 
     } 
if(N>SIZE){ printf("ERROR: Please provide an integer less than or equal to 80\n"); 
      return 0; 
      } 
    char line[SIZE]; 
    int i; 
    for(i=0;i<N;i++){ 
         get_input(line); 
         reverse(line); 
         printf("%s\n", line); 
        } 
return 0; 
} 
2

你把<=而不是>=

for(i = line_len; i <= 0; --i) { 
+1

'* = <'是什麼意思? – Qix

+0

x> = z:這意味着X高於或等於z。 x <= z:這意味着z高於或等於x。 – Bumblum

+0

好的,但是'* = <'不是操作符。 – Qix

0

在轉回很好的嘗試。但是,你可能會使它比需要的更困難。如果您只是簡單地顛倒參數的順序,您可以將它們反向打印出來,或者如果您想從反轉的參數中構建一個字符串,只需將它們按與strcat相反的順序連接即可。有很多方法可以做到這一點,但一個相當簡單的方法是:

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

#define MAXC 1024 

int main (int argc, char **argv) { 

    char string[MAXC] = ""; 
    int i, len = 0; 

    for (i = argc - 1; i && len + strlen (argv[i]) + 2 < MAXC; i--) { 
     strcat (string, argv[i]); 
     strcat (string, " "); 
     len = strlen (string); 
    } 
    printf ("%s\n", string); 

    return 0; 
} 

它只是最後一個參數指標argc - 1開始,然後,只要參數指標大於0i)和你是你允許的字符數len + strlen (argv[i]) + 2 < MAXC+2以允許NUL,終止' ')的範圍內,串聯的參數您的字符串strcat (string, argv[i]);並添加一個空格strcat (string, " ");

(你可以添加if (i > 1)之前strcat (string, " ");消除尾隨空間)。

示例使用/輸出

$ ./bin/str_revargs The fox jumped over a log 
log a over jumped fox The