Created
January 6, 2024 02:44
-
-
Save Samu31Nd/f46e75f9b23a193cc2bf2f67081266fd to your computer and use it in GitHub Desktop.
Programa del palindromo hecho en C. Para ver el resultado, se checa el archivo output.txt dejado en el mismo directorio.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <time.h> | |
| //GLOBAL VARIABLES | |
| char numAux; | |
| int n; | |
| int size; | |
| char *palindromo(); | |
| FILE *createFile(char *name); | |
| void printCharbyChar(char *string,FILE *doc); | |
| char getRandomBin( ); | |
| int main(int argc, char *argv[]){ | |
| srand(time(NULL)); | |
| if(argc > 1){ | |
| n = atoi(argv[1]); | |
| } else { | |
| printf("Ingresa n: "); | |
| scanf("%d", &n); | |
| } | |
| char *string = palindromo(); | |
| return 0; | |
| } | |
| char *palindromo(){ | |
| FILE *doc = createFile("output"); | |
| //si es par, entonces lo haremos impar para añadirle el p | |
| //de otra forma, ya es impar, y remplazaremos P por un numero | |
| size = n + ( (n%2)?0:1 ); | |
| char *string = (char*)malloc(sizeof(char)*size); | |
| // posicionamos el apuntador a la mitad de la cadena para ponerle el P | |
| char *apP = &(string[n/2]); | |
| char *apStart, *apFinal; | |
| apFinal = apStart = string; | |
| //lo llenamos vacio y nos vamos al final, esto es para que al imprimir, | |
| //podamos imprimir la cadena sin interrupcion de parte del caracter nulo | |
| for(int i = 0; i < size; i++) | |
| *apFinal++ = 0; | |
| apFinal--; | |
| *apP = 'P'; | |
| printCharbyChar(string, doc); | |
| for(int i = 0; i < size/2; i++){ | |
| numAux = getRandomBin(); | |
| *apFinal-- = *apStart++ = numAux; | |
| fprintf(doc,"%s. ",(numAux == '0')? "(4)":"(5)"); | |
| printCharbyChar(string, doc); | |
| } | |
| if( !(n%2) ){ | |
| *apP = 'e'; | |
| fprintf(doc,"(1). "); | |
| printCharbyChar(string, doc); | |
| *apP = 0; | |
| fprintf(doc," "); | |
| printCharbyChar(string, doc); | |
| } else{ | |
| numAux = getRandomBin(); | |
| *apP = numAux; | |
| fprintf(doc,"%s. ",(numAux == '0')? "(2)":"(3)"); | |
| printCharbyChar(string, doc); | |
| } | |
| fclose(doc); | |
| return string; | |
| } | |
| void printCharbyChar(char *string,FILE *doc){ | |
| for(int i = 0; i < size; i++){ | |
| if(string[i] == 0) continue; | |
| fprintf(doc,"%c",string[i]); | |
| } | |
| fprintf(doc, "\n"); | |
| } | |
| char getRandomBin( ){ | |
| return rand()%2 ? '0':'1'; | |
| } | |
| FILE *createFile(char *name){ | |
| FILE *fp; | |
| char filename[40]; | |
| sprintf(filename,"%s.txt",name); | |
| if ((fp=fopen(filename,"w")) == NULL){ | |
| fprintf(stderr,"Error al crear el archivo"); | |
| exit(1); | |
| } | |
| return fp; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment