yasin
yasin

Reputation: 212

Convert string to char array without spaces in c++

In my code, I have char array and here it is: char pIPAddress[20]; And I'm setting this array from a string with this code:strcpy(pIPAddress,pString.c_str());

After this loading; for example pIPAddress value is "192.168.1.123 ". But i don't want spaces. I need to delete spaces. For this i did this pIPAddress[13]=0;.

But If IP length chances,It won't work. How can i can calculate space efficient way? or other ways?

Thnx

Upvotes: 2

Views: 2854

Answers (5)

The simplest approach that you can do is to use the std::remove_copy algorithm:

std::string ip = read_ip_address();
char ipchr[20];
*std::remove_copy( ip.begin(), ip.end(), ipchr, ' ' ) = 0; // [1]

The next question would be why would you want to do this, because it might be better not to copy it into an array but rather remove the spaces from the string and then use c_str() to retrieve a pointer...

EDIT As per James suggestion, if you want to remove all space and not just the ' ' character, you can use std::remove_copy_if with a functor. I have tested passing std::isspace from the <locale> header directly and it seems to work, but I am not sure that this will not be problematic with non-ascii characters (which might be negative):

#include <locale>
#include <algorithm>
int main() {
   std::string s = get_ip_address();
   char ip[20];
   *std::remove_copy_if( s.begin(), s.end(), ip, (int (*)(int))std::isspace ) = 0; // [1]
}

The horrible cast in the last argument is required to select a particular overload of isspace.

[1] The *... = 0; needs to be added to ensure NUL termination of the string. The remove_copy and remove_copy_if algorithms return an end iterator in the output sequence (i.e. one beyond the last element edited), and the *...=0 dereferences that iterator to write the NUL. Alternatively the array can be initialized before calling the algorithm char ip[20] = {}; but that will write \0 to all 20 characters in the array, rather than only to the end of the string.

Upvotes: 5

slaphappy
slaphappy

Reputation: 6999

I see you have a std::string. You can use the erase() method :

std::string tmp = pString;
for(std::string::iterator iter = tmp.begin(); iter != tmp.end(); ++iter)
  while(iter != tmp.end() && *iter == ' ') iter = tmp.erase(iter);

Then you can copy the contents of tmp into your char array. Note that char arrays are totally deprecated in C++ and you shouldn't use them unless you absolutely have to. In either way, you should do all your string manipulations using std::string.

Upvotes: 0

Pieter
Pieter

Reputation: 17705

If spaces are only at the end (or beginning) of your string, you'd best use boost::trim

#include <boost/algorithm/string/trim.hpp>

std::string pString = ...
boost::trim(pString);
strcpy(pIPAddress,pString.c_str());

If you want to handcode, <cctype> has the function isspace, which also has a locale specific version.

Upvotes: 0

paper.plane
paper.plane

Reputation: 1197

I think if you are using

strcpy(pIPAddress,pString.c_str())

then nothing is required to be done, as c_str() returns the a char* to a null terminated string. So after doing the above operation your char array 'pIPAddress' is itself null terminated. So nothing needs to be done to adjust the length as you said.

Upvotes: -1

seth
seth

Reputation: 1389

To make the solution work at all cases, i suggest you iterate through your string, and when finding a space you deal with it.

A more high-level solution may be for you to use the string methods that allow you to do that automatically. (see: http://www.cplusplus.com/reference/string/string/)

Upvotes: -1

Related Questions