How can the isspace function be used to remove excess white space?

  • Thread starter Thread starter magnifik
  • Start date Start date
  • Tags Tags
    Function
Click For Summary

Discussion Overview

The discussion revolves around the use of the isspace function in programming to remove excess white space from strings read from text files. Participants are exploring methods to ensure that only a single space remains between words while eliminating additional spaces.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant suggests using isspace(c) to identify spaces and proposes a method to copy characters from one array to another, allowing only the first space after a word to be copied.
  • Another participant shares a code snippet that attempts to remove extra white space but inadvertently deletes the first character of every word that follows a space, leading to incorrect output.
  • A later reply points out a potential misuse of the get() function in the code, suggesting that the participant may not be using it correctly.

Areas of Agreement / Disagreement

Participants have not reached a consensus on the best approach to use the isspace function effectively, and there are differing opinions on the implementation details and correctness of the code provided.

Contextual Notes

There are unresolved issues regarding the correct use of the get() function and the handling of characters in the input stream, which may affect the outcome of the proposed solutions.

magnifik
Messages
350
Reaction score
0
I am trying to remove excess white space using the isspace function. For example, I am reading text from a txt file that says:

I am super sleepy.

------------------------------
but I want it to say:

I am super sleepy.

------------------------------

I was thinking I could use the isspace(c), where c is each character being read and try to make a statement that somehow erases all but one space. Any suggestions??
 
Physics news on Phys.org
Added [ code] tags make your spaces show up.
magnifik said:
I am trying to remove excess white space using the isspace function. For example, I am reading text from a txt file that says:
Code:
I am                super sleepy.
------------------------------
but I want it to say:
Code:
I am super sleepy.
------------------------------

I was thinking I could use the isspace(c), where c is each character being read and try to make a statement that somehow erases all but one space. Any suggestions??
What I would do is read the original sentence into one character array, and then copy characters to another character array. In the copy operation, when I encountered the first space after a word, I would copy that space, but would ignore any subsequent spaces and not copy them. When I hit the first non-space character I would copy it and the subsequent characters up to the first space after the word, and so on, as before.
 
Hmm.. I tried using this segment of code in my program

Code:
			while (isspace(c))
			{
				if(!inf.get(c))
					return false;
			}

it does delete extra white space, but in the process it also deletes the first character of every word preceded by a white space...

For example, if the input text file read:

Why is this program so difficult?

The output text file prints:

Why s his rogram o ifficult?

I don't know what to do to modify my code to fix this problem...any help would be appreciated!
 
You are not using get() correctly. Here is a link to some documentation for this function: http://www.cplusplus.com/reference/iostream/istream/get/
 
Last edited by a moderator:

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 8 ·
Replies
8
Views
7K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
7
Views
3K
Replies
4
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 1 ·
Replies
1
Views
801