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!
– Gabriel Pellegrino