分配: 寫一個函數反方向(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 []數組,但它不工作只在主要我做的字符串處理。
問題在哪裏?
我看不到函數'reverse()',的確沒有'reverses()'。 – EOF
'line [len-j]'是'j == 0'結尾的空字符,所以你的反轉字符串是空的。你可能需要'j