k&r ex1-18 removing trailing spaces.

This is a discussion on k&r ex1-18 removing trailing spaces. within the C Programming forums, part of the General Programming Boards category; ive been at this for a while. i just cant seem to get the logic down so if none of ...

  1. #1
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63

    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.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char test[] "does this work?       ";
    
    function( test );
    printf("\'%s\'", test );
    That's how you test it. Simply surround the would-be-output by quotes, or put some other item past what would be the end, and see if it ends up displaying correctly.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with file
    By nevrax in forum C Programming
    Replies: 12
    Last Post: 04-23-2007, 06:18 PM
  2. saving a CString to binary file with trailing spaces
    By nineofhearts in forum C++ Programming
    Replies: 12
    Last Post: 01-03-2006, 10:53 AM
  3. Removing Leading & Trailing Whitespace
    By EvilGuru in forum C Programming
    Replies: 11
    Last Post: 12-01-2005, 01:59 PM
  4. Removing spaces
    By chris1985 in forum C Programming
    Replies: 3
    Last Post: 12-22-2004, 08:23 AM
  5. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21