943,547 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 668
  • C RSS
Aug 4th, 2009
0

Removing chars from a string

Expand Post »
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. }
Similar Threads
Reputation Points: 34
Solved Threads: 7
Posting Whiz in Training
MrNoob is offline Offline
218 posts
since May 2009
Aug 4th, 2009
0

Re: Removing chars from a string

Its working fine with me..!!!No problem at all!!!
Reputation Points: 22
Solved Threads: 12
Junior Poster
Dream2code is offline Offline
144 posts
since Jun 2009
Aug 4th, 2009
0

Re: Removing chars from a string

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.
Reputation Points: 22
Solved Threads: 12
Junior Poster
Dream2code is offline Offline
144 posts
since Jun 2009
Aug 4th, 2009
1

Re: Removing chars from a string

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.
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Aug 4th, 2009
0

Re: Removing chars from a string

thanks i was using putchar as to debug output but if i did temp as char array would it work 2 ?
Reputation Points: 34
Solved Threads: 7
Posting Whiz in Training
MrNoob is offline Offline
218 posts
since May 2009
Aug 4th, 2009
0

Re: Removing chars from a string

Click to Expand / Collapse  Quote originally posted by MrNoob ...
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...
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Aug 4th, 2009
0

Re: Removing chars from a string

i think it can be with VLA variables
Reputation Points: 34
Solved Threads: 7
Posting Whiz in Training
MrNoob is offline Offline
218 posts
since May 2009
Aug 4th, 2009
0

Re: Removing chars from a string

anyways thanks case solved
Reputation Points: 34
Solved Threads: 7
Posting Whiz in Training
MrNoob is offline Offline
218 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Need help for run time speaker indicator
Next Thread in C Forum Timeline: dice probability





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC