k&r ex1-18 removing trailing spaces.
ive been at this for a while. i just cant seem to get the logic down so if none of the below code makes sense or can be done a better way please suggest the way. =)
if this code is correct, how would i test to see if it actually removes trailing spaces.
Code:
#include<stdio.h>
#define MAXLINE 1000
int getline(char string[], int maxline);
main()
{
int c;
int i;
int j;
int len;
char s[MAXLINE];
for ( i = 0; i < MAXLINE - 1; ++i)
s[i] = c;
len = getline(s, MAXLINE);
j = len;
--j;
--j;
while (j == ' ')
--j;
s[j] == '\n';
++j;
s[j] == '\0';
printf("%s", s);
}
/*getline function returns len*/
int getline(char line[], int limit)
{
int c, i;
for (i = 0; i < limit-1 && (c = getchar()) != EOF && c != '\n'; ++i)
line[i] = c;
if (c = '\n') {
line[i] = c;
++i;
}
line[i] = '\0';
return i;
}
thanks in advance for the response.