Problem in string printing

Asked

Viewed 157 times

0

I would like to know what the problem is with my string inside the function, it goes through the string and when I find an uppercase letter turn it into a lowercase letter (I have tried using the tolower and isupper, but it did not work) and when you find a point, exclamation, question, two points and point and comma should replace them by a space and return it to the main, but it is not being changed, follows below the code of the function:

char* diminuieretira(char** p, int r)//Declaração de função que converte as letras maiusculas em minusculas e adiciona no lugar das pontuações o espaço
{
    int i = 0;

    for(i = 0; i < r; i++)
    {

        if((*p[i]) >= 65 && (*p[i]) <= 90) //Se o caracter for maisculo
            (*p[i]) + 32; //Converto-o para minusculo

        else if((*p[i]) == 46 || (*p[i]) == 44 || (*p[i]) == 59 || (*p[i]) == 58 || (*p[i]) == 33 || (*p[i]) == 63); //Se o caracter for um ponto de exclamação, ponto, interrogação, dois pontos ou ponto e virgula
            (*p[i]) = 32; //Essa pontuação é substituida por um espaço
    }

    printf("%s", (*p)); //Imprimo na tela a strign modificada

    return (*p); //Retorno para a main a string modificada
}

2 answers

1

(*p[i]) + 32; does nothing - this command adds 32 to (*p[i]), but does nothing with the result. You want

*p[i] = *p[i] + 32;

(parentheses are unnecessary) or, more succinctly

*p[i] += 32;
  • And to replace the values with space, it would be wrong to make *p[i] = 32???

  • 1

    This one’s all right, but I’d use it *p[i] = ' ' to make clear what you are doing (idem to (*p[i]) == 46 || (*p[i]) == 44 || …)

  • It keeps going wrong, the mask is used properly by me??

  • If you don’t want to make it explicit like @ctgPi spoke, you can leave the commented line explaining what each value is. The custom of writing comments is very useful for yourself and for future people who will see your code, sometimes you can go a while without seeing the code and when you need it will not remember what each value means.

  • If I withdraw "*", the compiler accuses incompatibility of arguments

  • I tried to use the for but the problem too

Show 1 more comment

0

You don’t need to use char** p in your function, you can use only char* p, since you are working only on top of a string. Utilize char** p will make your function more confusing. And unless you want to modify only part of the string, you also don’t need to pass the string size (int r), you can use the function strlen for.

Your code (*p[i]) + 32 does not change the value of *p[i], because there is an equal sign. It does nothing. And as you are using a double pointer (char**), it would be good to put writing as follows: (*p)[i], as * may refer to p or to the p[i]. It is necessary to change to: (*p)[i] = (*p)[i] + 32

And as @ctgPi said, you can put the symbols directly for your code to be more readable (or put a comment on the side). Then your job would look like this:

char* diminuieretira(char* p)//Declaração de função que converte as letras maiusculas em minusculas e adiciona no lugar das pontuações o espaço
{
    int i = 0;

    for(i = 0; i < strlen(p); i++)
    {

        if(p[i] >= 65 && p[i] <= 90) //Se o caracter for maisculo
            p[i] += 32; //Converto-o para minusculo

        else if(p[i] == '.' || p[i] == ',' || p[i] == ';' || p[i] == ':' || p[i] == '!' || p[i] == '?'); //Se o caracter for um ponto de exclamação, ponto, interrogação, dois pontos ou ponto e virgula
            p[i] = ' '; //Essa pontuação é substituida por um espaço
    }

    printf("%s", p); //Imprimo na tela a strign modificada

    return p; //Retorno para a main a string modificada
}

Browser other questions tagged

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