Difference between revisions of "Solução: Problema da palavra mágica (Mauricio Lage)"

From AdonaiMedrado.Pro.Br
Jump to: navigation, search
(New page: #include <stdio.h> #include <stdlib.h> #include <string.h> int crescente(const void *a, const void *b) { return *(char *)a-*(char *)b; } int main(int argc, char** argv) { char...)
 
 
Line 1: Line 1:
 +
<code lang = "c">
 +
 
#include <stdio.h>
 
#include <stdio.h>
 
#include <stdlib.h>
 
#include <stdlib.h>
Line 37: Line 39:
 
     return (EXIT_SUCCESS);
 
     return (EXIT_SUCCESS);
 
}
 
}
 +
 +
</code>

Latest revision as of 16:54, 3 July 2009

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int crescente(const void *a, const void *b)
{
	return *(char *)a-*(char *)b;
}
 
 
int main(int argc, char** argv) {
 
    char palavra[101];
    char prefixo[51]= "";
    char sufixo[51]= "";
    int tamPalavra;
 
    scanf("%s", palavra);
    tamPalavra= strlen(palavra);
    if( (tamPalavra % 2) == 0 ) {
        strncat(prefixo, palavra, tamPalavra/2);
        strncat(sufixo, &palavra[tamPalavra/2], tamPalavra/2);                
        qsort(prefixo, (tamPalavra / 2), sizeof(char), crescente);        
        if(!strcmp(prefixo, sufixo)) {
            printf("%c\n", 'S');
        }
        else {
            printf("%c\n", 'N');            
        }
 
    }
    else {
        printf("%c\n", 'N');
    }
 
 
    return (EXIT_SUCCESS);
}