segunda-feira, 7 de setembro de 2020

STRCHR - Pesquisando caractere numa string

Esta função varre uma string na direção frontal
em busca da primeira ocorrência de um caractere.
strchr retorna um ponteiro para a primeira ocorrência
do caractere procurado na string,
se o caractere não ocorre na string, strchr retorna nulo.
Leve em conta o terminador nulo como parte da string.




Veja abaixo o código do programa:

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

void Janela ( ) {
int l, c;
for ( l = 1; l <= 30 ; l++ ) {
         for ( c = 1; c < 80 ; c++ ) {
              gotoxy ( c , l );
  if ( l == 2 ) {
   cprintf ( " " );
  }
  if ( c == 1 ) {
   cprintf ( "  " );
              }
              if ( c == 79 ) {
   textattr ( 200 );
   cprintf ( "  " );
  }
}
}
}
//------------------------------------------------------------------------------
void Pesq_Caractere ( char str [ 20 ], char c );
//------------------------------------------------------------------------------
void Informe ( ) {
         textcolor ( LIGHTBLUE );
gotoxy ( 26, 19 );
cprintf ( "Por: " );
textcolor ( LIGHTMAGENTA );
cprintf ( "Samuel Lima" );
         textcolor ( WHITE );
gotoxy ( 26, 20 );
cprintf ( "sa_sp10@hotmail.com" );
         Sleep ( 1800 );
textcolor ( LIGHTRED );
gotoxy ( 37, 23 );
cprintf ( "MUITO OBRIGADO" );
}
//------------------------------------------------------------------------------
void Digita_Caractere ( char str [ 20 ], char c ) {
system ( "cls" );
Janela ( );
textbackground ( BLACK );
textcolor ( LIGHTRED );
gotoxy ( 20 , 3 );
cprintf ( "STRCHR - PESQUISANDO CARACTERE NUMA STRING" );
textcolor ( LIGHTBLUE );
         gotoxy ( 28 , 5 );
cprintf ( "Digite uma Palavra : " );
         textcolor ( LIGHTRED );
         scanf ( "%s" , str );
         fflush ( stdin );
textcolor ( LIGHTBLUE );
         gotoxy ( 28 , 7 );
cprintf ( "Veja abaixo a Palavra digitada " );
         textcolor ( LIGHTRED );
         gotoxy ( 28 , 9 );
cprintf ( "%s" , str );
Sleep ( 1800 );
Pesq_Caractere ( str, c );
}
//------------------------------------------------------------------------------
void Pesq_Caractere ( char str [ 20 ] , char c ) {
char *pch;
int op;
do {
system ( "cls" );
Janela ( );
textbackground ( BLACK );
textcolor ( LIGHTRED );
gotoxy ( 20 , 3 );
cprintf ( "STRCHR - PESQUISANDO CARACTERE NUMA STRING" );
textcolor ( LIGHTBLUE );
gotoxy ( 28 , 5 );
cprintf ( "Digite um Caractere : " );
textcolor ( LIGHTRED );
scanf ( "%c" , &c );
fflush ( stdin );
pch = strchr ( str , c );
if ( pch != NULL ) {
         textcolor ( LIGHTBLUE );
gotoxy ( 18 , 7 );
cprintf ( "O Caractere " );
                 textcolor ( LIGHTRED );
cprintf ( "%c" , c );
textcolor ( LIGHTBLUE );
cprintf ( " foi encontrado na " );
                 textcolor ( LIGHTRED );
cprintf ( "%dª" , pch - str );
textcolor ( LIGHTBLUE );
cprintf ( " posição " );
                 textcolor ( LIGHTBLUE );
cprintf ( " da Palavra" );
Sleep ( 1800 );

textcolor ( LIGHTRED );
gotoxy ( 18, 11 );
cprintf ( "Escolha uma opção abaixo" );

textcolor ( LIGHTBLUE );
gotoxy ( 18, 13 );
cprintf ( "1 - Pesquisar outro caractere na mesma palavra" );

gotoxy ( 18, 15 );
cprintf ( "2 - Digitar outra palavra" );

gotoxy ( 18, 17 );
cprintf ( "3 - Finalizar o programa" );
                 Informe ( );
textcolor ( LIGHTRED );
                 gotoxy ( 43, 17 );
scanf ( "%d" , &op );
fflush ( stdin );

if ( op == 1 ) {
Pesq_Caractere ( str, c );
}
if ( op == 2 ) {
  Digita_Caractere ( str, c );
}
if ( op == 3 ) {
             exit ( 0 );
}
} else {
textcolor ( LIGHTBLUE );
                 gotoxy ( 18 , 13 );
cprintf ( "O Caractere " );
                 textcolor ( LIGHTRED );
cprintf ( "%c" , c );
                 textcolor ( LIGHTBLUE );
cprintf ( " não foi encontrado na Palavra" );
                 Sleep ( 1800 );
                 textcolor ( LIGHTBLUE );
                 gotoxy ( 28 , 15 );
                 cprintf ( "\aTente novamente" );
Sleep ( 1800 );
Pesq_Caractere ( str, c );
getche ( );
}
Sleep ( 1800 );
} while ( true );
}
//------------------------------------------------------------------------------
int main ( ) {
system ( "title STRCHR - PESQUISANDO CARACTERE NUMA STRING" );
char c;
char str [ 20 ];
Digita_Caractere ( str, c );
}
//------------------------------------------------------------------------------

Nenhum comentário:

Postar um comentário

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