Single skipping multiple spaces is replaced by single spaces and eliminates spaces and spaces

void RemoveSpace(char *String)
{
    int i=0,y=0;
    int leading=0;

    for(i=0,y=0;String[i]!='\0';i++,y++)
    {
        String[y]=String[i];    // let us copy the current character.

        if(isspace(String[i]))  // Is the current character a space?
        {
            if(isspace(String[i+1])||String[i+1]=='\0'||leading!=1) // leading space
                y--;
        }
        else
            leading=1;
    }
    String[y]='\0';
}

Does this do the trick of removing leading and lagging spaces and replacing multiple spaces with single ones? I tested it for the zero line, all spaces, leading spaces and trailing spaces.

Do you think this is an effective one-pass solution?

+3
source share
4 answers

based on your code, suppose your iswhite () is efficient, (that you cannot make it separate, as it is called too frequent) suppose that the one in the line is valid in itself (should be more secure)

=========================

void RemoveSpace(char *String)
{

    int i=0, j=0;

    int inWhite=0;

    char c = String[i++];
    while(c)
    {
        if (isspace(c))
        {
            inWhite= 1;
        }
        else
        {
            // there are space before, and not beginning
            if (inWhite && j > 0)
            {
                String[j++] = ' ';
            }
            String[j++] = c;
            inWhite = 0;
        }

        c = String[i++];
    }
    String[j]='\0';
}

not verified, please check yourself ...

0
source

:

void rem_space(char *str)
{
    int len = strlen(str) - 1;
    int i = 0;
    int spaces = 0;

    if(str == NULL) return;
    while(i < len){
        while(str[i] == ' ') {spaces++; i++;}
        while(str[i] != ' ' && str[i] != '\0') {str[i - spaces] = str[i]; i++;}
        if(str[i + spaces - 1] != '\0') {
            str[i - spaces] = ' '; spaces--; 
        } else {
            break;
        }
    }
    str[i - spaces] = '\0';
    return;
}
0

?

- .

void Test(const char *input, const char *expected_output) {
    char buffer[80];
    strcpy(buffer, input);
    RemoveSpace(buffer);
    assert(strcmp(buffer, expected_output) == 0);
}

int main() {
    Test("   Leading spaces removed.", "Leading spaces removed.");
    Test("Trailing spaces removed.  ", "Trailing spaces removed.");
    Test("Inner spaces      trimmed.", "Inner spaces trimmed.");
    Test(" A   little of  everything.  ", "A little of everything.");
    Test(" \tTabs \t\tare  \t spaces, too.", "Tabs are spaces, too.");
    return 0;
}

OP , .

, ?

. , .

C- C , . , , . , , , , .

:

#include <assert.h>
#include <ctype.h>
#include <string.h>

void RemoveSpace(char *string) {
    char *target = string;
    char *last = target;
    int skipping_spaces = 1;

    for (const char *source = string; *source != '\0'; ++source) {
        if (isspace(*source)) {
            if (!skipping_spaces) {
                *target++ = *source;
                skipping_spaces = 1;
            }
        } else {
            *target++ = *source;
            last = target;
            skipping_spaces = 0;
        }
    }
    *last = '\0';
}

, , , , , . , ( ).

0

, , . , . :

: " text" : " text"

, . :

void RemoveSpace(char *string)
{
        int i = 0, y = 0;
        while(isspace(string[i]))         // Discard leading spaces.
                i++;
        for(y = 0; string[i]!='\0'; i++)
        {
                string[y] = string[i];    // let us copy the current character.

                if(!isspace(string[i]) || !isspace(string[i+1]) && !string[i+1]=='\0')
                        y++;              // This character shall not be covered.
        }
        string[y] = '\0';
}

, , .

0

Source: https://habr.com/ru/post/1737169/


All Articles