Untitled
unknown
plain_text
a year ago
1.3 kB
8
Indexable
#include <stdio.h>
#include <string.h>
/*
Chiedere all'utente di inserire una password e verificare se è corretta
*/
// '\0'
int main() {
  
char str1[] = "Ciao va come?"; //13
char str2[] = "come"; //4
//Posizione:  0    1    2    3    4    5    6    7    8    9    10   11   12
//Caratteri: 'C'  'i'  'a'  'o'  ' '  'v'  'a'  ' '  'c'  'o'  'm'  'e'  '?'
int trovato = -1;
if(strlen(str1) < strlen(str2))
{
    printf("Impossibile cercare sottostringa");
}
else
{
    for(int i=0; i<= strlen(str1) - strlen(str2); i++)
    {
        int match = 1;
        
        for(int j=0; str2[j]!='\0'; j++)
        {
            if(str1[i+j] != str2[j])
            {
                match =0;
            }
        }
        
        if(match == 1)
        {
            trovato = i;
        }
        
    }
}
 
    return 0;
}
#include <stdio.h>
#include <string.h>
/*
Chiedere all'utente di inserire una password e verificare se è corretta
*/
// '\0'
int main() {
  
char str1[] = "Ciao va come?"; 
char str2[] = "come"; 
char *risultato = strstr(str1, str2);
if(risultato != NULL)
{
    int posizione = risultato - str1;
    printf("Sottostringa trovata in posizione: %d\n", posizione);
}else{
    printf("Sottostringa non trovata");
}
 
    return 0;
}Editor is loading...
Leave a Comment