Deleting Characters Function

Thread Solved

Join Date: Oct 2006
Posts: 73
Reputation: aznballerlee is an unknown quantity at this point 
Solved Threads: 0
aznballerlee aznballerlee is offline Offline
Junior Poster in Training

Deleting Characters Function

 
0
  #1
Nov 23rd, 2006
My task is this, and I'm having trouble finishing up the code:
  1. Write a function named deleteS that accepts one character pointer as a parameter and returns no value. The parameter is a C string. This function must remove all of the upper and lower case 's' letters from the string. The resulting string must be a valid C string.
    Your function must declare no more than one local variable in addition to the parameter; that additional variable must be of a pointer type. Your function may not use any square brackets.
    int main()
    {
    char msg[50] = "She'll be a massless princess."
    deleteS(msg);
    cout << msg; // prints he'll be a male prince.
    }
Here is what I've managed so far:

  1. void deleteS (char* ptr[])
  2. {
  3. int*counter;
  4. for (counter = ptr; counter<ptr + strlen(ptr); counter++)
  5. {
  6. if (isupper(*ptr + counter)
  7. strcpy (" ", i);
  8. else if (*(ptr + counter) == 's')
  9. // delete the s, and move any blankspace back


I can't seem to come up with the remaining code. I hear that I can use a vector to delete elements (which is what I want to do), but I haven't learned that yet ..

How would I be able to finish up?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Deleting Characters Function

 
0
  #2
Nov 23rd, 2006
http://www.daniweb.com/code/snippet262.html

Bit of an over kill but it works if you convert the line to lower case first.

  1. int main(void)
  2. {
  3. const char before[] = "She'll be a massless princess.";
  4. printf("before = \"%s\"\n", before);
  5. test(before, "s", "");
  6. getchar();
  7. return 0;
  8. }
Last edited by iamthwee; Nov 23rd, 2006 at 4:13 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 73
Reputation: aznballerlee is an unknown quantity at this point 
Solved Threads: 0
aznballerlee aznballerlee is offline Offline
Junior Poster in Training

Re: Deleting Characters Function

 
0
  #3
Nov 23rd, 2006
The problem is that I can only use one local variable, and not the entire code you provided for me (thouugh that could work)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Deleting Characters Function

 
0
  #4
Nov 23rd, 2006
Originally Posted by aznballerlee View Post
The problem is that I can only use one local variable, and not the entire code you provided for me (thouugh that could work)
Yup I agree, that is an overkill. If your function doesn't have to return anything, i.e doesn't have to alter the string, I can see how this would be very easy.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 73
Reputation: aznballerlee is an unknown quantity at this point 
Solved Threads: 0
aznballerlee aznballerlee is offline Offline
Junior Poster in Training

Re: Deleting Characters Function

 
0
  #5
Nov 23rd, 2006
How would it be easy?? I wish for feedback, as this section of the code is due in hours!
Last edited by aznballerlee; Nov 23rd, 2006 at 4:27 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Deleting Characters Function

 
0
  #6
Nov 23rd, 2006
How about using strcat?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 73
Reputation: aznballerlee is an unknown quantity at this point 
Solved Threads: 0
aznballerlee aznballerlee is offline Offline
Junior Poster in Training

Re: Deleting Characters Function

 
0
  #7
Nov 23rd, 2006
I see the idea behind strcat now.
I can show you an idea of what I have ..
it compiles but gives no output ..
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void deleteS (char ptr[])
  5. {
  6. char *counter = ptr;
  7. for (counter = ptr; counter < ptr + strlen(ptr); counter++)
  8. {
  9. if ((*(counter)== 'S') || (*(counter) == 's'))
  10. continue;
  11. else
  12. {
  13. strcat(ptr, counter);
  14. ptr++;
  15. }
  16. }
  17. return;
  18. }
  19.  
  20. int main()
  21. {
  22. char msg[50] = "She'll be a massless princess.";
  23. deleteS(msg);
  24. cout << msg; // prints he'll be a male prince.
  25. }
Last edited by aznballerlee; Nov 23rd, 2006 at 6:57 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Deleting Characters Function

 
0
  #8
Nov 24th, 2006
Both counter and ptr point to the same memory space.. so when you modify ptr in the loop you end up modifying what counter points to.

And btw have you read the description of strcat( ) since it is not doing things in your case whch you are expecting it to do.

You can look into memmove to solve your problem.
Last edited by ~s.o.s~; Nov 24th, 2006 at 2:09 am.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Deleting Characters Function

 
0
  #9
Nov 24th, 2006
Originally Posted by aznballerlee View Post
My task is this, and I'm having trouble finishing up the code:
Here is what I've managed so far:

  1. void deleteS (char* ptr[])
  2. {
  3. int*counter;
  4. for (counter = ptr; counter<ptr + strlen(ptr); counter++)
  5. {
  6. if (isupper(*ptr + counter)
  7. strcpy (" ", i);
  8. else if (*(ptr + counter) == 's')
  9. // delete the s, and move any blankspace back


I can't seem to come up with the remaining code. I hear that I can use a vector to delete elements (which is what I want to do), but I haven't learned that yet ..

How would I be able to finish up?
First thing, your function definition should be void deleteS (char* ptr) -- this passes in a pointer, you are passing in an array of pointers.

Next, set up another character pointer. You are not dealing with integers in this function. I'll call this pointer source

Set this new pointer equal to the parameter, like you did in the for statement. Now both pointers point to the same address.

Start a while loop that exits when when you reach the end of source (*source == '\0')

In the loop,
1) Copy the current character from source to the current character of ptr.
2) Increment source to point to the next character.
3) If the current character in ptr (the one just copied) is not 's' or 'S', increment ptr to point to the next character. This will overwrite any S that was copied.

When the loop exits, copy the source character to ptr one last time. This loads the ending '\0'. Your string should now be copied, and ptr should now contain no S's.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Deleting Characters Function

 
0
  #10
Nov 24th, 2006
Originally Posted by WaltP View Post
First thing, your function definition should be void deleteS (char* ptr) -- this passes in a pointer, you are passing in an array of pointers.
That was a typo I think, she has already taken care of this in her last post.
I don't accept change; I don't deserve to live.
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