Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // EsercizioGennaio
- //
- // Created by Michele Zotti on 23/01/2020.
- // Copyright © 2020 Michele Zotti. All rights reserved.
- //
- /*
- 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)
- 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)
- 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)
- */
- #include <stdio.h>
- #include <string.h>
- #define MAX 5
- char * cerca_pal(char *[], FILE *);
- int pal(char *);
- int scrivi_file(FILE *, char *);
- int main(int argc, char *argv[])
- {
- FILE *output, *input;
- input=fopen(argv[1],"r");
- output=fopen(argv[2],"w");
- char *data[MAX]={"nvcfvfdv", "vfvfhvvs","xxppxx","uutryvsdg","nvjfiep"}, *ptr_pal;
- ptr_pal=cerca_pal(data, input);
- if (ptr_pal)
- scrivi_file(output,ptr_pal);
- }
- char * cerca_pal(char *data[], FILE *input) {
- char buf[200];
- while((fgets(buf, 200, input)) != NULL) {
- printf("%d",pal(buf));
- printf("%s", buf);
- }
- return NULL;
- }
- int pal(char* str) {
- int flag = 0;
- long lenght = strlen(str);
- for (int i = 0; i < lenght; i++) {
- if (str[i] != str[i - lenght - 1]) {
- flag = 1;
- }
- }
- return flag;
- }
- int scrivi_file(FILE * output , char *data) {
- if (output != NULL) {
- fprintf(output, "%s", data);
- return 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement