sábado, 12 de setembro de 2015

Caracteres : pesquisando - removendo e substituindo ocorrências

No pouco tempo que tenho estive pensando por aqui:
Ora, se meu programa recebe caracteres digitados pelo usuário, faz uma eficiente pesquisa,
e achando-o caracter pedido faz a remoção das ocorrências daquele caracter, por que não
colocar mais um recurso permitindo ao usuário recolocar o caracter removido em todas as suas
ocorrências ou até mesmo de inserir um novo caracter em todas as posições que foram removidas?
E foi isto que fiz, e disponibilizo-o gratuitamente para todos interessados em linguagem 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 00" );
     for ( lin = 2; lin <= 34; lin++ )
         for ( col = 2; col <= 78; col++ ) {
              gotoxy ( col, lin );
              textbackground ( YELLOW );
              printf ( " " );
         }
}
char Subs_titui ( char *s, char c, char x ) {
     char *p;
     for ( p = s; *p != '\0'; p++ )
         if ( *p == c ) {
              *p = x;
         }
     return *p;
}
int main ( ) {
     system (
              "title CARACTERES : PESQUISANDO - REMOVENDO"
              " E SUBSTITUINDO OCORRÊNCIAS" );
     unsigned int cont, d, i, j, lenv;
     char *st_r, y;
     st_r = ( char* ) malloc ( 10 * sizeof(char) );
     char str [ 255 ];
     Janela5 ( );
     textcolor ( LIGHTRED );
     gotoxy ( 11, 3 );
     printf ( "INTEIROS : PESQUISANDO - REMOVENDO"
               " E SUBSTITUINDO 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" );
     Sleep ( 1800 );
     textcolor ( LIGHTRED );
     gotoxy ( 30, 23 );
     printf ( "PRESSIONE QUALQUER TECLA" );
     getche ( );
     gotoxy ( 25, 7 );
     clreol ( );
     gotoxy ( 52, 7 );
     clreol ( );
     gotoxy ( 34, 9 );
     clreol ( );
     gotoxy ( 30, 23 );
     clreol ( );
     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 ] );
         }
     }
     getche ( );
     do {
         textcolor ( LIGHTBLUE );
         gotoxy ( 7, 21 );
         printf ( "Você removeu todas as ocorrências do caracter " );
         i = 0;
         textcolor ( LIGHTRED );
         printf ( " %c ", st_r [ i ] );
         textcolor ( LIGHTBLUE );
         gotoxy ( 7, 23 );
         printf ("Deseja preencher as ocorrências "
                   "removidas com um novo caracter ?" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 7, 25 );
         textcolor ( LIGHTRED );
         printf ( "1 " );
         textcolor ( LIGHTBLUE );
         printf ( "- Sim, desejo preencher as ocorrências removidas " );
         textcolor ( LIGHTRED );
         gotoxy ( 7, 26 );
         printf ( "2 " );
         textcolor ( LIGHTBLUE );
         printf ( "- Não, prefiro finalizar o programa assim mesmo " );
         textcolor ( LIGHTRED );
         scanf ( "%d", &d );
         fflush ( stdin );
         if ( d == 1 ) {
              break;
         }
         if ( d == 2 ) {
              Sleep ( 1000 );
              textcolor ( LIGHTRED );
              gotoxy ( 36, 32 );
              printf ( "MUITO OBRIGADO" );
              getche ( );
              exit ( 0 );
         }
         textcolor ( BLACK );
         gotoxy ( 36, 28 );
         printf ( "\aopcão errada!" );
         getche ( );
         gotoxy ( 36, 28 );
         clreol ( );
         gotoxy ( 50, 26 );
         clreol ( );
     } while ( 1 );
     textcolor ( LIGHTBLUE );
     gotoxy ( 7, 28 );
     printf ( "Digite o novo caracter para fazer a substituição " );
     textcolor ( LIGHTRED );
     scanf ( "%c", &y );
     fflush ( stdin );
     i = 0;
     Subs_titui ( str, st_r [ i ], y );
     textcolor ( LIGHTBLUE );
     gotoxy ( 7, 30 );
     printf ( "Os caracteres foram substituidos"
               " com sucesso veja abaixo " );
     textcolor ( BLACK );
     gotoxy ( 7, 32 );
     for ( i = 0; i < strlen ( str ); i++ ) {
         printf ( " %c ", str [ i ] );
     }
     Sleep ( 1000 );
     textcolor ( LIGHTRED );
     gotoxy ( 36, 34 );
     printf ( "MUITO OBRIGADO" );
     getche ( );
     free ( st_r );
     return ( 0 );
}

Nenhum comentário:

Postar um comentário

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