Well I was solving the K&R problem - Write a function reverse(s) that reverses the character string s . Use it to write a program that reverses its input a line at a time. I got the first part correct but when I tried to modify to accommodate the second part the results aren't as intended. I know I screwed up somewhere in the array index calculation, but can't make it right ! Please help ! Here's my code so far:
Code:#include <stdio.h> #define MAXLINE 1000 void getline(char[], int); int main() { char line[MAXLINE]; getline(line, MAXLINE); printf("%s", line); return 0; } void getline(char s[], int maxlen) { int arrayLength, c, temp, i, preLen = 0; for(arrayLength = 0; arrayLength < maxlen-1 && (c = getchar()) != EOF; arrayLength++) { if(c == '\n') { s[arrayLength] = '\n'; for(i = preLen; i < (arrayLength+preLen)/2; i++) { temp = s[i]; s[i] = s[arrayLength - i - 1]; s[arrayLength - i - 1] = temp; } preLen += arrayLength; } else { s[arrayLength] = c; } } s[arrayLength--] = '\0'; }



2Likes
LinkBack URL
About LinkBacks




.