segunda-feira, 7 de setembro de 2015

Caracteres - pesquisando e removendo ocorrências

Localizar e remover ocorrências de um caractere numa frase previamente digitada pelo usuário
é a finalidade deste código didático.
O programa está funcionando perfeitamente aqui no meu eclipse, e aposto que funcionará
com perfeição na sua IDE favorita, portanto aproveite este ótimo trabalho dedicado a estudantes
de linguagens C.

Veja abaixo imagens do programa em execução:





Veja abaixo o código do programa:

#include <stdio.h>
#include <conio.h>
void Janela5 ( ) {
     int lin, col;
     col = 0;
     system ( "Color 40" );
     for ( lin = 2; lin <= 24; lin++ )
         for ( col = 2; col <= 78; col++ ) {
              gotoxy ( col, lin );
              textbackground ( LIGHTCYAN );
              printf ( " " );
         }
}
int continuando ( ) {
     int nr;
     do {
         system ( "Color 90" );
         Janela5 ( );
         textcolor ( LIGHTRED );
         gotoxy ( 20, 3 );
         printf ( "CARACTERES - PESQUISANDO E REMOVENDO OCORRÊNCIAS " );
         textcolor ( BROWN );
         gotoxy ( 25, 7 );
         printf ( "Programa desenvolvido por:" );
         textcolor ( GREEN );
         gotoxy ( 52, 7 );
         printf ( "Samuel Lima" );
         textcolor ( BLACK );
         gotoxy ( 34, 9 );
         printf ( "sa_sp10@hotmail.com" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 23, 11 );
         printf ( "Digite " );
         textcolor ( LIGHTRED );
         printf ( " 0 " );
         textcolor ( LIGHTBLUE );
         printf ( " para sair ou " );
         textcolor ( LIGHTRED );
         printf ( " 1 " );
         textcolor ( LIGHTBLUE );
         printf ( " para continuar " );
         textcolor ( LIGHTRED );
         gotoxy ( 24, 13 );
         scanf ( "%d", &nr );
         fflush ( stdin );
         if ( nr == 0 ) {
              textcolor ( LIGHTRED );
              gotoxy ( 36, 18 );
              printf ( "MUITO OBRIGADO" );
              getche ( );
              exit ( 0 );
         } else if ( nr == 1 ) {
              return 1;
         }
         textcolor ( BLACK );
         gotoxy ( 36, 16 );
         printf ( "\aopcão errada!" );
         getche ( );
     } while ( 1 );
     return 0;
}
int main ( ) {
     system ( "title CARACTERES - PESQUISANDO E REMOVENDO OCORRÊNCIAS" );
     continuando ( );
     system ( "cls" );
     Janela5 ( );
     unsigned int cont, i, j, lenv;
     char *st_r;
     st_r = ( char* ) malloc ( 10 * sizeof(char) );
     char str [ 255 ];
     textcolor ( LIGHTRED );
     gotoxy ( 20, 3 );
     printf ( "CARACTERES - PESQUISANDO E REMOVENDO OCORRÊNCIAS " );
     textcolor ( LIGHTBLUE );
     gotoxy ( 7, 5 );
     printf ( "Digite uma frase ==> " );
     textcolor ( BLACK );
     gets ( str );
     textcolor ( LIGHTBLUE );
     gotoxy ( 7, 7 );
     printf ( "Frase digitada ==> " );
     textcolor ( BLACK );
     printf ( " %s", str );
     do {
         textcolor ( LIGHTBLUE );
         gotoxy ( 7, 9 );
         printf ( "Digite um caracter  para ser removido ==> " );
         textcolor ( LIGHTRED );
         gets ( st_r );
         lenv = strlen ( st_r );
         cont = 0;
         for ( i = 0; i <= strlen ( str ); i++ )
              for ( j = 0; j <= strlen ( st_r ); j++ )
                   if ( str [ i ] == st_r [ j ] && str [ i ] != 0 )
                        cont++;

         if ( cont == 0 ) {
              textcolor ( LIGHTBLUE );
              gotoxy ( 7, 13 );
              printf ( "Não há Nenhuma ocorrência de " );
              textcolor ( LIGHTRED );
              printf ( "%s", st_r );
              textcolor ( LIGHTBLUE );
              printf ( " na frase digitada" );
              Sleep ( 1800 );
              textcolor ( LIGHTRED );
              gotoxy ( 7, 15 );
              printf ( "Pressione qualquer tecla para tentar outra vez" );
              getche ( );
              gotoxy ( 7, 13 );
              clreol ( );
              gotoxy ( 30, 15 );
              clreol ( );
              gotoxy ( 7, 15 );
              clreol ( );
              gotoxy ( 42, 9 );
              clreol ( );
         }
     } while ( cont == 0 );
     textcolor ( LIGHTBLUE );
     gotoxy ( 7, 11 );
     printf ( "No nome ==> " );
     textcolor ( BLACK );
     printf ( "%s ", str );
     textcolor ( LIGHTBLUE );
     gotoxy ( 7, 13 );
     printf ( "Foram encontrados " );
     textcolor ( LIGHTRED );
     printf ( " %d ", cont );
     textcolor ( LIGHTBLUE );
     printf ( " Caracteres" );
     textcolor ( LIGHTRED );
     printf ( " %s ", st_r );
     textcolor ( LIGHTBLUE );
     gotoxy ( 7, 15 );
     printf ( "Mostrando abaixo o Nome ==> " );
     textcolor ( BLACK );
     printf ( "%s", str );
     textcolor ( LIGHTBLUE );
     gotoxy ( 7, 17 );
     printf ( "Sem seus " );
     textcolor ( LIGHTRED );
     printf ( " %d ", cont );
     textcolor ( LIGHTBLUE );
     printf ( " caracteres " );
     textcolor ( LIGHTRED );
     printf ( " %s ", st_r );
     textcolor ( LIGHTBLUE );
     printf ( " que foram removidos " );
     textcolor ( BLACK );
     gotoxy ( 7, 19 );
     for ( i = 0; i < strlen ( str ); i++ ) {
         for ( j = 0; j < lenv; j++ )
              if ( str [ i ] == st_r [ j ] )
                   break;
         if ( j == lenv ) {
              printf ( " %c", str [ i ] );
         }
     }
     Sleep ( 1000 );
     textcolor ( LIGHTRED );
     gotoxy ( 36, 23 );
     printf ( "MUITO OBRIGADO" );
     getche ( );
     return ( 0 );
}

Nenhum comentário:

Postar um comentário

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