Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.91 KB | None | 0 0
  1. //
  2. //  main.c
  3. //  EsercizioGennaio
  4. //
  5. //  Created by Michele Zotti on 23/01/2020.
  6. //  Copyright © 2020 Michele Zotti. All rights reserved.
  7. //
  8.  
  9. /*
  10.  
  11.  1. char *cerca_pal (char *data[]), FILE *): determina se la stringa (unica) presente nel File input è contenuta nell’array di puntatori a carattere e ne restituisce l’indirizzo solo se è palindroma (eventualmente NULL se la stringa non esiste o non è palindroma). Utilizzare la notazione puntatore (Punti 18)
  12.  2. int pal (char *): ricevuto l’indirizzo della stringa, verifica se è palindroma: restituisce 1 (la stringa esiste) o 0 (la stringa non esiste). Utilizzare la notazione puntatore e vettore (Punti 7)
  13.  3. void scrivi_file(FILE *, char *): la funzione scrive nel file, il cui nome è immesso sulla linea di comando (vedi void main(int argc, char *argv[])), la stringa palindroma ed il suo indirizzo. (Punti 5)
  14.  */
  15. #include <stdio.h>
  16. #include <string.h>
  17. #define MAX 5
  18.  
  19. char * cerca_pal(char *[], FILE *);
  20. int pal(char *);
  21. int scrivi_file(FILE *, char *);
  22.  
  23. int main(int argc, char *argv[])
  24. {
  25.     FILE *output, *input;
  26.     input=fopen(argv[1],"r");
  27.     output=fopen(argv[2],"w");
  28.     char *data[MAX]={"nvcfvfdv", "vfvfhvvs","xxppxx","uutryvsdg","nvjfiep"}, *ptr_pal;
  29.     ptr_pal=cerca_pal(data, input);
  30.     if (ptr_pal)
  31.         scrivi_file(output,ptr_pal);
  32.    
  33. }
  34.  
  35. char * cerca_pal(char *data[], FILE *input) {
  36.     char buf[200];
  37.  
  38.     while((fgets(buf, 200, input)) != NULL) {
  39.         printf("%d",pal(buf));
  40.       printf("%s", buf);
  41.     }
  42.  
  43.    
  44.     return NULL;
  45. }
  46.  
  47. int pal(char* str) {
  48.     int flag = 0;
  49.     long lenght = strlen(str);
  50.     for (int i = 0; i < lenght; i++) {
  51.         if (str[i] != str[i - lenght - 1]) {
  52.             flag = 1;
  53.         }
  54.     }
  55.     return flag;
  56. }
  57.  
  58. int scrivi_file(FILE * output , char *data) {
  59.     if (output != NULL) {
  60.         fprintf(output, "%s", data);
  61.         return 1;
  62.     }
  63.    
  64.     return 0;
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement