C
Chakib Benziane
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 = c;
if (c == '\n'){
line = c;
++i;
line = '\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 != '\0'; ++i){
if (space == OUT)
if (line != ' ')
cleaned = line;
else if (line == ' '){
space == IN;
cleaned = line;
}
else if (space == IN)
if (line != ' '){
cleaned = line;
space == OUT;
}
else if (line == ' ')
;
}
cleaned = line;
}
/*************************************/
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
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 = c;
if (c == '\n'){
line = c;
++i;
line = '\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 != '\0'; ++i){
if (space == OUT)
if (line != ' ')
cleaned = line;
else if (line == ' '){
space == IN;
cleaned = line;
}
else if (space == IN)
if (line != ' '){
cleaned = line;
space == OUT;
}
else if (line == ' ')
;
}
cleaned = line;
}
/*************************************/
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