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':
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?
– user11504
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 sayif(nome[i] <= 90) nome[i] = nome[i] + 32;
(90 = Z to the decimal place).– Peoplee
@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– Lucas Virgili
I’m going through without the
char *nome
and is functioning normally.– user11504
What do you mean without the parameter? Only
char nome
? You shouldn’t compile, since onechar
is not indexable.– Lucas Virgili
only
char nome
no, I’m passingchar 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.– user11504
Ah, and equivalent. No problem. The
fgets
and better pq Voce passes the maximum string size, but goes from your XD taste– Lucas Virgili
Thanks really guy thanks, but the problem of
fgets
is that it picks random character when I go to print, that’s bad...– user11504