0

I have to make a C program read ordinary text (a sequence of lines) from the program's standard input, and print it with each line reversed from left to right. It has to be no longer than 80 symbols and it should read until it gets an EOF. Im stumped now...here is what i have so far...

#include <stdio.h>
#include <stdlib.h>
#define MAXLEN 80


int getline(char *, int);


void main() {
char a[MAXLEN];
int i;
int temp;
int n;


getline(a, MAXLEN);


for (i=0; i<n/2; i++) {
temp = a;
a     = a[n-i-1];
a[n-i-1] = temp;
printf("%s", a);


system("Pause");


}


/* getline: read a line into s,  return length */


int getline(char *s, int lim)
{
int c, i;


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

Edited by happygeek: fixed formatting

3
Contributors
2
Replies
3
Views
12 Years
Discussion Span
Last Post by anastacia
0

loops can be traversed in either direction. Decrease your counter from the length of a string to 0 while printing.

That's all the hint you should need.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.