Removing chars from a string

Thread Solved

Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Removing chars from a string

 
0
  #1
Aug 4th, 2009
Hey its me again i dunno this string stuff gonna drive me insane really been doing since yesterday exercises on string did half of them but i have this program which should remove chars that i specify and when i test with putchar it does remove them but The string itself didnt !!!
  1. #include <stdio.h>
  2. #include <string.h>
  3. int RemoveChars(char *S,char c) {
  4. int i=0;
  5. int spaces=0;
  6. char temp;
  7. for(i=0;S[i]!=0;i++) {
  8. temp=S[i];
  9. if(S[i]!=c) {
  10. S[i]=temp;
  11. putchar(temp);//this totally works !!!!!!
  12. }
  13. else
  14. spaces++;
  15. }
  16. return spaces;
  17. }
  18. int main(void)
  19. {
  20. char name[]="dude and duder";
  21. int spaces;
  22. spaces=RemoveChars(name,' ');
  23. printf("Now after its Changed its %s and there was %d spaces\n",name,spaces);//What the Hell why doesnt it WORK !!!!!!!
  24. return 0;
  25. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 139
Reputation: Dream2code is an unknown quantity at this point 
Solved Threads: 11
Dream2code's Avatar
Dream2code Dream2code is offline Offline
Junior Poster

Re: Removing chars from a string

 
0
  #2
Aug 4th, 2009
Its working fine with me..!!!No problem at all!!!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 139
Reputation: Dream2code is an unknown quantity at this point 
Solved Threads: 11
Dream2code's Avatar
Dream2code Dream2code is offline Offline
Junior Poster

Re: Removing chars from a string

 
0
  #3
Aug 4th, 2009
You should try sumthing like this:
1)return the spaces by the function(you r doing it..good)
2)Pass the char ** to the function and take an temporary sting remove spaces from that then assign address of the temp sting to your char** which is passed to the function.

  1. "dont print valuse using putchar inside the function."
Last edited by Dream2code; Aug 4th, 2009 at 7:21 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Removing chars from a string

 
1
  #4
Aug 4th, 2009
Here is your code and its logical error.
int RemoveChars(char *S,char c) {
    int i=0;
    int spaces=0;
    char temp;
    for(i=0;S[i]!=0;i++) {
        temp=S[i];
        if(S[i]!=c) {
            // temp = S[i] and S[i] = temp, Hence, S[i] = S[i]
            S[i]=temp; 
            putchar(temp);
        }
        else
            spaces++;
    }
    return spaces;
}

So basically, the reason why putchar(temp); works is because you avoid printing character that you want to remove into the screen. However, your string that store in the memory remain the same.

So, I made some correction to your code and it works perfectly, I guess.

  1. int RemoveChars(char *S,char c)
  2. {
  3. int count=0;
  4. int spaces = 0;
  5. char* temp;
  6. // allocate enough memory to store new string after remove.
  7. for(int i=0; S[i] != 0; i++) {
  8. if (S[i]==c)
  9. spaces++;
  10. else
  11. count++;
  12. }
  13. temp = (char*)malloc(count+1);
  14.  
  15. // write new string without character you want to remove
  16. for(int j=0, i=0; S[i] != '\0'; i++) {
  17. if (S[i]!=c)
  18. temp[j++] = S[i];
  19. }
  20. temp[count] = '\0'; // end string
  21.  
  22. strcpy(S, temp); // copy new string to old string
  23. free(temp); // de-allocate new string memory.
  24.  
  25. return spaces;
  26. }
Last edited by invisal; Aug 4th, 2009 at 10:00 am.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: Removing chars from a string

 
0
  #5
Aug 4th, 2009
thanks i was using putchar as to debug output but if i did temp as char array would it work 2 ?
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Removing chars from a string

 
0
  #6
Aug 4th, 2009
Originally Posted by MrNoob View Post
thanks i was using putchar as to debug output but if i did temp as char array would it work 2 ?
You can also use temp as char array, but you don't know how much element you need for your new string...
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: Removing chars from a string

 
0
  #7
Aug 4th, 2009
i think it can be with VLA variables
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: Removing chars from a string

 
0
  #8
Aug 4th, 2009
anyways thanks case solved
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC