Eliminating spaces in excess of a text

Asked

Viewed 527 times

0

I have the code of a function that should adjust space in the sentence, but it is not working.

I’d like to do with input user the same way the function that removes works.

Suppose I have the following phrase:

input: meu codigo em C para ajustar espaços.

The return should be: meu codigo em C para ajustar espaços.

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

void rSpace(char *string)
{

    int i = 0, j = 0;
    int tam = strlen(string);

    for(i = 0; i < tam; i++)
    {
        if(string[i] != ' ') {
            string[j] = string[i];
            j++;
        }
    }

   string[j] = '\0';
}

void jSpace(char *string)
{
    char blank[1000];
    int c = 0, d = 0;

    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++;
                }
           }
        }

        blank[d] = string[c];
        c++;
        d++;
    }

    blank[d] = '\0';
}

int main()
{
    //char frase[200] = "minha   frase com   algums     espaços";
    //rSpace(frase); //output: minhafrasecomalgumsespaços OK
    //printf("%s\n", frase);

    char frase2[200] = "minha   frase com   algums     espaços";

    //output: minha frase com algums espaços //ERRO não esta validando ajustes no espaço
    jSpace(frase2);

    printf("%s\n", frase2);

    return 0;
}
  • I recommend you look for Trim algorithms, have several already implemented and very good!

1 answer

1


I was on the right track, I just needed to find the condition that would determine whether or not space should be copied. Knowing this the algorithm can be very simple.

What should you copy? All characters different space OR the previous character (last analyzed) is different from space. After all, if the last one was a space enough, I don’t need the others.

Obviously you need a variable to control which is the last character parsed.

This algorithm still has a problem, and I’ll leave it up to you. If you have spaces at the beginning and end of the sentence you would normally delete all spaces and are currently leaving one. Then if you cannot or are not sure that it is correct and simple (it is very important to make simple algorithms, your jSpace() was increasingly crazy) post a new question with the code for us to analyze. But first try to do.

Tips:

  • to delete the first one only has to make a small change at the beginning of the function, no need to add or delete anything, just change
  • to delete the last space you need to do a check at the end of the function, if you know how to use the conditional operator, will not add a single line to the algorithm.

Note that I eliminated the strlen(), it is almost never necessary to use it and it’s better this way.

#include <stdio.h>

void rSpace(char *string) {
    int j = 0;
    char last = '\0';
    for (int i = 0; string[i] != '\0'; i++) {
        if (string[i] != ' ' || last != ' ') {
            string[j] = string[i];
            j++;
            last = string[i];
        }
    }
   string[j] = '\0';
}

int main() {
    char frase2[200] = "minha   frase com   algums     espaços";
    rSpace(frase2);
    printf("%s\n", frase2);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Nice guy worth I tried several ways but I got a little lost and ended up not understanding well sorry the delay is that I was traveling and could not access my...

  • my jSpace function has already solved

  • @dark777 is no problem, take a look at [tour] to understand how we work. You can accept and vote the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.