Exercise 1-18. Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines.


I have no idea about this program .


1. first tell me the meaning and what to do in this question?

2. second provide me the source code if possible

The forums purpose is to provide assistance not complete solutions...Please post what you have completed so far and any detailed questions.

As for the meaning...

Since I'm not in your class I can only speculate about the absolute meaning of the question...

To me it seems you have to open a text file and read each line of input and display it without any trailing blanks or tabs and skip blank lines but like I said I'm not in your class so this is only speculation.

Use fgets(CharArray, sizeof(CharArray), YourFilePointer);

to put each line into the CharArray[80 or so], that you've previously declared.

Now you can use for or while loops, to "walk" through the CharArray[], and pick out the tabs, and spaces, etc.

When you find the line meets your needs, then put the end of string char: '\0', as the very last char. In C, this is NOT a string:

John

THIS is a string:

John'\0'

Even though you don't see the end of string char on your screen, it MUST be there - or it's just a bunch of letters, not a string, to C.

Spaces tabs and blanks will all have a value less than 'A', (hint, hint, hint).

:)

To remove blank lines, just ignore them. Don't do anything with them.

To remove trailing spaces: Set a pointer to the end of the character array, back up until the first non-white-space is found, then insert a string's null terminator at that point. For example, not that in the code below, isspace() is prototyped in ctype.h header file.

char line[] = "Hello World             "; // trailing spaces
char *ptr = line[ strlen(line)-1 ]; // start at end of line
// back up until either beginning of line is found
// or the first non-white-space character is found
while( ptr > line && isspace(*ptr) )
    ptr--;
// If the character at the current position is not
// a white-space character, then advance one 
// position.  If the character at the current position
//IS white-space, that means the entire line is just 
// white space, so make it a blank line.
if( !isspace(ptr) ) ptr++; // oops, went too far
*ptr = '\0'; // truncate trailing spaces

Edited 6 Years Ago by Ancient Dragon: n/a

Spaces tabs and blanks will all have a value less than 'A', (hint, hint, hint).

surely you're not suggesting that he blindly remove every char with a value of less than 'A'? because that would wipe out all the numbers and most of the commonly-used punctuation.

lionaneesh, dont make it harder than it needs to be:

to get rid of leading spaces, start at the beginning of the inputString, move through each character that you find a space ' ' (0x20). keep track of the index. once you finally find a character that's not a space, recopy the inputString starting at this location back over the input string like so: strcpy(inputString, &inputString[index]); as for the rest, do what A.D. says, above


.

Edited 6 Years Ago by jephthah: n/a

Actually, using strcpy() over the same buffer is not defined. See section 2.14.13 strcpy here. Also here and here.

You need to copy into a different buffer.

Edited 6 Years Ago by WaltP: n/a

Actually, using strcpy() over the same buffer is not defined. See section 2.14.13 strcpy here. Also here and here.

You need to copy into a different buffer.

Agree -- memmov() is used for that purpose.

This article has been dead for over six months. Start a new discussion instead.