Function that converts string to lowercase and compares [duplicate]

-4
struct registro{ /*Estrutura para guardar os dados do usuário*/
char usuario[50]; }atual[1000], *ptr; 
main() {
int v = 0; verific = 0; //posicao e variavel para comp. de string
volta_usuario:
printf("\n\t\t\tUsuário: ");
gets(atual[v].usuario);
verific = verifica_usuario(&v);
    if(verific == 0) {
        printf("\t\t\tUsuário já existente");
        goto volta_usuario;
    } v++; goto volta_usuario;} int verifica_usuario(int *ptr){
int i;
int cmp1;
int cmp2;
for(i = -1; i < *ptr; i++) {
    cmp1 = tolower(atual[*ptr].usuario);
    cmp2 = tolower(atual[i + 1].usuario);
    if(strcmp(cmp1,cmp2) == 0){
        return 0;
    }
}
return 1;}

The code above should ask the user for a name, when calling the function verifica_usuario(&v); the program should convert the last string read to lowercase and, lasts a loop, convert the other strings (also to lowercase), then compare and see if there are any matching strings. My goal is: if the person type "name" and then type "NAME" or "Name" (etc) the program must recognize that this user already exists, that is, does not differentiate between upper and lower case. I have tried several ways to fix the bug in this highlighted but unsuccessful function. I would like help solving this problem.

    
asked by anonymous 03.11.2017 / 03:59

1 answer

0

I made a program some time ago where the function is to perform the lowercase conversion - > capitalization of each letter of the text entered by the user. See the code:

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

int main( void ){

    char str1[30];
    int  tam,i;

    printf( "TEXTO:\n" );
    gets( str1 );

    tam = strlen( str1 );

    for( i = 0; i <= tam; i++ ){

        if( ( str1[i] >= 65 ) && ( str1[i] <= 90 ) )

            str1[i] = str1[i] + 32;

        else if( ( str1[i] >= 97 ) && ( str1[i] <= 132 ) )

            str1[i] = str1[i] - 32;

    }

    puts( str1 );

    getche();

}

I've also done a program that compares strings . See the code:

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

int main(void){

    char str1[20], str2[20], str3[20];
    int i, tam, tam2, comp;

    printf( "TEXTO:\n" );
    gets( str1 );

    printf( "TEXTO:\n" );
    gets( str2 );

    comp = strcmp( str1, str2 );

    if( comp == 0 ) printf("IGUAIS\n"); 
    else {

         strncat( str1, str2, 15 );
         printf( "DIFERENTES\nCONCATENADAS = %s\n", str1 );

    }

    getche();

}
    
04.11.2017 / 06:42