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 888137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:20:48+00:00 2026-05-15T13:20:48+00:00

How to remove blank spaces in a string with a complexity of O(n). My

  • 0

How to remove blank spaces in a string with a complexity of O(n).
My approach is using two indexes. One will traverse till length on string. Other will be incremented only when a non-blank character is encountered.
But i am not sure of this approach.

TIA,
Praveen

  • 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-15T13:20:48+00:00Added an answer on May 15, 2026 at 1:20 pm

    This approach is fine. The O(n) requirement simply means that the running time is proportional to the number of items which in this case means the number of characters in the string (assuming you mean time complexity which is a fairly safe bet here).

    The pseudocode:

    def removeSpaces (str):
        src = pointer to str
        dst = src
        while not end-of-string marker at src:
            if character at src is not space:
                set character at dst to be character at src
                increment dst
            increment src
        place end-of-string marker at dst
    

    is basically what you’re trying to do.

    Because it has a single loop dependent only on the number of characters, it is indeed O(n) time complexity.


    The following C program shows this in action:

    #include <stdio.h>
    
    // Removes all spaces from a (non-const) string.
    
    static void removeSpaces (char *str) {
        // Set up two pointers.
    
        char *src = str;
        char *dst = src;
    
        // Process all characters to end of string.
    
        while (*src != '\0') {
            // If it's not a space, transfer and increment destination.
    
            if (*src != ' ')
                *dst++ = *src;
    
            // Increment source no matter what.
    
            src++;
        }
    
        // Terminate the new string.
    
        *dst = '\0';
    }
    

     

    // Test program.
    
    int main (void)
    {
        char str[] = "This is a long    string with    lots of spaces...   ";
        printf ("Old string is [%s]\n", str);
        removeSpaces (str);
        printf ("New string is [%s]\n", str);
        return 0;
    }
    

    Running this gives you:

    Old string is [This is a long    string with    lots of spaces...   ]
    New string is [Thisisalongstringwithlotsofspaces...]
    

    Note that, if there are no spaces in the string, it simply copies every character over itself. You might think that you could optimise it by checking if src == dst and not copying but you’ll probably find the check is as expensive as the copy. And, unless you’re frequently copying multi-megabyte strings, performance won’t be an issue here.

    Also keep in mind this will be undefined behaviour with const strings but that would be the case in any in-place modification.

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

Sidebar

Related Questions

How to remove all blank spaces and empty lines from a txt File using
I want to remove a blank page from a PDF generated using the iText
I am using this code to find blank spaces in a file name in
How to remove a blank page that gets added automatically after \part{} or \chapter{}
How do I remove a USB drive using the Win32 API? I do a
Is there a way to remove the blank space between the words in the
I've been working on this algorithm in C# to take a given string, remove
I need to remove a blank line prior to a match. So given the
How to remove space or blank line at begin , i have on my
I need to Trim a String . But I want to remove all the

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.