| | |
Deleting Characters Function
Thread Solved |
•
•
Join Date: Oct 2006
Posts: 73
Reputation:
Solved Threads: 0
My task is this, and I'm having trouble finishing up the code:
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?
- ••••
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.
}
C Syntax (Toggle Plain Text)
void deleteS (char* ptr[]) { int*counter; for (counter = ptr; counter<ptr + strlen(ptr); counter++) { if (isupper(*ptr + counter) strcpy (" ", i); else if (*(ptr + counter) == 's') // 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?
http://www.daniweb.com/code/snippet262.html
Bit of an over kill but it works if you convert the line to lower case first.
Bit of an over kill but it works if you convert the line to lower case first.
C Syntax (Toggle Plain Text)
int main(void) { const char before[] = "She'll be a massless princess."; printf("before = \"%s\"\n", before); test(before, "s", ""); getchar(); return 0; }
Last edited by iamthwee; Nov 23rd, 2006 at 4:13 pm.
*Voted best profile in the world*
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*
•
•
Join Date: Oct 2006
Posts: 73
Reputation:
Solved Threads: 0
I see the idea behind strcat now.
I can show you an idea of what I have ..
it compiles but gives no output ..
I can show you an idea of what I have ..
it compiles but gives no output ..
C Syntax (Toggle Plain Text)
#include <iostream> using namespace std; void deleteS (char ptr[]) { char *counter = ptr; for (counter = ptr; counter < ptr + strlen(ptr); counter++) { if ((*(counter)== 'S') || (*(counter) == 's')) continue; else { strcat(ptr, counter); ptr++; } } return; } int main() { char msg[50] = "She'll be a massless princess."; deleteS(msg); cout << msg; // prints he'll be a male prince. }
Last edited by aznballerlee; Nov 23rd, 2006 at 6:57 pm.
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.
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.
•
•
•
•
My task is this, and I'm having trouble finishing up the code:
Here is what I've managed so far:
C Syntax (Toggle Plain Text)
void deleteS (char* ptr[]) { int*counter; for (counter = ptr; counter<ptr + strlen(ptr); counter++) { if (isupper(*ptr + counter) strcpy (" ", i); else if (*(ptr + counter) == 's') // 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?
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: HELP dynamically created and resized arrays
- Next Thread: Reading a file line by line problem
| Thread Tools | Search this Thread |
* adobe ansi api array arrays binarysearch calculate centimeter char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking hardware highest homework i/o inches incrementoperators intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pattern pdf performance pointer posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h






