Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6791743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:53:00+00:00 2026-05-26T17:53:00+00:00

In my code, I have char array and here it is: char pIPAddress[20]; And

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:53:01+00:00Added an answer on May 26, 2026 at 5:53 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

String product = Integer.toString(w); char[] original = String.toCharArray(product); This is the code I have
I have this code struct Student { char name[48]; float grade; int marks[10,5]; char
I have this code #include <iostream> using namespace std; int main(int argc,char **argv) {
Newbie here, I have a struct for a word, which contains a char array
I have this program that is reading the test string from a textbox and
here in this code i have a sentence that i convert it to an
I have a struct with many char array like this (and it works) :
Assuming I have char C whose ascii code is 0110 0111 . How can
Ok, so I have the code char *token; char *delimiter = ; token =
We have the following code fragment: char tab[2][3] = {'1', '2', '\0', '3', '4',

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.