0

The task is to remove all of the extra whitespaces in a string, for example if a string looks like this:

   volim      OR      

after the program is done it should look like this:

volim OR

so the program has to remove all the whitespaces before and after the strings, and those in between the words, so in the end there is just one between each word. This what I have so far :

#include <stdio.h>

char *IzbaciViskaRazmake (char *Str)
{
    char *p = Str;
    char *p2 = Str;
    while(*p!='\0')
    {
        if(*p==' ')
            p++;
        else if(*p!=' ')
        {
            *p2 = *p;
            p2++;
            p++;
        }
    }
    *p2='\0';
    return Str;
}

int main() {
    char tekst[] = "        volim      OR      ";
    IzbaciViskaRazmake(tekst);
    printf("'%s'",tekst);
    return 0;
}

The problem with my code is that it removes all of the whitespaces so it gives the output

volimOR

How can I repare my code, so it keeps the whitespaces between the words. PS- The use of pointers is a must.

5
  • 3
    use of pointers is a must - please show me how to do it without pointers... Commented Aug 20, 2018 at 21:44
  • So you are skipping all of the spaces. Why not to have some kind of state variable telling that the sequence of spaces is just starting, so you could keep one? Commented Aug 20, 2018 at 21:46
  • 1
    Try drawing up a state machine that should be able to handle the task and then code up the state machine. Commented Aug 20, 2018 at 21:46
  • Sorry, by that I meant, that the use of indexation like string[i] or *(s+1) is forbidden. Only pointer arithmetic is allowed. Commented Aug 20, 2018 at 21:46
  • 4
    *(s+1) is pointer arithmetic. Commented Aug 20, 2018 at 21:50

3 Answers 3

1

You can do this by adding the first (whitespace) to *p2 and skipping the rest (whitespace) using a while-loop.

Try this modified code , This will work :-

#include <stdio.h>

char *IzbaciViskaRazmake(char *Str)
{
    char *p = Str;
    char *p2 = Str;
    while (*p != '\0')
    {
        if (*p == ' ')
        {
            if (*p != *Str) // Not begining
            {
                *p2 = *p; // adding the first ' '
                p2++;
                p++;
            }
            while (*p == ' ') // skipping the rest ' '
            {
                p++;
            }
        }
        else
        {
            *p2 = *p;
            p2++;
            p++;
        }
    }
    if (*(p2 - 1) == ' ') //ends with space
    {
        p2--;
    }
    *p2 = '\0';

    return Str;
}

int main()
{
    char tekst[] = "        volim      OR      ";
    IzbaciViskaRazmake(tekst);
    printf("'%s'", tekst);
    return 0;
}

Output :-

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

1 Comment

Almost. The lead space should not be there per the OP's statement of what the result "should look like." And btw, the if-part of else if (*p != ' ') is redundant. You already know that's true because if (*p == ' ') failed.
0

Anoopknrs code gave me an idea how this code actually works, so I have managed to quickly fix it. Here is the fully working, hopefully it can help some other people.

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

    char *IzbaciViskaRazmake (char *Str)
    {
        char *p = Str;
        char *p2 = Str;
        while(*p!='\0')
        {
            if(*p==' ')
                p++;
            else if(*p!=' ')
            {
                *p2 = *p;
                p2++;
                p++;
                if(*p==' ')
                    {
                        *p2=*p;
                        p2++;
                        p++;
                    }
            }
        }
        *(p2-1)='\0';
        return Str;
    }

    int main() {
        char tekst[] = "        volim      OR      ";
        IzbaciViskaRazmake(tekst);
        printf("'%s'",tekst);
        return 0;
    }

1 Comment

your code may fail if your string do not end with ` ` . Try my modified code.
0

I think you should divide this into three separate tasks:

  • Remove all whitespaces before first non-whitespace character
  • Remove all whitespaces after last non-whitespace character
  • Remove all whitespaces between words

This is three pretty separate tasks, and a reasonable assumption in your case seems to be to only count ' ' as whitespace. However, functions like this should i general return a NULL pointer on failure.

So we can declare this function:

char * trimSpaces(char *str) {
    if(!str) return NULL; // If we get a NULL pointer as input
    if((str = trimInitialSpaces(str)) == NULL) return NULL;
    if((str = trimEndingSpaces(str)) == NULL) return NULL;
    if ((str = trimSuperfluousSpaces(str)) == NULL) return NULL;
    return str;
}

Now you have three functions to write, but each of these is simpler than your original function.

The implementation of trimIntitialSpaces could then look like this:

char * trimInitialSpaces(char *str) {
    if(!str) return NULL;
    char * ptr = str;
    int len = strlen(str); // Get length of string                              
    if(len == 0) return str; // If len is 0, there's nothing to do
    while(*ptr == ' ' && *ptr != '\0') ptr++; // Find first non-space                               
    // memmove can handle overlapping arrays, which memcpy cannot               
    memmove(str, ptr, len-(ptr-str)+1);
    return str;
}

You have two functions left to write now. trimEndingSpaces(str) is slightly trickier, but all in all almost the same as trimInitialSpaces(str). And then you only have trimSuperfluosSpaces(str) left, which is the trickiest one. But now you can debug all these three separate. Good luck.

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.