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!

remove a character from a string 1

Status
Not open for further replies.

Saxer

Technical User
Jul 3, 2000
6
AR
how I can remove of a string that contains "227-204-242] ", the character "]"
thank you
Daniel

 
try to use this:
Code:
#include <stdio.h>

void RemoveChar( char* string, char c )
{
	char *cp = string;
	
	while( *string )
	{
		if( *string != c )
		{
			*cp = *string;
			cp++;
		}

		string++;
	}

	*cp = 0;
}

void main()
{
	char buffer[] = {&quot;227-204-242] &quot;};

	RemoveChar( buffer, ']' );

	printf(&quot;buffer = %s\n&quot;, buffer );
}

 
An alternative, but it truncates the string as well.

#include <string.h>
char buffer[80] = &quot;227-204-242]xyz&quot;;
char* toremove = strchr (buffer, '[');
*toremove = '\0';

You didn't say whether you wanted to keep the rest of the string.
 
the xwd's code's dangerous, run time error will occurs if the specified character doesn't exists in the character string.

this is mine:

Code:
void remC(int c, char *s)
{
   while ((s = strchr(s, c)) != 0)
      strcpy(s, (s + 1));
}
 
Leibnitz, xwb, and denniscpp:
Thank you to all for your help
you saved me of my problem
thank you again
Daniel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top