Thread: simple silly program

  1. #1
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101

    Thumbs down simple silly program

    Hey guys,

    I was just messing around making a really simple encryption type program. Reads a file in, changes some letters around (makes no effort at case sensitivity), then outputs it to another file. I have given it a good try but can't seem to get the letters to change. I'm sure it's something stupid...but I can't see it right now.

    Code:
    
    
    
    /**************************************************************************************
    A program to receive a text file, then use simple encryption method to encrypt it, then 
    another method to decrypt it***********************************************/
    
    #define _CRT_SECURE_NO_DEPRECATE
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #define SIZE 63
    #define LBUFSIZE 64
    
    int getWord(FILE *fp, char wbuf[], int c);
    void output(char lbuf[]);
    FILE *encrypt; 
    
    void main(void)
    {
    	FILE *fp;
    
    	int c = 0;
    
    	char wbuf[SIZE];
    	char lbuf[LBUFSIZE];
    
    	
    	encrypt = fopen("encrypt.dat", "w");	
    	fp = fopen("file.txt", "r");
    
    	
    		lbuf[0] = '\0';
    
    		c = getWord(fp, wbuf, c);
    
    		while(c != EOF)
    		{
    			//add 1 for space
    			if((strlen(lbuf) + 1 + strlen(wbuf)) > 62)
    			{
    				output(lbuf);
    				strcpy(lbuf, wbuf);
    			}
    			else
    			{
    				//enough space for this word
    				strcat(lbuf, " ");
    				strcat(lbuf, wbuf);
    			}
    		c = getWord(fp, wbuf, c);
    
    		}
    		if (c == EOF && strlen(lbuf) > 0)
    		{
    			output(lbuf);
    		}
    		
    
    	fclose(fp);
    	fclose(encrypt);
    	
    
    }
    
    
    
    int getWord(FILE *fp, char wbuf[], int c)
    {
    	
    	
    	int i = 0;
    
    	if(isalpha(c))
    	{
    		letterSwitch(c);
    		wbuf[i] = c;
    		i++;
    	}
    
    	c = fgetc(fp);
    
    	while(c !=EOF && isalpha(c))
    	{
    		letterSwitch(c);
    		wbuf[i] = c;
    		i++;
    		c = fgetc(fp);
    		
    	}
    	
    	wbuf[i] = '\0';	
    
    	while(c != EOF && !isalpha(c))
    	{
    		c = fgetc(fp);
    	}
    	return c;
    
    }
    void output(char lbuf[])
    {
    
    	printf("%s\n", lbuf);
    	fprintf(encrypt, "%s\n", lbuf);
    
    	
    }
    
    int letterSwitch(int c)
    {
    
    	switch(c)
    	{
    	case 'a':
    		c = '!';
    	case 'e': 
    		c = '@';
    	case 'i':
    		c = '#';
    	case 'o':
    		c = '%';
    	case 'r':
    		c = '^';
    	case 'b':
    		c = '&';
    	case 's':
    		c = '*';
    	case 't':
    		c = '(';
    	case 'v':
    		c = ')';
    	case 'p':
    		c = '_';
    	case 'l':
    		c = '+';
    	case 'n':
    		c = '=';
    	case 'm':
    		c = '[';
    	case 'g':
    		c = ']';
    	case 'c':
    		c = '?';
    	case 'y':
    		c = '<';
    	case 'q':
    		c = '>';
    	case 'k':
    		c = '/';
    	}
    	return c;
    }
    And there it is!

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    	if(isalpha(c))
    	{
    		letterSwitch(c);
    		wbuf[i] = c;
    		i++;
    	}
    letterswitch returns the new value and does not changes the argument, you are ignoring the return value and using the original letter

    wbuf[i] = letterSwitch(c);

    PS.
    int main(void)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    break; is also needed after each entry in your switch statement, unless you want to always have c='/' whenever c matches any of those.

  4. #4
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    Thanks for the help....I actually figured both those things out as I was going along after I wrote that. But new problem. It won't finish reading the line to undo the encryption (I feel stupid calling it that). I think the problem is in getWord() the last while loop...I think maybe I'm using the wrong is...whatever function....i've been scanning through the MSDN but still haven't found what I'm looking for.....(hey...U2....LOL). And vart I'm still working on remembering that int main (our teacher didn't really pound that into us).
    Code:
    #define _CRT_SECURE_NO_DEPRECATE
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #define SIZE 63
    #define LBUFSIZE 64
    
    int getWord(FILE *fp, char wbuf[], int c);
    void output(char lbuf[]);
    int letterSwitch(int c);
    
    int main(void)
    {
    	FILE *fp;
    
    	int c = 0;
    
    	char wbuf[SIZE];
    	char lbuf[LBUFSIZE];
    
    	
    		
    	fp = fopen("encrypt.txt", "r");
    
    	
    		lbuf[0] = '\0';
    
    		c = getWord(fp, wbuf, c);
    
    		while(c != EOF)
    		{
    			//add 1 for space
    			if((strlen(lbuf) + 1 + strlen(wbuf)) > 62)
    			{
    				output(lbuf);
    				strcpy(lbuf, wbuf);
    			}
    			else
    			{
    				//enough space for this word
    				strcat(lbuf, " ");
    				strcat(lbuf, wbuf);
    			}
    		
    			c = getWord(fp, wbuf, c);
    		}
    		
    		if (c == EOF && strlen(lbuf) > 0 && strlen(wbuf) > 0)
    		{
    			strcat(lbuf, wbuf);
    			output(lbuf);
    		}
    
    		
    		
    
    	fclose(fp);
    
           return 0;
    	
    
    }
    
    
    
    int getWord(FILE *fp, char wbuf[], int c)
    {
    	
    	
    	int i = 0;
    
    	if(isascii(c) && isgraph(c))
    	{
    		
    		c = letterSwitch(c);
    		wbuf[i] = c;
    		i++;
    
    
    	}
    
    	c = fgetc(fp);
    
    	while(c !=EOF && (isascii(c) && isgraph(c)))
    	{
    		c = letterSwitch(c);
    		wbuf[i] = c;
    		i++;
    		c = fgetc(fp);
    		
    	}
    	
    	wbuf[i] = '\0';	
    
    	while(c != EOF && (isascii(c) && isgraph(c)))
    	{
    		c = fgetc(fp);
    	}
    	return c;
    
    }
    void output(char lbuf[])
    {
    
    	printf("%s\n", lbuf);
    
    	
    }
    
    int letterSwitch(int c)
    {
    
    	switch(c)
    	{
    	case '!':
    		c = 'a';
    		break;
    	case '@': 
    		c = 'e';
    		break;
    	case '#':
    		c = 'i';
    		break;
    	case '%':
    		c = 'o';
    		break;
    	case '^':
    		c = 'r';
    		break;
    	case '&':
    		c = 'b';
    		break;
    	case '*':
    		c = 's';
    		break;
    	case '(':
    		c = 't';
    		break;
    	case ')':
    		c = 'v';
    		break;
    	case '_':
    		c = 'p';
    		break;
    	case '+':
    		c = 'l';
    		break;
    	case '=':
    		c = 'n';
    		break;
    	case '[':
    		c = 'm';
    		break;
    	case ']':
    		c = 'g';
    		break;
    	case '?':
    		c = 'c';
    		break;
    	case '<':
    		c = 'y';
    		break;
    	case '>':
    		c = 'q';
    		break;
    	case '-':
    		c = 'k';
    		break;
    	default:
    		c = c;
    
    	}
    	return c;
    }
    Last edited by verbity; 12-19-2006 at 12:59 AM.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    "c = c" is redundant. Well, it might prevent timing attacks... but this isn't encryption, it's encoding.

    In addition, this might be much faster:
    Code:
    char lookuptable[256];
    lookuptable[(int)'a'] = '!';
    ...
    lookuptable[(int)'k'] = '-';
    
    char reverselookuptable[256];
    reverselookuptable[(int)'!']= 'a';
    ...
    reverselookuptable[(int)'-'] = 'k';
    
    
    for(i < strlen())
        encodedstring[i] = lookuptable[str[i]];
    
    for(i < strlen())
        decodedstring[i] = reverselookuptable[encodedstring[i]];
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  6. #6
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101

    Question

    Thanks and I guess, but that didn't really solve my problem....I'll mess with it some more and hopefully I'll come up with something. this is just to have fun so it's not that important.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  2. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM
  5. I need help on a formula for this simple program.
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-28-2002, 10:01 PM