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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...