Why is my string not converted to lowercase?

Asked

Viewed 80 times

-1

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


/* 
Lucas Correia
Síntese
Objetivo:Ler o nome de duas pessoas e colocar o primeiro letras maiúsculas e o segundo, em letras minúsculas 
Entrada:Dois nomes
Saida:Primeiro nome em letras maiúsculas e o segundo em minúsculas
 */

#define QTD_NOMES 2
#define TAM_NOME 30

char * leValidaNome();
int main(int argc, char *argv[]) {
    int i=0, j=0;
    char nomes[QTD_NOMES][TAM_NOME];


    for(i=0; i < QTD_NOMES; i++){
        strcpy(nomes[i], leValidaNome());
    }

    for(i=0; i < QTD_NOMES; i++){
        if(i==0){
            for(j; j < strlen(nomes[i]); j++){
                nomes[i][j] = toupper(nomes[i][j]);

            }
        }else{
            for(j; j < strlen(nomes[i]); j++){
                nomes[i][j] = tolower(nomes[i][j]);
            }
        }

    }

    printf("%s\n", nomes[0]);
    printf("\t%s", nomes[1]);

    return 0;
}
char * leValidaNome(){
    char nome[30], *pNome;
    int cont=0, flag = 1;
    do{
        printf("Informe seu nome:");
        scanf(" %[^\n]s", nome);

        if(strlen(nome) == 0){
            printf("\nNao invalido!\nDigite algo!\n");
            flag = 0;
        }else{
            for(cont; cont < strlen(nome); cont++){
                if(isdigit(nome[cont]) != 0){
                    printf("\nNome invalido!\nDigite apenas letras!\n");
                    flag = 0;
                    break;
                }else{
                    flag = 1;
                }
            }
        }

    }while(!flag);
    return pNome = nome;
}

1 answer

0


When there is too much error I do not try to look for error, I rewrite right, even not solving all problems and not optimizing as much as it can. And I do not know if I understood the problem, it is not described and the code may have indicated something else:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define QTD_NOMES 2
#define TAM_NOME 30

int soChars(char *nome, int tamanho) {
    for (int i = 0; i < tamanho; i++) {
        if (isdigit(nome[i]) != 0) {
            printf("\nNome invalido!\nDigite apenas letras!\n");
            return 0;
        }
    }
    return 1;
}

void leValidaNome(char *nome) {
    while (1) {
        printf("Informe seu nome:");
        scanf(" %[^\n]s", nome);
        int tamanho = strlen(nome);
        if (tamanho == 0) printf("\nNome invalido!\nDigite algo!\n");
        else if (soChars(nome, tamanho)) return;
    }
}

int main() {
    char nomes[QTD_NOMES][TAM_NOME];
    for (int i = 0; i < QTD_NOMES; i++) leValidaNome(nomes[i]);
    for (int j = 0; j < strlen(nomes[0]); j++) nomes[0][j] = toupper(nomes[0][j]);
    for (int i = 1; i < QTD_NOMES; i++) {
        for (int j = 0; j < strlen(nomes[i]); j++) nomes[i][j] = tolower(nomes[i][j]);
    }
    printf("\n%s\n%s", nomes[0], nomes[1]);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Part of the problem is the code is too confusing and complicated, it can be simpler and better written.

The allocation was done inefficiently and incorrectly, as it allocated the space for all words of main() just use this address for any write function.

I simplified the function loop that reads names by separating the validation if everything is character, thus eliminates flag, which is a plague, so when it is valid, only the function.

It is possible that the fact of not initializing the variables of the loops has generated some error, why I always say to declare variables and then use not to declare before.

  • Thanks, Maniero! I had already managed to do it, but thanks for the touch. I’ll take a look at the things I did wrong or inefficiently.

  • @Lucascorrhea do you know you can vote for everything on the site? See the [tour]

Browser other questions tagged

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