#include #define MAX 30 // lunghezza massima di una stringa int mainA(); int mainB(); int mainC(); int mainD(); int mainE(); int mainF(); int main(){ return mainA(); // esegue mainA() } /********** ESERCIZIO 1.a *****************/ /* Restituisce la lunghezza di w (numero di caratteri di w) */ int strLength(char *w){ return -1; } int mainA(){ printf("strLength(\"cane\") = %d\n", strLength("cane")); printf("strLength(\"asino\") = %d\n", strLength("asino")); printf("strLength(\"\") = %d\n", strLength("")); printf("strLength(\"ape\") = %d\n", strLength("ape") ); printf("strLength(\"ape\"+1) = %d\n" , strLength("ape"+1) ); printf("strLength(\"ape\"+2) = %d\n" , strLength("ape"+2) ); printf("strLength(\"ape\"+3) = %d\n" , strLength("ape"+3) ); // printf("strLength(\"ape\"+4) = %d\n" , strLength("ape"+4) ); // cosa stampa ? return 0; } /********** ESERCIZIO 1.b *****************/ /* Copia in t (target) la stringa s (source). Si assume che t contenga spazio sufficiente per contenere s */ void strCopy(char *t, char *s){ } int mainB(){ char input[MAX+1]; // dimensione necessaria per memorizzare stringa di MAX caratteri char w[MAX+1]; printf("Inserire parola ---> "); scanf("%s", input); // legge da standard input una stringa e la memorizza nell'array w1[] strCopy(w,input); printf("strCopy(w,\"%s\") ---> w = %s\n", input, w); return 0; } /********** ESERCIZIO 1.c *****************/ /* word e' la parola ottenuta aggiungendo alla parola source il carattere c Si assume che word contenga spazio sufficiente per contenere il risultato. */ void addChar(char *word, char *source, char c){ // Usare funzioni strLength() e strCopy() } int mainC(){ char w[MAX+1]; addChar(w, "cavall", 'o'); printf("addChar(w, \"cavall\", 'o') ---> w = %s\n", w); addChar(w, "ap", 'e'); printf("addChar(w, \"ap\", 'e') ---> w = %s\n", w); addChar(w, "cas", 97); printf("addChar(w, \"cas\", 97) ---> w = %s\n", w); addChar(w, "1 2 3 4 5 6 ", '7'); printf("addChar(w, \"1 2 3 4 5 6 \", '7') ---> w = %s\n", w); addChar(w, "1 2 3 4 5 6 ", 7); printf("addChar(w, \"1 2 3 4 5 6 \", 7) ---> w = %s\n", w); return 0; } /********** ESERCIZIO 1.d *****************/ /* word e' la parola ottenuta eliminando da source il carattere source[k]. Si assume 0 <= k < strLength(source) e che word contenga spazio sufficiente per contenere il risultato. */ void deleteChar(char *word, char *source, int k){ } int mainD(){ char input[MAX+1]; char w[MAX+1]; int k; printf("Parola --> "); scanf("%s", input); printf("\nIndice carattere da eliminare --> "); scanf("%d",&k); if(0<= k && k < strLength(input)) { deleteChar(w,input,k); printf("\ndeleteChar(w,\"%s\",%d) ---> w = %s\n", input,k,w); } else printf("%d: indice non corrett0 per %s\n", k, input); return 0; } /********** ESERCIZIO 1.e *****************/ /* Restituisce 1 se le stringhe w1 e w2 sono uguali, 0 altrimenti */ int strUguali(char *w1, char *w2){ return -1; } int mainE(){ printf("strUguali(\"cane\",\"canzone\") = %d\n", strUguali("cane", "canzoni")); printf("strUguali(\"cane\",\"cane\") = %d\n", strUguali("cane", "cane")); printf("strUguali(\"cane\",\"canea\") = %d\n", strUguali("cane", "canea")); printf("strUguali(\"gattone\",\"gatto\") = %d\n", strUguali("gattone", "gatto")); printf("strUguali(\"\",\"\") = %d\n", strUguali("", "")); printf("strUguali(\"1 2\",\"12\") = %d\n", strUguali("1 2", "12")); printf("strUguali(\"12\",\"12\") = %d\n", strUguali("12", "12")); return 0; } /********** ESERCIZIO 1.f *****************/ /* t e' la parola ottenuta invertendo s. Si assume che t contenga spazio sufficiente per contenere il risultato. */ void strInv(char *t, char *s){ } int mainF(){ char input[MAX+1]; char w[MAX+1]; printf("Parola da invertire ---> "); scanf("%s", input); strInv(w,input); printf("\nstrInv(w,\"%s\") ---> w = %s\n", input, w); return 0; }