Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. /* Si scriva una funzione ricorsiva in C che prende in ingresso una stringa e restituisce la medesima invertita. Ad
  6. esempio, Cremona diventerebbe anomerC. La funzione deve poter gestire stringhe di dimensioni qualsiasi
  7. (si consiglia di avere la dimensione come parametro della funzione).
  8. Per semplificare la scrittura del codice, si ricorda che string.h mette a disposizione diverse funzioni per la
  9. gestione delle stringhe. Ad esempio:
  10. • size t strlen(const char *s), per calcolare la lunghezza effettiva di una stringa;
  11. • char *strcat(char *restrict s1, const char *restrict s2), per il concatenamento di due stringhe;
  12. • char *strncpy(char *restrict s1, const char *restrict s2, size t n), per la copia di n caratteri di una stringa in un’altra.*/
  13. //char* inversioneRic (char str[]);
  14. #define ARRLENGHT 8
  15.  
  16. typedef enum { false,true} bool;
  17.  
  18. bool InvertiStringa(char*pc,char*uc);
  19.  
  20. int main() {
  21. char myarr [ARRLENGHT] = {'c','i','a','o','c','o','m','e'};
  22. char val;
  23. int i;
  24. bool ok;
  25. ok= InvertiStringa(&myarr[0],&myarr[ARRLENGHT-1]);
  26. printf("la stringa inversa è: ");
  27.  
  28. for(i = 0; i<ARRLENGHT; i++)
  29. printf("%s",myarr[i]);
  30. return 0;
  31. }
  32.  
  33. bool InvertiStringa(char*pc,char*uc){
  34. char tempo;
  35. if(pc>=uc)
  36. return 1;
  37.  
  38. tempo = *pc;
  39. *pc = *uc;
  40. *uc= tempo;
  41.  
  42. return InvertiStringa(pc+1,uc-1);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement