2016-02-15 47 views
-1

分配: 寫一個函數反方向(S)這尊字符串s.Use它寫在某時刻尊其輸入線K&RC編程練習1-19

這裏是一個程序我試圖在解決它:

#include <stdio.h> 
#define MAXLINE 1000 

int getlinex(char line[], int maxline); 

int main(){ 
    int len, j; 
    char line[MAXLINE]; 
    char *temp; 
    while ((len = getlinex(line, MAXLINE)) > 0) { 
     for(j=0; j<=len; j++){ 
      temp = &line[len-j]; 
      line[j] = *temp ; 
     } 
     printf("%s", line); 
    } 
} 

int getlinex(char s[], int lim) { 
    int c, i; 
    for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++) { 
     s[i] = c; 
    } 
    if (c == '\n') { 
     s[i] = c; 
     i++; 
    } 
    s[i] = '\0'; 
    return i; 
} 

第二個版本

int getlinex(char line[], int maxline); 

int main(){ 
    int len,j; 
    char line[MAXLINE]; 
    while ((len = getlinex(line, MAXLINE)) > 0) { 
     for(j=0;j<=len;j++){ 
      reverse(&line,j,len); 
     } 
     printf("%s",line); 
    } 
} 

void reverse(char *s[], int i, int len){ 
    s[i] = s[len-i]; 
} 

int getlinex(char s[], int lim) { 
    int c, i; 
    for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++) { 
     s[i] = c; 
    } 
    if (c == '\n') { 
     s[i] = c; 
     i++; 
    } 
    s[i] = '\0'; 
    return i; 
} 

當我輸入字符的字符串輸出爲空。

我已經嘗試返回一個char []數組,但它不工作只在主要我做的字符串處理。

問題在哪裏?

+1

我看不到函數'reverse()',的確沒有'reverses()'。 – EOF

+0

'line [len-j]'是'j == 0'結尾的空字符,所以你的反轉字符串是空的。你可能需要'j

回答

0

由於它是寫在作業中,你必須寫一個相應的功能。它可以按照演示程序中顯示的以下方式進行查看。

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

char * reverse(char *s) 
{ 
    size_t n = strlen(s); 

    for (size_t i = 0; i < n/2; i++) 
    { 
     char c = s[n-i-1]; 
     s[n-i-1] = s[i]; 
     s[i] = c; 
    } 

    return s; 
} 

int main(void) 
{ 
    char s[] = "Hello, World!"; 

    puts(s); 
    puts(reverse(s)); 
}  

它的輸出是

Hello, World! 
!dlroW ,olleH 

至於循環在你的程序

  for(j=0;j<=len;j++){ 
      temp = &line[len-j]; 
      line[j] = *temp ; 
      } 

那麼它是沒有意義的。例如,當j等於0時,循環體正好覆蓋line[0],存儲在&line[len]的字符等於字符串的終止零。您需要將頭部中的字符與字符串尾部的字符交換。

考慮到函數getlinex在字符串中包含新的行字符。

if (c == '\n') { 
    s[i] = c; 
    i++; 
    } 

也許你應該將它從字符串中刪除,然後再將其倒轉。

例如

if (line[len-1] == '\n') line[len-1] = '\0'; 

或者僅僅從以上功能getlinexif聲明所示刪除。

0

考慮這個循環:

for(j=0;j<=len;j++){ 
    temp = &line[len-j]; 
    line[j] = *temp; 
} 

的第一次通過,j0,所以temp設置爲&line[len]。由於這指向終止\0字節line,*temp\0。然後將其分配給line[j],即line[0]。之後的內容並不重要,因爲line的第一個字節終止了字符串。

這解釋了爲什麼你看到一個空字符串作爲結果。但即使這個問題得到解決,字符串也會被原地翻轉,所以一旦它到達字符串的中間,剩下的字符將被覆蓋並且不再可用。解決方法是交換字符對,而不是僅將一個字符複製到另一個字符。

0

以下是我在通過KnR練習時編寫的代碼。

#include <stdio.h> 

/* exercise 1.19 */ 

#define MAXLEN 1000 

int getline(char s[], int len); 
void reverse(char s[]); 

int 
main(){ 
    int len; 
    char line[MAXLEN]; 

    while ((len=getline(line,MAXLEN))){ 
     reverse(line); 
     printf("%d - %s",len, line); 
    } 

    return 0; 
} 

/* 
* Reads a line from stdin 
*/ 
int getline(char s[], int len){ 
    int c, i; 

    for (i = 0; i < len -1 && (c=getchar()) != EOF && c != '\n'; ++i) 
    s[i] = c; 

    if (c == '\n'){ 
     s[i] = '\n'; 
     ++i; 
    } 

    s[i]='\0'; 
    return i; 
} 


/* 
* Reverses the order of chracters in string s 
*/ 
void reverse(char s[]){ 
    int i, k; 
    char tmp[MAXLEN]; 

    i = 0; 
    /* copy the character string */ 
    while ((tmp[i] = s[i]) != '\0') 
     ++i; 

    --i; /* last character is nil */ 

    for (k = 0; k <= i ; ++k) 
     s[k] = tmp[i-k]; 

    s[k] = '\0'; 
}