|
-
February 27th, 2006, 02:01 AM
#1
having trouble manipulating array of char
I'd like to remplace every 'c' in my char array into 'a'
did something like
Code:
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
-
February 27th, 2006, 02:05 AM
#2
Re: having trouble manipulating array of char
 Originally Posted by GoDaddy
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
Code:
for(i=0; array[i]!='\0'; i++)
{
if(array[i] == 'c')
{
array[i] = 'a';
}
}
- Sreehari
"Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
" Everybody is sent to Earth on a purpose. I am so Lagging behind that i won't die." – Calvin
-
February 27th, 2006, 02:17 AM
#3
Re: having trouble manipulating array of char
thanks
How could i then ... remove the 'c' in my array of char then? remove it completely
like ... aaacaaa would become aaaaaa
-
February 27th, 2006, 02:28 AM
#4
Re: having trouble manipulating array of char
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
- Sreehari
"Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
" Everybody is sent to Earth on a purpose. I am so Lagging behind that i won't die." – Calvin
-
February 27th, 2006, 02:35 AM
#5
Re: having trouble manipulating array of char
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.
-
February 27th, 2006, 02:39 AM
#6
Re: having trouble manipulating array of char
Yes there is a simpler solution - use std::string - use std::remove_if algorithm. Regards.
Last edited by exterminator; February 27th, 2006 at 02:41 AM.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
February 27th, 2006, 02:41 AM
#7
Re: having trouble manipulating array of char
 Originally Posted by GoDaddy
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
Code:
int j=0;
for(int i=0; array[i]!='\0';i++)
{
if(array[i] == 'c') continue;
newArray[j++] = array[i];
}
- Sreehari
"Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
" Everybody is sent to Earth on a purpose. I am so Lagging behind that i won't die." – Calvin
-
February 27th, 2006, 02:43 AM
#8
Re: having trouble manipulating array of char
 Originally Posted by sreehari
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
Code:
int j=0;
for(int i=0; array[i]!='\0';i++)
{
if(array[i] == 'c') continue;
newArray[j++] = array[i];
}

Thanks, i guess i'm wasting memory with that solution.
-
February 27th, 2006, 03:08 AM
#9
Re: having trouble manipulating array of char
how come u use *str++, that will not work, u must use str++ if using pointers.
-
February 27th, 2006, 11:18 AM
#10
Re: having trouble manipulating array of char
 Originally Posted by Odiee
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 ....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|