How to incorporate pointer notation

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);
}

Thank you.
Last edited on
Can you write the function using array notation?
There are several ways of doing this. One is to use a temp buffer, another is to shuffle the chars.

To use a temp buffer, possibly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <cstring>

void trimSpaces(char* str);

int main()
{
	char str[] {"hi this is a test"};

	trimSpaces(str);
	std::cout << str << '\n';
}

void trimSpaces(char* str) {
	auto tmp {new char[](std::strlen(str))}, tb {tmp};

	for (auto c {str}; *c; ++c)
		if (!std::isspace(*c))
			*tb++ = *c;

	*tb++ = '\0';
	std::snprintf(str, tb - tmp, tmp);

        delete [] tmp;
}



hithisisatest


Note. Rather than new/delete, std::unique_ptr would usually be used.

Or as a char shuffle:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstring>

void trimSpaces(char* str);

int main()
{
	char str[] {"hi this is a test"};

	trimSpaces(str);
	std::cout << str << '\n';
}

void trimSpaces(char* str)
{
	auto curr {str};

	for (; *str; ++str)
		if (!isspace(*str))
			*curr++ = *str;

	*curr = '\0';
}

Last edited on
re pointer notation/usage. See
https://www.learncpp.com/cpp-tutorial/introduction-to-pointers/
and subsequent links
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cctype>
using namespace std;

void trimSpaces(char *str)
{
   char *current = str;
   for ( ; *str; str++ )
   {
      if ( !isspace( *str ) ) *current++ = *str;
   }
   *current = 0;
}

int main()
{
   char str[] = "hi this is a test";
   trimSpaces(str);
   cout << str;
}
*(ptr + offset)
is the same as
ptr[offset]

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.
Last edited on
You guys just can't help yourselves. I thought we weren't a homework service.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>

void trimSpaces(char *str, int length)
{
    int position{0};
    for(int i = 0; i < length; i++)
    {
        if( !isspace( str[i] )  )
        {
            str[position] = str[i];
            position++;
        }
            
    }
    str[position] = '\0'; //TERMINATE REVISED c-String
}

int main()
{
    char str[] = "hi th is is a test";
    trimSpaces( str, sizeof(str)/sizeof(char) );
    std::cout << str << '\n';
}
Topic archived. No new replies allowed.