Originally Posted by
Salem
Perhaps your main should read
Code:
getline(line, MAXLINE);
reverseline(line);
printf("%s", line);
Don't cram unrelated functionality into getline.
Made two functions and almost solved the problem but there's a small snag. One of my inputs token gets skipped ! Why is it behaving like that?
Code:
#include <stdio.h>#include <stdlib.h>
#define MAXLINE 1000
void reverseLine(char[], int);
int getline(char[], int);
int main()
{
int len,c;
char line[MAXLINE];
while((c = getchar()) != EOF)
{
len = getline(line, MAXLINE);
reverseLine(line, len);
printf("%s\n", line);
}
return 0;
}
int getline(char s[], int maxlen)
{
int str, c;
for(str = 0; str < maxlen-1 && (c = getchar()) != EOF && c != '\n'; str++)
{
s[str] = c;
}
if(c == '\n')
{
s[str] = '\0';
}
return str;
}
void reverseLine(char s[], int len)
{
int i, temp;
for(i = 0; i < len/2; i++)
{
temp = s[i];
s[i] = s[len - i - 1];
s[len - i - 1] = temp;
}
}
Thanks !