Cannot find the error on my program ( verry basic C stuff )



Hi folks,

I'am learning C programming from ANSI_C Reference Manual of Kernighan & D.Ritchie .
I'm still working on the tutorial chapter , in have some issues with exercice 1-18 .

The goal is to write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines.

Here's my source code :

**********************

/* Remove trailing blanks and tabs from each line
** not worked yet on removing tabs
*/

#include <stdio.h>

#define MAXLINE 1000 /* max input line length */
#define IN 1
#define OUT 0

/* function prototypes */
int getline (char line[], int maxline);
void clean (char line[], char cleaned[]);

/* prints input lines after removing trailing blanks and tabs */
int main(void)
{
char line[MAXLINE]; /* current line */
char cleaned[MAXLINE]; /* cleaned line */

while (getline (line, MAXLINE)){
clean (line, cleaned);
printf ("\n%s", cleaned);
}
}


/* get new line into an char array */
int getline(char line[], int lim)
{
int c, i;

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

}

void clean(char line[], char cleaned[])
{
int space;
int i;

if (line[0] == ' ')
space == IN;
else
space == OUT;

for (i=0; line[i] != '\0'; ++i){
if (space == OUT)
if (line[i] != ' ')
cleaned[i] = line[i];
else if (line[i] == ' '){
space == IN;
cleaned[i] = line[i];
}
else if (space == IN)
if (line[i] != ' '){
cleaned[i] = line[i];
space == OUT;
}
else if (line[i] == ' ')
;
}
cleaned[i] = line[i];
}

**************************************


The output gives me 8 whatever the input was .
If somebody can help me to resolve this problem it would be great , i need just to find where's the error not to get the solution to this probleme as i have the solution book .

Thx in advance and sorry for my poor english

Chakib .B
.



Relevant Pages

  • Re: Portability: Harmony between PC and microcontroller
    ... int is the natural integer type for the system. ... You are, perhaps unintentionally, paraphrasing the standard in a way ... One of the things that you might not realize is that the C programming ... In the real world, most embedded systems have more complex jobs to do, ...
    (comp.lang.c)
  • Re: What will be the next MAJOR programming language for commercial use?
    ... Variants can be considered as such a common OO pattern that it is worth ... Int of int ... As lists are built-in, they can be decomposed in a pattern just as they are ... features found in other state-of-the-art programming languages. ...
    (comp.lang.misc)
  • Re: Bug/Gross InEfficiency in HeathFields fgetline program
    ... Maybe this is true in the sort of programming you do, ... you are storing a list of amounts of money as integers. ... int average ... the array, rounded towards zero. ...
    (comp.lang.c)
  • Re: beginner with programming, how to learn to debug and few C general questions
    ... I am first an accountant and decided to take on programming a few ... I have mainly been programming under Windows Xp DJGPP Dolorie... ... int main ... If you fix all of the above problems, it might fix whatever the issue ...
    (comp.lang.c)
  • Cannot find error on my program ( verry basic C stuff )
    ... I'am learning C programming from ANSI_C Reference Manual of Kernighan & D.Ritchie. ... The goal is to write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines. ... int getline; ... If somebody can help me to resolve this problem it would be great, i need just to find where's the error not to get the solution to this probleme as i have the solution book. ...
    (comp.lang.c)