CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2006
    Posts
    69

    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

  2. #2
    Join Date
    Oct 2005
    Location
    Bangalore
    Posts
    1,051

    Re: having trouble manipulating array of char

    Quote 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

  3. #3
    Join Date
    Feb 2006
    Posts
    69

    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

  4. #4
    Join Date
    Oct 2005
    Location
    Bangalore
    Posts
    1,051

    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

  5. #5
    Join Date
    Feb 2006
    Posts
    69

    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.

  6. #6
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

  7. #7
    Join Date
    Oct 2005
    Location
    Bangalore
    Posts
    1,051

    Re: having trouble manipulating array of char

    Quote 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

  8. #8
    Join Date
    Feb 2006
    Posts
    69

    Re: having trouble manipulating array of char

    Quote 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.

  9. #9
    Join Date
    Feb 2006
    Location
    Croatia - Zagreb
    Posts
    459

    Re: having trouble manipulating array of char

    how come u use *str++, that will not work, u must use str++ if using pointers.

  10. #10
    Join Date
    Feb 2006
    Posts
    69

    Re: having trouble manipulating array of char

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured