I'd like to remplace every 'c' in my char array into 'a'
did something like
how can i do so? sorry for my newbienessCode:while(*str!='\0')
{
if(*str=='c')
//thought i could do *str='a' but i guess i cannot
*str++;
}
Printable View
I'd like to remplace every 'c' in my char array into 'a'
did something like
how can i do so? sorry for my newbienessCode:while(*str!='\0')
{
if(*str=='c')
//thought i could do *str='a' but i guess i cannot
*str++;
}
since you said it s an char array ....Quote:
Originally Posted by GoDaddy
try this
Code:
for(i=0; array[i]!='\0'; i++)
{
if(array[i] == 'c')
{
array[i] = 'a';
}
}
thanks
How could i then ... remove the 'c' in my array of char then? remove it completely
like ... aaacaaa would become aaaaaa
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
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.
Yes there is a simpler solution - use std::string - use std::remove_if algorithm. Regards.
yeah that is always possible....its just that you are using another array ... when the same job can be done with the existing one...Quote:
Originally Posted by GoDaddy
anyway ...
this is how probably you could go about it
:wave:Code:int j=0;
for(int i=0; array[i]!='\0';i++)
{
if(array[i] == 'c') continue;
newArray[j++] = array[i];
}
Quote:
Originally Posted by sreehari
Thanks, i guess i'm wasting memory with that solution.
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 ....Quote:
Originally Posted by Odiee