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

  • Home
  • SEARCH
  • 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 7734117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:10:12+00:00 2026-06-01T07:10:12+00:00

I have simple algorithm that clean the whitespace from half string until end. Here

  • 0

I have simple algorithm that clean the whitespace from half string until end. Here it is:

char a[] = "abc         "; /* The string to string to trim. */
printf("orginal [%s]\n", a);
char * org = strdup(a); /* duplicate the orginal string */
char * half_str = org + strlen(a) / 2; /* Get the half of string. */
strrev(half_str); /* reverse the string */
char * phs = half_str; /* Point to half_string */
char * news = malloc(strlen(half_str) + 1); /* The new string, without spaces. */
char * ptonews = news; /*Pointer to new string */

while(*phs) 
{
    /* if it's not whitespace like( ,\f,\r,\t,\v,\n) then concat to new string. */
    if(!isspace(*phs)) {
        *ptonews ++= *phs;      
    }
    phs ++;
}

/*Put the 0-terminator into new string. */
*phs ++ = '\0'; 

/* Replace the half_str with the newstring */
strcpy(half_str, news);

printf("new string [%s]\n", org);

it works fine. the output is:

orginal [abc        ]
new string [abc]

But the C code is a bit slow. How can I improve it?

  • 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-06-01T07:10:14+00:00Added an answer on June 1, 2026 at 7:10 am

    You’re doing a lot of unnecessary allocation. Try something like this:

    char a[] = "abc     ";
    printf( "original [%s]\n", a);
    int halfTrimmed = strlen(a)/2 + 1;
    for( ; halfTrimmed>0; halfTrimmed-- )
        if( !isspace(a[halfTrimmed-1]) ) break;
    char* news = malloc( halfTrimmed+1 );
    strncpy( news, a, halfTrimmed );
    printf( "new string [%s]\n", news );
    

    In words, this algorithm finds the index of half the string (halfTrimmed), then starts looking at each character, starting at that index, until it finds the first non-space index. It then copies from the beginning of the original string to the last non-space index into a new string, and you’re done.

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

Sidebar

Related Questions

I have a relatively simple algorithm that walks an std::vector looking for two neighbouring
I have a language-agnostic question about an algorithm. This comes from a (probably simple)
I have converted a relatively simple algorithm that performs a large number of calculations
I am building a Space Invaders game. I have got a simple algorithm that
I have a retain/release project that has a simple algorithm. I start out w/
I have to implement simple ray tracing algorithm, but I can not figure out
I'm working with my simple PHP captcha algorithm ( http://www.source.ofitall.com/devel/captcha.php ) and I have
I have simple win service, that executes few tasks periodically. How should I pass
I have a simple (but performance critical) algorithm in C (embedded in C++) to
WCF binding chooser algorithm I have watched a speak at a TechEd conference that

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.