Click to See Complete Forum and Search --> : having trouble manipulating array of char
GoDaddy
February 27th, 2006, 01:01 AM
I'd like to remplace every 'c' in my char array into 'a'
did something like
while(*str!='\0')
{
if(*str=='c')
//thought i could do *str='a' but i guess i cannot
*str++;
}
how can i do so? sorry for my newbieness
sreehari
February 27th, 2006, 01:05 AM
I'd like to remplace every 'c' in my char array into 'a'
how can i do so? sorry for my newbieness
since you said it s an char array ....
try this
for(i=0; array[i]!='\0'; i++)
{
if(array[i] == 'c')
{
array[i] = 'a';
}
}
GoDaddy
February 27th, 2006, 01:17 AM
thanks
How could i then ... remove the 'c' in my array of char then? remove it completely
like ... aaacaaa would become aaaaaa
sreehari
February 27th, 2006, 01:28 AM
for removing it completely....
you will have to move all the values in the array one step ahead....
that is write the 4th element in the 3rd position .....5th in 4th n so on...there are various threads in the forum that have already discussed the same.... just do a search
check out this thread remove element from array (http://www.codeguru.com/forum/showthread.php?t=377207&highlight=arrays)
GoDaddy
February 27th, 2006, 01:35 AM
isn't there a simpler solution? what if i wanted to put the result into a new array then? instead of removing from the original i'll return a new array of char with the result.
exterminator
February 27th, 2006, 01:39 AM
Yes there is a simpler solution - use std::string (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrf_string_string.asp) - use std::remove_if (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vcsampsampleremoveifstlsample.asp) algorithm. Regards.
sreehari
February 27th, 2006, 01:41 AM
isn't there a simpler solution? what if i wanted to put the result into a new array then? instead of removing from the original i'll return a new array of char with the result.
yeah that is always possible....its just that you are using another array ... when the same job can be done with the existing one...
anyway ...
this is how probably you could go about it
int j=0;
for(int i=0; array[i]!='\0';i++)
{
if(array[i] == 'c') continue;
newArray[j++] = array[i];
}
:wave:
GoDaddy
February 27th, 2006, 01:43 AM
yeah that is always possible....its just that you are using another array ... when the same job can be done with the existing one...
anyway ...
this is how probably you could go about it
int j=0;
for(int i=0; array[i]!='\0';i++)
{
if(array[i] == 'c') continue;
newArray[j++] = array[i];
}
:wave:
Thanks, i guess i'm wasting memory with that solution.
Odiee
February 27th, 2006, 02:08 AM
how come u use *str++, that will not work, u must use str++ if using pointers.
GoDaddy
February 27th, 2006, 10:18 AM
how come u use *str++, that will not work, u must use str++ if using pointers.
Yeah i know it won't work ... my mistake ....
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.