Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

removing BLANK spaces

Status
Not open for further replies.

Bashment

Technical User
Jun 30, 2005
3
JM
I have to write a program that remove blank spaces from a paragraph. How do I go about doing that?
 
the easiest way to do it would be to read the text into a buffer, make a buffer of the same size and copy the text to it a character at a time. The other easy approach is to travers the string and when a space is hit move the next character to it. when two spaces have been hit, move the next character 2 spaces back.

"hello my name is joe"

m & y move back a single space
n a m & e move back 2
i & s move back 3
etc...

The last easy approach i can think of is by using a combination of strstr & strcat to modify the buffer.

Matt
 
Hi Friend,

Here is the sample code. You can modify it to read the whole paragraph. It's very simple. Try this and let me know.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#include <stdio.h>
#include <string.h>

void RemoveSpace(char *ptr);

main()
{
char array[] = &quot;My name is Mahesh Rathi&quot;;
RemoveSpace(array);
printf(&quot;String = %s\n&quot;,array);
}

void RemoveSpace(char *ptr)
{
char *tempptr = ptr;
while(*ptr)
{
if(*ptr != ' ')
{
*tempptr = *ptr;
tempptr++;
}
ptr++;
}
*tempptr=0;
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

regards,
Mahesh

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top