Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Verificare se una stringa è palindroma; se lo è si comunica, se non lo è la si inverte.*/
- #include <stdio.h>
- #include <string.h>
- int scegli_opzione(void);
- void leggi_stringa(char *,int *);
- int pal(char *, int);
- void reverse(char *, int);
- int stampa_risposta(char *, int);
- int main(void)
- { char stringa[100];
- int opzione, dim_str, temp=1, ris;
- do
- { opzione=scegli_opzione();
- switch(opzione)
- { case 0: temp=0; break;
- case 1: leggi_stringa(stringa, &dim_str);
- ris=pal(stringa,dim_str);
- if(!ris)
- reverse(stringa,dim_str);
- stampa_risposta(stringa, ris); break;
- }
- }while(temp);
- return 0;
- }
- int scegli_opzione(void)
- { int sc;
- printf("Se vuoi eseguire il programma inserire 1; altrimenti, per terminare, inserisci 0: ");
- scanf("%d",&sc);
- return sc;
- }
- void leggi_stringa(char *vect,int* n)
- { printf("Inserire stringa: ");
- scanf("%s", vect);
- *n=strlen(vect);
- }
- int pal(char *vect, int n)
- { int i,res, j=n-1;
- for(i=0; i<n/2; i++, j--)
- { if(*(vect+i)==*(vect+j))
- res=1;
- else
- res=0;
- }
- return res;
- }
- void reverse(char *vect, int n)
- { int i, j=n-1;
- char temp;
- for(i=0; i<n/2; i++,j--)
- { temp=*(vect+i);
- *(vect+i)=*(vect+j);
- *(vect+j)=temp;
- }
- }
- int stampa_risposta(char *vect, int ris)
- { if(ris==0)
- printf("STRINGA NON PALINDROMA. \nSTAMPA STRINGA INVERTITA.\n------------------------\n%s \n", vect);
- else
- printf("STRINGA %s PALINDROMA.\n", vect);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement