-2
Good morning, good afternoon, good evening. I’m trying to make a statement from my C programming course that asks me to make a code that checks if a string2 is contained in a string1, using pointer arithmetic, I’ve tried in many ways to perform this exercise, but the only way I’ve found to do itlo is based on the method I will send here, but when executing the code I can verify that it contains only the first letters of string1, for example: if I type "brazil" for the first string and type "bra" for the second, it detects that it is contained, but if I type "brazil" for the first and "Sil" for the second, it does not detect that it is contained, I would like to know how to solve this problem. Thank you, I await your reply. Note: The statement is as a comment in the code, right below the libraries.
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>
/*
18) Elabora uma função que receba duas strings como parâmetros e
verifique se a segunda string está dentro da primeira. Para isso, utilize
apenas aritmética de ponteiros.
*/
void verifica(const char* ptr1,const char* ptr2) {
int l=0;
for (size_t i = 0; i < strlen(ptr1); i++) {
if (ptr1[i]==ptr2[i]) {
l++;
}//if
}//for
if (l == strlen(ptr2)) {
printf("Está contida!! \n");
}else{
printf("Não está contida!! \n");
}//ifelse
}//verifica
int main (){
setlocale(LC_ALL, "Portuguese");
char str1[21], str2[21];
char *ptr[2] = {str1, str2};
for (size_t i = 0; i < 2; i++) {
printf("Digite a %dº palavra de até 20 caracteres: ", i + 1);
fgets(ptr[i], 21, stdin);
ptr[i][strcspn(ptr[i],"\n")]='\0';
setbuf(stdin,NULL);
}//for
verifica(ptr[0],ptr[1]);
return 0;
}//main
vlw, thank you very much!!!
– filipeoioi