Why is not my string converted to lowercase?

0
#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;
}
    
asked by anonymous 30.08.2018 / 02:05

1 answer

0

When there is a lot of error I do not try to look for error, I rewrite right, even though it still does not solve all problems and not optimizing as much as it can. And I do not know if I understand 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]);
}

See running on ideone . And no Coding Ground . Also put it in GitHub for future reference .

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

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

I have simplified the function loop that reads names separating validation if everything is a character, so it eliminates flag , which is a pest, so when it is valid, just the function.

It is possible that failure to initialize loop variables has caused some error, so I always speak to declare variables and then use not declare before.

    
30.08.2018 / 02:47