// Naive String Matching Algorithm
/*
MIT License
Copyright (c) 2022 Guilherme Araújo Brasil and João Bruno Rodrigues de Freitas
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
//
// ------- HEADER -------------------------------------------------------------------
// standard library header C
#include <stdio.h>
// Header file that provides C programming language standard library functions,
// macros, and definitions for manipulating strings and memory regions.
#include <string.h>
// ----------------------------------------------------------------------------------
//
// ------- NAIVE STRING MATCHING ALGORITHM ------------------------------------------
/*
The alghoritm below is a searching software of strings.
The software will read a text and search a pattern previously registred by user.
Pass the pattern over the text one by one and check for a match. If a match is found,
swipe 1 again to check for subsequent matches.
This alghoritm is implemented in browsers, text files and, sometimes, in
password systems.
input:
char txt[904] <- char pointer txt up to 904 characters <- string with up to 904 characters <-
char pat[] <- char pointer pattern smaller than len_txt <- string less than length of string txt(char txt[904]).
output: Indexes of the txt string where the pattern was found.
*/
char txt[] =
"Parte 2: Apresentação ATIVIDADES, T1 (parte2):Implementar e validar o algoritmo na plataforma Linux.Se possível, valide sua implementação na plataforma Raspberry PI.Fazer apresentação dos resultados.Fazer demonstração do funcionamento, mostrando os testes.12 min para apresentação/demonstração. (25 e 28.11).ENTREGA:Slide da apresentação em PDF.ASPECTOS A SEREM AVALIADOS:Funcionamento correto [20%]Grau de dificuldade do algoritmo escolhido [20%]Procedimentos de teste e validação [10%]Clareza do código para fins de reuso [10%]Conhecimento/Segurança dos conceitos durante a apresentação/demonstração. [40%](O trabalho é em equipe, mas AS NOTAS SÃO INDIVIDUAIS)SUGESTÃO DE TÓPICOS PARA APRESENTAÇÃO [tempo sugerido]:- Descrição resumida do(s) algoritmo(s) e da(s) estrutura(s) de dados usados - dkaslkdlakdlakdlakdlaasd,msaldmaplsdm,lpa,dlsal,lasdkfmskldmfksmdkfmskmdfk904";
String pattern;
char pat[20]; // Receive the pattern string
int len_txt = strlen(txt); // len_txt gets the text's length
void naive_string_matching(char *pat, char *txt) {
int len_pattern = strlen(pat); // len_pattern gets the pattern's length
int i;
int j;
/* Reading pattern's characters one by one */
for (i = 0; i <= len_txt - len_pattern; i++) {
/* Matching character analysed and file character */
for (j = 0; j < len_pattern; j++){
if (txt[i + j] != pat[j]){
break;
}
}
if (j == len_pattern){
Serial.print("Pattern founded in index: ");
Serial.println(i);
}
}
}
// ------------------------------------------------------------------------------------
//
// ------- MAIN -----------------------------------------------------------------------
void setup() {
Serial.begin(9600);
int len; // Pattern length
//Code - User Relationship
Serial.println("Enter the pattern to search:");
delay(10000); // Time to type
pattern = Serial.readString();
len = pattern.length();
pattern.toCharArray(pat, len);
Serial.print("Pattern searched ");
Serial.println(pattern);
delay(1000); // Time to answer
//Code - User Relationship
if(pattern != 0){
naive_string_matching(pat, txt); // Called function: naive_string_matching
}
}
void loop() {
}
// ----------------------------------------------------------------------------------------------
/*
MIT License
Copyright (c) 2022 Guilherme Araújo Brasil and João Bruno Rodrigues de Freitas
*/