0

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 a c answer: Remove spaces in string, excluding these in specified between specified characters

Please help.

1
  • Use strtok. It removes your specified delimiter. Commented Jan 23, 2016 at 6:17

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I get a whole bunch of errors pointing at the malloc function. Is it a c++ only function?
malloc is a C function. Put the line #include <stdlib.h> at the very top of your file.
Make sure you also have string.h and stdio.h included while you're at it.
It's also a good idea to check that malloc() succeeds before using the return result.
0

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;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.