Programming error C, how to make it work

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);
}
    
asked by anonymous 12.09.2014 / 02:42

1 answer

1

1) You are traversing the string in the wrong way.

A for is executed while its logical condition, the second parameter, is true:

for (inicializacao ; condicao; passo)

Right?

Now think: if your i starts with 0 , it will never be greater than the length of your nome string. So, you need to switch to:

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

2) Vectors in C (so, strings, which are character vectors as well) are already pointers implicitly. So, to use the string name in the paraBaixo function, you do not use * . Also, when you do nome[i] + 32 , you need to save it back into nome[i] itself, otherwise the result is 'thrown out':

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

3) Now, just more 'stylistic' questions:

  • Declare main as int main() and add return 0; at the end of it. And a good habit in C ;
  • The gets function is deprecated and dangerous, both that my compiler issues this warning when I compile your program: "warning: the 'gets' function is dangerous and should not be used . " You'd better switch to one of the two:

    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;
}
    
12.09.2014 / 03:01