Jump to content

C programming [solved]


Recommended Posts

Hi guys,

 

I'm trying to learn C from K&R and I'm having trouble with Exercise 1-18.

 

#include <stdio.h>
#define MAXLINE 1000 /* max line length */

int getline(char s[], int maxline);
void remspac(char t[], int len);

/* Remove trailing whitespace from lines & remove blank lines */
main()
{
 int len; /* line length */
 char line[MAXLINE];

 while ((len = getline(line, MAXLINE)) > 0)
;
 remspac(line, len);

 return 0;
}

/* getline: reads a line into s, and returns the length (i) */
int getline(char s[], int lim)
{
 int i, c;
 for (i = 0; i < lim - 1 && (c=getchar()) != EOF && c!='\n'; ++i)
   s[i] = c;
 if (c == '\n') {
   s[i] = c;
   ++i;
 }
 s[i] = '\0';
 if (s[i-2] == ' ')
   printf("Trailing space\n");

 return i;
}

void remspac(char t[], int len)
{
   printf("%s", t);
}

 

The problem is that my character array "line" does not seem to be passed to the function remspac correctly - remspac (which will do more later) doesn't print anything!! Anyone any ideas what I'm doing wrong? I know pointers are an important concept in C, but they haven't been covered in the book so far so I don't want to use them.

 

Thanks in advance,

 

Grant

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...