quarta-feira, 29 de outubro de 2014

Memchr - Pesquisando caractere numa string


Pesquisar caractere numa palavra digitada é tarefa muito fácil usando esta
função memchr();
cuja sintaxe é: void * memchr ( const void *, int, size_t );
A pesquisa é realizada começando pelos primeiros bytes num bloco de memória
apontado por por uma variável que contém a string digitada.
Já na primeira ocorrência do caractere encontrado, que são obviamente
comparados, um ponteiro é retornado, e termina a verificação mostrando o
caractere encontrado. Se caso não houver nenhuma ocorrências, no bloco de
memória a função retorna um valor nulo.

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 Apagaclreol ( ) {
     gotoxy ( 18 , 13 );
     clreol ( );
     gotoxy ( 28 , 15 );
     clreol ( );
     gotoxy ( 50 , 11 );
     clreol ( );
}
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 Digita_Caractere ( char str [ 20 ] , char str1 , int *c );
void inicio ( char str [ 20 ] , char str1 , int *c ) {
     system ( "title MEMCHR - PESQUISANDO CARACTERE NUMA STRING" );
     system ( "cls" );
     int i;
     do {
         Janela5 ( );
         textbackground ( WHITE );
         textcolor ( LIGHTRED );
         gotoxy ( 23 , 7 );
         printf ( "MEMCHR - PESQUISANDO CARACTERE NUMA 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" );
              Digita_Caractere ( str , str1 , c );
         } else {
              textcolor ( LIGHTRED );
              gotoxy ( 37 , 20 );
              printf ( "\aOPÇÃO ERRADA" );
              Sleep ( 1800 );
              system ( "cls" );
         }
     } while ( i );
}
void Pesq_Caractere ( char str [ 20 ] , char str1 , int *c );
void Digita_Caractere ( char str [ 20 ] , char str1 , int *c ) {
     system ( "cls" );
     Janela5 ( );
     textbackground ( WHITE );
     textcolor ( LIGHTRED );
     gotoxy ( 23 , 3 );
     printf ( "MEMCHR - PESQUISANDO CARACTERE NUMA STRING" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 28 , 5 );
     printf ( "Digite uma Palavra : " );
     textcolor ( LIGHTRED );
     scanf ( "%s" , str );
     fflush ( stdin );
     textcolor ( LIGHTBLUE );
     gotoxy ( 28 , 7 );
     printf ( "Veja abaixo a Palavra digitada " );
     textcolor ( LIGHTRED );
     gotoxy ( 28 , 9 );
     printf ( "%s" , str );
     Sleep ( 1800 );
     Pesq_Caractere ( str , str1 , c );
}
void Pesq_Caractere ( char str [ 20 ] , char str1 , int *c ) {
     char *pch;
     textcolor ( LIGHTBLUE );
     gotoxy ( 28 , 11 );
     printf ( "Digite um Caractere : " );
     textcolor ( LIGHTRED );
     scanf ( "%c" , &str1 );
     fflush ( stdin );
     *c = str1;
     pch = ( char* ) memchr ( str , *c , strlen ( str ) );
     if ( pch != NULL ) {
         textcolor ( LIGHTBLUE );
         gotoxy ( 18 , 13 );
         printf ( "O Caractere " );
         textcolor ( LIGHTRED );
         printf ( "%c" , str1 );
         textcolor ( LIGHTBLUE );
         printf ( " foi encontrado na posição " );
         textcolor ( LIGHTRED );
         printf ( "%d" , pch - str );
         textcolor ( LIGHTBLUE );
         printf ( " da Palavra" );
         Sleep ( 1800 );
         textcolor ( LIGHTRED );
         gotoxy ( 35 , 22 );
         printf ( "MUITO OBRIGADO" );
         Sleep ( 1800 );
         exit ( 0 );
     } else {
         textcolor ( LIGHTBLUE );
         gotoxy ( 18 , 13 );
         printf ( "O Caractere " );
         textcolor ( LIGHTRED );
         printf ( "%c" , str1 );
         textcolor ( LIGHTBLUE );
         printf ( " não foi encontrado na Palavra" );
         Sleep ( 1800 );
         textcolor ( LIGHTBLUE );
         gotoxy ( 28 , 15 );
         printf ( "\aTente novamente" );
         Sleep ( 1800 );
         Apagaclreol ( );
         Pesq_Caractere ( str , str1 , c );
         getche ( );
     }
}
int main ( ) {
     int *c;
     char str [ 20 ];
     char str1;
     textbackground ( WHITE );
     inicio ( str , str1 , c );
}

Nenhum comentário:

Postar um comentário

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