quarta-feira, 3 de dezembro de 2014

Memcpy - extraindo substring


Se a finalidade da função memcpy do arquivo de cabeçalho string.h é
copiar um intervalo de memória de uma área de *origem para uma de *destino,
fica fácil usar uma  lógica bem aplicada para se extrair partes desta string,
e foi o que fiz neste código, que faz exatamente isto, extrai substring de uma string, na verdade é só usar a imaginação que mais coisas além destas
são capazes de serem feitas usando esta fantástica função.

Veja abaixo imagens do programa em execução:



Veja este claro exemplo abaixo no código:


#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 MEMCPY - EXTRAINDO SUBSTRING");
int i;do{Janela5();textbackground(WHITE);
textcolor(LIGHTRED);gotoxy(28,7);printf("MEMCPY - EXTRAINDO SUBSTRING");
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 Imp_Matr ( char *num ){
unsigned int i;
for( i = 0; i < strlen ( num ); i++ ){
if ( num [ i ] < '0' || num [ i ] > '9' )
return 0;
}
return 1;
}
int main (){
char str [ ] =  "ABCDEFGHIJLKMNOPQRSTUVXZYW";
int i, n, ini, fim, real;char num [ 3 ];
char Extr [ 27 ] ;
inicio();Janela5();textbackground(WHITE);
textcolor(LIGHTRED);gotoxy(28,3);printf("MEMCPY - EXTRAINDO SUBSTRING");
textcolor(LIGHTBLUE);gotoxy(7,5);printf("O tamanho da string ==> ");
textcolor(LIGHTRED);printf(" %s",  str );
textcolor(LIGHTBLUE);printf(" é igual a ");
textcolor(LIGHTRED);printf(" %d ",strlen ( str ) );
textcolor(LIGHTBLUE);gotoxy(7,7);
printf("Digite a posição para inicio da extração : ");
textcolor(LIGHTRED);scanf ("%d", &ini );fflush ( stdin );
textcolor(LIGHTBLUE);gotoxy(7,9);
printf("Digite a posição para o fim da extração : ");
textcolor(LIGHTRED);scanf ("%d", &fim );fflush ( stdin );
real = ( fim - ini );
#define re_al  real
do{
textcolor(LIGHTBLUE);gotoxy(7,11);
printf("Você está prestes a extrair uma string de ");
textcolor(LIGHTRED);printf(" %d ", re_al);
textcolor(LIGHTBLUE);printf(" posições " );
textcolor(BLACK);gotoxy(7,13);printf("Tem certeza que quer fazer isto ?");
textbackground(WHITE);
textcolor(LIGHTBLUE);gotoxy(23,15);printf("Escolha ");
textcolor(LIGHTRED);printf("[ 1 ] ");
textcolor(LIGHTBLUE);printf("para sim ou ");
textcolor(LIGHTRED);printf("[ 2 ] ");
textcolor(LIGHTBLUE);printf("Para não " );
textcolor(LIGHTRED);gets ( num );fflush(stdin);
n =  atoi (num);
if ( n < 1 || n > 2 ){
textcolor(LIGHTRED);gotoxy(25,10);
printf("\aNúmero Acima de 0 e menor que 2" );
getche();continue;
}
if( n == 2 ){
textcolor(LIGHTRED);gotoxy(35,21);printf("MUITO OBRIGADO");
Sleep(1800);exit(0);
}
if ( Imp_Matr ( num ) == 0 || n == 0){
textcolor(LIGHTGREEN);gotoxy(25,7);printf("\aVocê digitou : %s", num );
Sleep(1800);textcolor(LIGHTGREEN);gotoxy(25,9); printf("\a%s Não é Válido !", num );
Sleep(1800);continue;
}
memcpy ( Extr, str, sizeof ( Extr ) );
textcolor(LIGHTBLUE);gotoxy(23,17);printf("Veja abaixo a string extraida");
textcolor(LIGHTRED);gotoxy(25,19);
for( i = ini; i < fim; i++ ){
printf("%c", str [ i ] );
}break;
}while ( 1 );Sleep(1800);
textcolor(LIGHTRED);gotoxy(36,23);printf ("MUITO OBRIGADO");
getche();exit(0);
}