#if and #endif

This is a discussion on #if and #endif within the C Programming forums, part of the General Programming Boards category; I want to implement #if (DEBUG==1) I want an entering and exiting message to print before and after invoking decrypt() ...

  1. #1
    Arthuro
    Guest

    #if and #endif

    I want to implement #if (DEBUG==1) I want an entering and exiting message to print before and after invoking decrypt()

    I have no clue to how to do it...my books I so confusing and hopeless!!!

    I need help!!

    here is my program: A bit long it is basically encrypting and then decrypting a text using a scramble and unscramble key

    Code:
    #include <stdio.h>
    #include <string.h>
    #define LINESIZE 60
    #define MIN(a,b) (((a)<(b))?(a):(b))
    
    static const char KEY[]="qazxswedcvfrtgbnhyujmkiolp";
    
    void decrypt(const char in[],char out[]);
    void encrypt(const char in[],char out[]);
    void printtext(const char t[]);
    char scramble(const char c);
    char unscramble(const char c);
    
    int main(void)
    {
        const char plaintext[]="Fourscore and seven years ago our fathers \
    brought forth upon this continent a new nation, conceived in liberty, \
    and dedicated to the proposition that all men are created equal.";
    
        char decrypttext[sizeof(plaintext)/sizeof(char)];
    	char encrypttext[sizeof(plaintext)/sizeof(char)];
    	
    	printtext(plaintext);
        encrypt(plaintext,encrypttext);
    	printtext(encrypttext);
    	decrypt(encrypttext,decrypttext);
    	printtext(decrypttext);
    
    	if (strcmp(plaintext,decrypttext)==0)
    		puts("plain same decrypttext");
        	else puts("not identical");
    
    	return 0;
    }
    
    /*encrypt*/
    void encrypt(const char in[],char out[])
    {
    	int k;
    	
    	for (k=0;in[k];k++)
    		out[k]=scramble(in[k]);
    	out[k]='0';
    	
    	return;
    }
    
    /*decrypt*/
    void decrypt(const char in[],char out[])
    {
    	int k;
    	
    	for (k=0;in[k];k++)
    		out[k]=unscramble(in[k]);
    	out[k]='0';
    	
    	return;
    }
    
    /*scramble*/
    char scramble(const char c)
    {
    	if ((c<'a')|| (c>'z')) return c;
    	return KEY[c-'a'];
    }
    
    /*unscramble*/
    char unscramble(const char c)
    {
    	int k;
    	char alphabet[]="abcdefghijklmnopqrstuvwxyz";
        
    	if ((c<'a')||(c>'z')) return c;
    
    	for (k=0;k<strlen(KEY);k++)
    		if (c==KEY[k]) break;
    	
    	return alphabet[k];
    }
    
    /*printtext*/
    void printtext(const char t[])
    {
    	int start;
    	int finish;
    	int k, tmp;
    
    	start =0;
    
    	while (start<strlen(t))
    	{
    		tmp=MIN((start+LINESIZE),strlen(t));
    		for (finish=tmp;finish>start;finish--)
    			if ( (t[finish]==' ') || (t[finish]=='\0') ) break;
    
    		for(k=start;k<finish;k++)
    			putchar(t[k]);
    		printf("\n");
    
    		start=finish+1;    
    	}
    	printf("\n");
    
    	return;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    int main(void)
    {
      const char plaintext[]="Fourscore and seven years ago our fathers \
        brought forth upon this continent a new nation, conceived in liberty, \
        and dedicated to the proposition that all men are created equal.";
    
      char decrypttext[sizeof(plaintext)/sizeof(char)];
      char encrypttext[sizeof(plaintext)/sizeof(char)];
    
      printtext(plaintext);
      encrypt(plaintext,encrypttext);
      printtext(encrypttext);
    
    #ifdef DEBUG
      fputs ( "Entering decrypt\n", stdout );
    #endif
      decrypt(encrypttext,decrypttext);
    #ifdef DEBUG
      fputs ( "Exiting decrypt\n", stdout );
    #endif
    
      printtext(decrypttext);
    
      if (strcmp(plaintext,decrypttext)==0)
        puts("plain same decrypttext");
      else
        puts("not identical");
    
      return 0;
    }
    Now all you have to do is define DEBUG like this:

    #define DEBUG

    And the print statements will execute, if you comment out the above line, neither of the print statements will execute.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21