K&R C Bible Exercise1-19

/************************************************************* /* 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. **********************************************************/ #include "stdafx.h" #include <stdio.h> #define MAX_LINE 1024 void discardnewline(char s[]); int reverse(char s[]); int getline(char s[], int lim); int main(int argc, char* argv[]) { char line[MAX_LINE]; while(getline(line,sizeof line) > 0)//sizeof line? { discardnewline(line); reverse(line); printf("%s/n", line); } return 0; } void discardnewline(char s[]) { int i; for(i = 0; s[i] != '/0'; i++) if(s[i] == '/n') s[i] = '/0'; } int reverse(char s[]) { char ch; int i, j; for(j = 0; s[j] != '/0'; j++) { } --j; for(i = 0; i < j; i++) { ch = s[i]; s[i] = s[j]; s[j] = ch; --j; } return 0; } int getline(char s[], int lim) { int c, i; for(i =0; i < lim -1 && (c = getchar()) != '/n'; ++i) { s[i] = c; } if(c == '/n') s[i++] = c; s[i] = '/0'; return i; }

你可能感兴趣的:(K&R C Bible Exercise1-19)