Deleting spaces in excess of a text

0

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

I would like to do with user's input in the same way that the remove function works.

Suppose I have the following sentence:

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] = '
#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] = '%pre%';
}

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

    while(string[c] != '%pre%') 
    {
        if (string[c] == ' ') 
        {
            int temp = c + 1;

            if (string[temp] != '%pre%') 
            {
                while (string[temp] == ' ' && string[temp] != '%pre%') 
                {
                    if (string[temp] == ' ') 
                    {
                        c++;
                    }

                    temp++;
                }
           }
        }

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

    blank[d] = '%pre%';
}

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;
}
'; } void jSpace(char *string) { char blank[1000]; int c = 0, d = 0; while(string[c] != '%pre%') { if (string[c] == ' ') { int temp = c + 1; if (string[temp] != '%pre%') { while (string[temp] == ' ' && string[temp] != '%pre%') { if (string[temp] == ' ') { c++; } temp++; } } } blank[d] = string[c]; c++; d++; } blank[d] = '%pre%'; } 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; }
    
asked by anonymous 12.02.2017 / 09:03

1 answer

1

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

What should you copy? All characters other than space OR the previous character (last parsed) is different from space . After all if the last one was enough space, I do not need the others.

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

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

Tips:

  • To delete the first one just make a small change at the beginning of the function, do not 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 , it will not add a line to the algorithm at all.

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

#include <stdio.h>

void rSpace(char *string) {
    int j = 0;
    char last = '
#include <stdio.h>

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

int main() {
    char frase2[200] = "minha   frase com   algums     espaços";
    rSpace(frase2);
    printf("%s\n", frase2);
}
'; for (int i = 0; string[i] != '%pre%'; i++) { if (string[i] != ' ' || last != ' ') { string[j] = string[i]; j++; last = string[i]; } } string[j] = '%pre%'; } int main() { char frase2[200] = "minha frase com algums espaços"; rSpace(frase2); printf("%s\n", frase2); }

See running on ideone . And No Coding Ground . Also put it on GitHub for future reference .

    
12.02.2017 / 16:21