简体   繁体   中英

C — Remove all extra spaces in string excluding chars between specified characters

I have this string: print "Foo cakes are yum" I need to somehow strip all extra whitespace but leave text between quotes alone. This is what i have so far:

char* clean_strip(char* string)
{
    int d = 0, c = 0;
    char* newstr;
    while(string[c] != '\0'){
         if(string[c] == ' '){
            int temp = c + 1;
            if(string[temp] != '\0'){
                while(string[temp] == ' ' && string[temp] != '\0'){
                    if(string[temp] == ' '){
                        c++;
                    }
                    temp++;
                }
            }
        }
        newstr[d] = string[c];
        c++;
        d++;
     }
    return newstr;
}

This returns this string: print "Foo cakes are yum"

I need to be able to skip text between thw quotes so i get this: print "Foo cakes are yum" .

Here is the same question but for php, i need ac answer: Remove spaces in string, excluding these in specified between specified characters

Please help.

Try this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* clean_strip(char* string)
{
    int d = 0, c = 0;
    char* newstr = malloc(strlen(string)+1);
    int quoted = 0;

    while(string[c] != '\0'){
        if (string[c] == '"') quoted = !quoted;

         if(!quoted && string[c] == ' '){
            int temp = c + 1;
            if(string[temp] != '\0'){
                while(string[temp] == ' ' && string[temp] != '\0'){
                    if(string[temp] == ' '){
                        c++;
                    }
                    temp++;
                }
            }
        }

        newstr[d] = string[c];
        c++;
        d++;
     }
    newstr[d] = 0;
    return newstr;
}


int main(int argc, char *argv[])
{
    char *input = "print     \"Foo cakes      are   yum\"";
    char *output = clean_strip(input);
    printf(output);
    free(output);
    return 0;
}

This will produce the output:

print "Foo cakes      are   yum"

It works by looking for the " character. If it's found it toggles the variable quoted . If quoted is true, then the whitespace removal is skipped.

Also, your original function never allocates memory for newstr. I added the newstr = malloc(...) part. It is important to allocate memory for strings before writing to them.

I simplified your logic a little.

int main(void)
{
    char string[] = "print     \"Foo cakes      are   yum\"";
    int i = 0, j = 1, quoted=0;
    if (string[0] == '"')
    {
        quoted=1;
    }
    for(i=1; i< strlen(string); i++)
    {
        if (string[i] == '"')
        {
            quoted = 1-quoted;
        }
        string[j] = string[i];
        if (string[j-1]==' ' && string[j] ==' ' && !quoted)
        {
            ;
        }
        else
        {
            j++;
        }
    }
    string[j]='\0';
    printf("%s\n",string);
    return 0;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM