Function that converts string to lowercase and compares

Asked

Viewed 835 times

-5

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 above code should ask the user for a name, when calling the function verifica_usuario(&v); the program should convert the last string read to lowercase, duration a loop, convert the other strings (also to lowercase) and then compare and see if there are identical 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, it does not differentiate between upper and lower case. I have tried several ways to fix the bug of this highlighted function, but without success. I would like help to solve this problem.

  • To stricmp of maniero’s response solves your comparison problem by keeping the state of the string for later presentation

  • Taking advantage, goto? You are making a loop the wrong way. Try to make a flow chart of the data and then implement the proper loop. See more in this question on the subject: https://answall.com/q/251860/64969

  • @Jeffersonquesado, could guide me on how to perform this code in the right way, helping me how to do it, at least the function?

  • You understood Maniero’s answer?

  • I don’t get it, no.

  • Then why did you mark his answer as correct?

  • I’m new here.

  • Do the [tour], will be of great benefit

  • A, thanks for the tip.

  • If you do not understand a reply, please seek clarification from the author. If you don’t, the person who responded is convinced that you were able to help when you didn’t. This also avoids creating "repeated" questions by not realizing the answers

Show 5 more comments

1 answer

0


I did a program some time ago where the function is to perform the lower case -> upper case conversion of each letter of the user typed text. 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 show that compare 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();

}

Browser other questions tagged

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