Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Soluzione dell'esame di febbraio 2012, usando le liste anche se non servono.
- * Perché a qualcuno piacciono tanto.
- */
- #include <stdio.h>
- #include <stdbool.h>
- #include <stdlib.h>
- #include <string.h>
- struct node {
- char info;
- struct node* next;
- };
- typedef struct node* Paroliere;
- bool cercaEdElimina(Paroliere paroliere, char c) {
- Paroliere fake = &(struct node){ .next = paroliere };
- for (; fake->next != NULL; fake = fake->next)
- if (fake->next->info == c) {
- Paroliere temp = fake->next->next;
- free(fake->next);
- fake->next = temp;
- return true;
- }
- return false;
- }
- int contaOccorrenze(Paroliere paroliere, char *algoritmi) {
- int i, occ = 0;
- while (true) {
- for (i = 0; i < strlen(algoritmi); i++) {
- // printf("%c ", algoritmi[i]);
- if (!cercaEdElimina(paroliere, algoritmi[i]))
- return occ;
- }
- occ++;
- }
- }
- /***** Metodi di test *****/
- Paroliere paroliereDaStringa(char *stringa) {
- size_t length = strlen(stringa);
- if (length == 0)
- return NULL;
- Paroliere array = calloc(length, sizeof(struct node));
- for (int i = 0; i < length-1; i++) {
- array[i].info = stringa[i];
- array[i].next = &array[i+1];
- }
- array[length-1].info = stringa[length-1];
- return array;
- }
- void stampaParoliere(Paroliere paroliere) {
- printf("Paroliere: \"");
- for (; paroliere != NULL; paroliere = paroliere->next)
- putchar(paroliere->info);
- printf("\"\n");
- }
- int main(int argc, char *argv) {
- Paroliere p = paroliereDaStringa("belladipadella");
- stampaParoliere(p);
- printf("Occorrenze: %d\n\n", contaOccorrenze(p, "algoritmi"));
- p = paroliereDaStringa("belladipadellagormiti");
- stampaParoliere(p);
- printf("Occorrenze: %d\n", contaOccorrenze(p, "algoritmi"));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment