Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdlib.h>
- #include<stdio.h>
- #include<string.h>
- #define N (20)
- typedef struct {
- char *str;
- char *anagr;
- } stringa;
- int ConfrontaCaratteri(const void *a, const void *b) {
- return strcmp((char*)a, (char*)b);
- }
- int ConfrontaAnagramma(const void *a, const void *b) {
- stringa s1= *(stringa*)a;
- stringa s2= *(stringa*)b;
- return strcmp(s1.anagr, s2.anagr);
- }
- int ConfrontaStr(const void *a, const void *b) {
- stringa s1= *(stringa*)a;
- stringa s2= *(stringa*)b;
- return strcmp(s1.str, s2.str);
- }
- int main() {
- int i, j, n;
- scanf("%d", &n);
- stringa *S= (stringa *) malloc(n * sizeof(stringa ));
- for(i=0; i<n; i++) {
- S[i].str= (char *) malloc(N * sizeof(char ));
- S[i].anagr= (char *) malloc(N * sizeof(char ));
- scanf("%s", S[i].str);
- strcpy(S[i].anagr, S[i].str);
- qsort(S[i].anagr, strlen(S[i].anagr), sizeof(char), ConfrontaCaratteri);
- }
- qsort(S, n, sizeof(stringa), ConfrontaAnagramma);
- j=0;
- for(i=1; i<n; i++) {
- if(strcmp(S[i].anagr, S[i-1].anagr)!=0) {
- if(i-j>1)
- qsort(S+j, i-j, sizeof(stringa), ConfrontaStr);
- j=i;
- }
- }
- if(i-j>1)
- qsort(S+j, i-j, sizeof(stringa), ConfrontaStr);
- printf("%s ", S[0].str);
- /* Stampa l'array ordinato */
- for( i = 1; i < n; i++ ) {
- if(strcmp(S[i].anagr, S[i-1].anagr)==0)
- printf("%s ", S[i].str);
- else {
- printf("\n");
- printf("%s ", S[i].str);
- }
- }
- printf(" \n");
- free(S);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement