segunda-feira, 27 de outubro de 2014

Strncmp - Comparando string


Programadores de Linguagem C podem comparar duas string facilmente usando a
função strncmp(); da biblioteca string.h.
A função compara caractere a caractere, em pares, até que encontre diferenças
entre eles, zero é alcançado se as strings forem iguais.
Criei este código simples que faz a simulação de um sistema logado, onde o
usuário deve digitar a senha correta pra não ser reprovado.

Veja abaixo imagens do programa em execução:


Veja abaixo o código do programa:

#include <stdio.h>
#include <string.h>
#include <conio.h>
void Janela5 ( ) {
     system ( "Color F0" );
     int lin, col;
     for ( lin = 0; lin <= 25; lin++ ) {
         for ( col = 0; col <= 80; col++ ) {
              gotoxy ( col, lin );
              if ( lin == 2 ) {
                   textbackground ( LIGHTBLUE );
                   printf ( " " );
              }
              if ( col == 1 ) {
                   textbackground ( LIGHTBLUE );
                   printf ( " " );
              }
              if ( lin == 25 ) {
                   textbackground ( LIGHTBLUE );
              }
              if ( col == 80 ) {
                   textbackground ( LIGHTBLUE );
                   printf ( " " );
              }
         }
     }
     textbackground ( BLACK );
}
void inicio ( ) {
     system ( "title STRNCMP - COMPARANDO STRING" );
     int i;
     do {
         Janela5 ( );
         textbackground ( WHITE );
         textcolor ( LIGHTRED );
         gotoxy ( 31, 7 );
         printf ( "STRNCMP - COMPARANDO STRING" );
         textcolor ( BROWN );
         gotoxy ( 25, 10 );
         printf ( "Programa desenvolvido por:" );
         textcolor ( BLACK );
         gotoxy ( 52, 10 );
         printf ( "Samuel Lima" );
         textcolor ( BLUE );
         gotoxy ( 34, 12 );
         printf ( "sa_sp10@hotmail.com" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 24, 14 );
         printf ( "DIGITE    PARA SAIR OU   PARA CONTINUAR  " );
         textcolor ( LIGHTRED );
         gotoxy ( 32, 14 );
         printf ( "1" );
         textcolor ( LIGHTRED );
         gotoxy ( 47, 14 );
         printf ( "2" );
         gotoxy ( 41, 16 );
         scanf ( "%d", &i );
         fflush ( stdin );
         if ( i == 1 ) {
              textcolor ( LIGHTRED );
              gotoxy ( 35, 20 );
              printf ( "MUITO OBRIGADO" );
              Sleep ( 1800 );
              exit ( 0 );
         }
         if ( i == 2 ) {
              system ( "cls" );
              return;
         } else {
              textcolor ( LIGHTRED );
              gotoxy ( 37, 20 );
              printf ( "\aOPÇÃO ERRADA" );
              Sleep ( 1800 );
              system ( "cls" );
         }
     } while ( i );
}
int main ( ) {
     char senha [ 7 ] = "Caqui";
     char sen_ha [ 7 ];
     int res;
     inicio ( );
     do {
         system ( "cls" );
         Janela5 ( );
         textbackground ( WHITE );
         textcolor ( LIGHTRED );
         gotoxy ( 28, 3 );
         printf ( "STRNCMP - COMPARANDO STRING" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 28, 5 );
         printf ( "Digite a senha : " );
         textcolor ( BLACK );
         gets ( sen_ha );
         res = strncmp ( senha, sen_ha, 7 );
         textcolor ( BLACK );
         gotoxy ( 28, 7 );
         printf ( "Verificando" );
         textcolor ( LIGHTRED );
         printf ( " ..." );
         Sleep ( 1800 );
         if ( res > 0 ) {
              textcolor ( LIGHTBLUE );
              gotoxy ( 28, 7 );
              printf ( "A senha está errada" );
              Sleep ( 1800 );
              textcolor ( LIGHTBLUE );
              gotoxy ( 28, 9 );
              printf ( "\aTente novamente" );
              Sleep ( 1800 );
              continue;
         } else if ( res < 0 ) {
              textcolor ( LIGHTBLUE );
              gotoxy ( 28, 7 );
              printf ( "\aA senha está errada" );
              Sleep ( 1800 );
              textcolor ( LIGHTBLUE );
              gotoxy ( 28, 9 );
              printf ( "\aTente novamente" );
              Sleep ( 1800 );
              continue;
         } else textcolor ( LIGHTBLUE );
         gotoxy ( 28, 7 );
         printf ( "A senha está correta" );
         Sleep ( 1800 );
         break;
     } while ( 1 );
     Sleep ( 1800 );
     textcolor ( LIGHTRED );
     gotoxy ( 35, 22 );
     printf ( "MUITO OBRIGADO" );
     getche ( );
     exit ( 0 );
     return ( 0 );
}

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.