8 Replies - 1245 Views - Last Post: 03 December 2009 - 06:18 PM Rate Topic: -----

#1 saff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 01-November 09

remove character from ** string

Posted 03 December 2009 - 12:05 PM

How do u remove a character from a string if its not at the end this is what i have

 for(i = 0; i < j; i ++)
	  if(splitrestnew[i] != NULL)
	if(strchr(splitrestnew[i], '&') != NULL)
	{
	  ampersand =1;
	  
	  splitrestnew[i][strlen(splitrestnew[i])-1] =' ';
	  
	}



thanks
Is This A Good Question/Topic? 0
  • +

Replies To: remove character from ** string

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: remove character from ** string

Posted 03 December 2009 - 12:10 PM

You can't "remove" a slot in an already allocated array. You could remove it and then reallocate, but that's expensive.

This post has been edited by KYA: 03 December 2009 - 12:10 PM

Was This Post Helpful? 0
  • +
  • -

#3 saff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 01-November 09

Re: remove character from ** string

Posted 03 December 2009 - 12:25 PM

is it not possible to remove a character from the middle of the string because this removes the character on the end
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: remove character from ** string

Posted 03 December 2009 - 12:26 PM

Define remove. If you want to eliminate the location/slot/whatever you want to call it from the string that is not possible since it is a C style string/array.

If you want to set the location to zero, a space, or some other meaningful representation for "blank", then you can do that.
Was This Post Helpful? 0
  • +
  • -

#5 saff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 01-November 09

Re: remove character from ** string

Posted 03 December 2009 - 12:29 PM

i would like to change it to another character a space or 0 would be perfect
Was This Post Helpful? 0
  • +
  • -

#6 saff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 01-November 09

Re: remove character from ** string

Posted 03 December 2009 - 01:30 PM

does anyone know or have any ideas on how to do it
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: remove character from ** string

Posted 03 December 2009 - 02:21 PM

void charReplace(char* str, char toReplace){
	 for(int i = 0; i < strlen(str)-1; i++){
		  if(str[i] == toReplace){
				  str[i] = ' ';//replaces index with a  space, does not remove index
		  }
	 }
}



Something like that I imagine.
Was This Post Helpful? 0
  • +
  • -

#8 UG Cyber  Icon User is offline

  • D.I.C Addict

Reputation: 32
  • View blog
  • Posts: 556
  • Joined: 24-July 08

Re: remove character from ** string

Posted 03 December 2009 - 04:58 PM

If you simply set the character or slot to 0, would it not cause some problems if you used functions like strlen(), or sizeof() ?

This is what I would do but only in small strings or small programs due to the fact that it can use up alot of memory if the string is big enough. Or if i was to need any of the functions stated above.

The logic: Simply create two buffers or Strings with appropriate sizes, then copy one character at a time to the other buffer, omitting the characters that are unwanted. To help save some memory, you could run a for loop through the string to count how many characters you would be omitting, then create the second buffer that many slots smaller then the orig. then run another for loop to transfer the characters.

Sounds over complicating but it would be more accurate.
Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: remove character from ** string

Posted 03 December 2009 - 06:18 PM

View PostUG Cyber, on 3 Dec, 2009 - 04:58 PM, said:

If you simply set the character or slot to 0, would it not cause some problems if you used functions like strlen(), or sizeof() ?


No as strlen() counts the number of characters in the string (from start to '\0'). The issue would arise if he set the location equal to the null terminator.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1