Created
August 2, 2013 21:09
-
-
Save meikj/6143497 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define BUF_SIZE 256 | |
void reverse(char dst[], char src[], int len); | |
int strend(char string[], int len); | |
int getline(char line[], int maxline); | |
int main(void) { | |
int len; | |
char line[BUF_SIZE], rev[BUF_SIZE]; | |
while ( (len = getline(line, BUF_SIZE)) > 0 ) { | |
if (line[0] == '\n') { | |
printf("\n"); | |
continue; | |
} | |
reverse(rev, line, BUF_SIZE); | |
printf("%s", rev); | |
} | |
return 0; | |
} | |
void reverse(char dst[], char src[], int len) { | |
int i, j, end; | |
end = strend(src, len); | |
if (src[end] == '\n') | |
dst[end] = src[end]; | |
for (i = 0, j = (end - 1); j >= 0; i++, j--) { | |
dst[i] = src[j]; | |
} | |
dst[++i] = '\0'; | |
} | |
int strend(char string[], int len) { | |
int i; | |
for (i = 0; i < len; ++i) { | |
if (string[i] == '\0') | |
return (i-1); // -1 for "human" end of string (before '\0') | |
} | |
return 0; | |
} | |
int getline(char line[], int maxline) { | |
int c, i; | |
for (i = 0; i < (maxline-1) && (c = getchar()) != EOF && c != '\n'; ++i) | |
line[i] = c; | |
if (c == '\n') { | |
line[i] = '\n'; | |
++i; | |
} | |
line[i] = '\0'; | |
return i; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment