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

  • Thread starter magnifik
  • Start date
  • Tags
    Function
In summary, the individual is attempting to remove excess white space from a text file using the isspace function. They are considering using a character array and copying characters to another array, ignoring subsequent spaces after a word. However, they are facing difficulties with their current code and are seeking help.
  • #1
magnifik
360
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
  • #2
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.
 
  • #3
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!
 
  • #4
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:
  • #5


The isspace function is a useful tool for removing excess white space in a string of text. This function checks each character in a string and returns a boolean value indicating whether or not the character is a white space. By using this function in conjunction with other string manipulation methods, you can easily remove any extra spaces in your text.

In your specific example, you could use a loop to go through each character in the string and check if it is a white space using the isspace function. If it is, you can replace it with a single space. This can be achieved using the replace method in most programming languages.

Another approach could be to use the split method to split the string into an array of words, and then join the array back together with a single space as the delimiter. This will automatically remove any extra spaces between words.

Overall, the isspace function is a helpful tool for removing excess white space in a string of text. With some additional string manipulation methods, you can easily achieve the desired result of removing extra spaces in your text.
 

1. What is the purpose of the isspace function?

The isspace function is used to determine whether a given character is a whitespace character or not. This includes spaces, tabs, and newlines.

2. How do I use the isspace function in my code?

To use the isspace function, you need to include the ctype.h header file in your code. Then, you can call the function and pass in the character you want to check as an argument.

3. What is the return value of the isspace function?

The isspace function returns an integer value of either 0 (false) or non-zero (true). This makes it useful for conditional statements in your code.

4. Can the isspace function be used for non-ASCII characters?

Yes, the isspace function can be used for both ASCII and non-ASCII characters. It checks for the presence of whitespace characters based on the current locale setting.

5. Are there any alternative functions to isspace?

Yes, there are alternative functions such as isblank and isspace_l that offer similar functionality to isspace, but with different criteria for what is considered a whitespace character. It is important to check the documentation for each function to determine the best one for your specific needs.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
16
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
6K
  • Special and General Relativity
Replies
29
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
21
Views
533
Back
Top