Pergunta

i have written a C programme which prints itself n times, but i can't get how to reverse print the same n times.E.g, if the sample programme is :

hello

then the required output should be "olleh" for n=1. Here's my quine programme,

#include <stdio.h>
int main()
{
  int n;
  char c;
  FILE *f;
  f=fopen(__FILE__,"r");
  scanf("%d",&n);
 while(n--)
 {
 while((c=getc(f))!=EOF)
 putchar(c);
 fseek(f,0,0);
 }
  return 0;
} 
Foi útil?

Solução

The easiest way would be to read the file into an array (like this answer), and then just reverse the array:

void swap(char* a, char* b) {
  char tmp = *b;
  *b = *a;
  *a = tmp;
}

void reverse(char* arr, int size) {
  for (int i = 0; i < size/2; ++i) {
    swap(arr+i, arr + (size - (i + 1)));
  }
}

Outras dicas

This is not a pure quine. See the Quine article in Wikipedia:

A quine takes no input. Allowing input would permit the source code to be fed to the program via the keyboard, opening the source file of the program, and similar mechanisms.

Just came across this post. Here is sample reverse quine in C, that i made. You can modify it to suit your needs!

a="};)01(rahctup;)--p*(rahctup);p*;43=p*(rof;)a(ftnirp;))a,b=p(tacrts(nelrts=+p{)p*rahc(niam;}7393422{=]99[b;";b[99]={2243937};main(char*p){p+=strlen(strcat(p=b,a));printf(a);for(*p=34;*p;)putchar(*p--);putchar(10);}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top