Hey I am trying to work on a problem:
----------------------------------------------------------------------------
Write a function named removeS 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 must not use any square brackets and must not use the strcpy library function.
int main()
{
char msg[50] = "She'll be a massless princess.";
removeS(msg);
cout << msg; // prints he'll be a male prince.
}
---------------------------------------------------------------------------
Since I'm only allowed to make one local variable of a pointer, I'm guessing making a new c string is out of the question. I made a for loop that checks for an 's' and replaces it with the next letter.
1 2 3 4 5 6 7 8 9 10 11 12
|
void removeS(char* message)
{
char* ptr = message;
for(; *ptr != '\0'; ptr++)
{
while(*ptr == 's' || *ptr == 'S')
{
*ptr = *(ptr+1);
}
}
}
|
Is this the right way to go about the problem? If so, how can I solve the problem of the rest of the characters?(She'll would become hhe'll)?
Or is there another more straightforward way of doing it