I would like to remove all white spaces in a string from a code sample so that way it can print out as a character array. However, I need to approach this by using pointer notation. I would also like some helpful explanations or comments on the sample code concerning pointer notation. Here is the following code:
#include<iostream>
using namespace std;
// Function Prototype
void trimSpcaes(char *str);
// Use only pointer Notation. Do not use array notation! do not use []
int main()
{
char str[] = "hi this is a test";
trimSpcaes(str);
}
or you can move ptr around (usually harder to follow unless its strict increment type changes)
memmove could be useful here. it is a pointer function that can move memory it its own buffer safely, so if you find a space, you move everything on the other side up it up a notch.
its inefficient, but its a way to do it in the same memory (same as the shuffle method above).
strtok is also a good answer, minimal effort:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void ts(char * s)
{
for(char* p = strtok(s," "); p; p = strtok (nullptr, " "))
{
cout << p;
}
}
int main()
{
char str[] = "hi this is a test";
ts(str);
}
that just prints it. If you need the actual string, you can strcat together the pieces back into a new string.