C programming error, how to make it work

Asked

Viewed 102 times

0

#include <C:\Users\Rosangela\Desktop\exercicios\bibliotecaFuncao.h>
#include<stdio.h>
#include<string.h>

void paraBaixo(char *nome)
{
    int i;
    for(i=0;i>strlen(*nome);i++)
    {
        *nome[i] + 32;// o erro é aqui pois diz que não é possivel usar inteiros, mas como faria sem ser dessa forma?
    }
}

main()
{
    char nome[25];

    printf("Insira um nome: \n");
    gets(nome);

    paraBaixo(&nome);
    printf("Nomo com tudo em minusculo: %s",nome);
}

1 answer

1


1) You are going through the string wrong way.

A for is executed as the logical condition of it, the second parameter, is true:

for (inicializacao ; condicao; passo)

right?

Now think: if yours i begins with 0, it will never be longer than the length of your string nome. Therefore, Voce needs to change to:

for (i = 0; i < strlen(nome); ++i)

2) Vectors in C (logo, strings, which are vectors of characters too) are already pointers implicitly. So, to use the string name in the function paraBaixo, Voce does not use the *. Also, when Voce does the nome[i] + 32, You need to keep it back on your own nome[i], since, otherwise, the result is 'thrown away':

void paraBaixo(char *nome)
{
    int i;
    for(i=0;i<strlen(nome);i++)
    {
         nome[i] = nome[i] + 32;
    }
}

3) Now only more 'stylistic' questions':

  • Declare the main as int main() and add a return 0; at the end of it. And a good habit in C;
  • Function gets and plundered and dangerous, so much so that my compiler issues this Warning when I compile your program: "Warning: the `gets' Function is Dangerous and should not be used.":

    scanf("%s", name);

or

fgets(nome, 25, stdin);

In the end, I recommend something like:

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

void paraBaixo(char *nome)
{
    int i;
    for(i=0;i<strlen(nome);i++)
    {
        nome[i] = nome[i] + 32;
    }
}

int main()
{
    char nome[25];

    printf("Insira um nome: \n");
    fgets(nome, 25, stdin);

    paraBaixo(nome);

    printf("Nomo com tudo em minusculo: %s\n",nome);
    return 0;
}
  • I understood thanks :D, I only have a doubt, for example here in the void parabaixo(char name) if I pass the argument being a normal string without the '(pointer)' it works normal so what is the need to pass the argument as pointer?

  • Very well explained! Note: The function paraBaixo is wrong, because to leave a small text you need to check before if the letter was uppercase. That is to say if(nome[i] <= 90) nome[i] = nome[i] + 32; (90 = Z to the decimal place).

  • @user11504 char *nome is a vector of characters. If you do not pass this way the function will find that it is a single character. @Peoplee as appears to be introductory course exercise think it is not very relevant to validate the data :P Even more when she could use the ready function of ctype. h XD

  • I’m going through without the char *nome and is functioning normally.

  • What do you mean without the parameter? Only char nome? You shouldn’t compile, since one char is not indexable.

  • only char nome no, I’m passing char nome[25], Is it wrong to do it this way? and another question I did not use the fgets because when I print it picks random character..., so I used the gets, the scanf of the very problem also try to avoid it.

  • Ah, and equivalent. No problem. The fgets and better pq Voce passes the maximum string size, but goes from your XD taste

  • Thanks really guy thanks, but the problem of fgets is that it picks random character when I go to print, that’s bad...

Show 3 more comments

Browser other questions tagged

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