Thread: Improvements/suggestions/comments to this simple program.. (leet translator!)

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    244

    Improvements/suggestions/comments to this simple program.. (leet translator!)

    I'd just like some tips on how to streamline it, make it smaller, any security warnings, etc. Or just comment on how cool I am..

    Code:
    #include <stdio.h>
    
    void leet(char *y);
    void ask(void);
    
    	char phrase[100];
    	char *y;
    	char ans[5];
    	char *x;
    
    int main()
    {
    
    	ask();
    	return 0;
    }
    
    void ask()
    {
    
    	puts("Would you like to translate something? (Y or N)");
    	gets(ans);
    	x = ans;
    	switch(*x)
    	{
    	case 'Y':
    		puts("What do you want to translate?");
    		leet(gets(phrase));
    	case 'y':
    		puts("What do you want to translate?");
    		leet(gets(phrase));
    	case 'n':
    		puts("You are the weakest link, goodbye!");
    		getch();
    		exit();
    	case 'N':
    		puts("You are the weakest link, goodbye!");
    		getch();
    		exit();
    	default:
    		puts("That is not an option, you moron.");
    			puts("Press any key to return to the REAL WORLD...");
    			getch();
    			ask();
    	}
    
    }
    
    void leet(char *y)
    {
    	while(*y != '\0')
    	{
    		
    		switch(*y)
    		{
    		case 'e':
    			putchar('3');
    			y++;
    			break;
    		case 'E':
    			putchar('3');
    			y++;
    			break;
    		case ' ':
    			putchar(' ');
    			y++;
    			break;
    		case 'o':
    			putchar('0');
    			y++;
    			break;
    		case 'O':
    			putchar('0');
    			y++;
    			break;
    		case 'T':
    			putchar('7');
    			y++;
    			break;
    		case 'A':
    			putchar('4');
    			y++;
    			break;
    		case 'a':
    			putchar('4');
    			y++;
    			break;
    		case 's':
    			putchar('$');
    			y++;
    			break;
    		case 'S':
    			putchar('$');
    			y++;
    			break;
    		default:
    			putchar(*y);
    			y++;
    			break;
    		}
    		
    	}
    	putchar('\n');
    	ask();
    }
    I know, lame program, but it's good practice to me.


    Oh yes, I'd also like to know how to make sure that the user doesn't input more characters then the array allows (100)... I know this can be done but I'm not sure how!

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    sorry I accidently clicked twice (damn IE)
    Last edited by Crossbow; 12-04-2001 at 09:34 PM.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    One thing to cut down on the size is in your switch statement where you say:

    case 'Y':
    puts("What do you want to translate?");
    leet(gets(phrase));
    case 'y':
    puts("What do you want to translate?");
    leet(gets(phrase));

    you can say:

    case 'Y':
    case 'y':
    puts("What do you want to translate?");
    leet(gets(plrase));
    break; //make sure you have a break statement or your switch
    //statement won't end, and it will print all the other cases.

    you can do the same thing for the other cases. Also, put your variables in their respective functions, global variables can get messy, so it is best not to use them, even with small programs. Also, you are passing a char array to a char pointer so that just won't work. You will have to change phrase to a single byte pointer char, or you can use the reference operator(if you don't know what it is, go to a good C++ pointer tutorial. I knew where one was, but I forgot). Good luck.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Originally posted by Crossbow
    One thing to cut down on the size is in your switch statement where you say:

    case 'Y':
    puts("What do you want to translate?");
    leet(gets(phrase));
    case 'y':
    puts("What do you want to translate?");
    leet(gets(phrase));

    you can say:

    case 'Y':
    case 'y':
    puts("What do you want to translate?");
    leet(gets(plrase));
    break; //make sure you have a break statement or your switch
    //statement won't end, and it will print all the other cases.

    you can do the same thing for the other cases. Also, put your variables in their respective functions, global variables can get messy, so it is best not to use them, even with small programs. Also, you are passing a char array to a char pointer so that just won't work. You will have to change phrase to a single byte pointer char, or you can use the reference operator(if you don't know what it is, go to a good C++ pointer tutorial. I knew where one was, but I forgot). Good luck.
    Thanks I'll do what I can with the global variables and the switch-case statements but I don't know what you are saying about the function. it looks right to ME, and the program works fine as it is in my post.. hmm..

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Also, you are passing a char array to a char pointer so that just won't work.
    Why not? The name of an array is simply a pointer to the first element of that array, so passing a char array by name to a char pointer is perfectly legal

    As for the actual program, here's a compactisized version
    Code:
    #include <stdio.h>
    
    void leet(char *y);
    void ask(void);
    
    int main(){
        ask();
        return 0;
    }
    
    void ask(){
        char phrase[100], *y, ans[5];
    
        puts("Would you like to translate something? (Y or N)");
        fgets(ans, sizeof(ans), stdin);
    
        if(strcmp(ans, "Y") == NULL || strcmp(ans, "y") == NULL){
    	puts("What do you want to translate?");
    	leet(fgets(phrase, sizeof(phrase), stdin));
        }else if(strcmp(ans, "N") == NULL || strcmp(ans, "n") == NULL){
    	puts("You are the weakest link, goodbye!");
    	getchar();
    	exit(0);
        }else{
    	puts("That is not an option, you moron.");
    	puts("Press any key to return to the REAL WORLD...");
    	getchar();
    	ask();
        }
    }
    
    void leet(char *y){
        while(*y != '\0'){
    	switch(*y)
    	{
    	case 'e':
    	case 'E':
    		putchar('3'); y++;
    		break;
    	case 'o':
    	case 'O':
    		putchar('0'); y++;
    		break;
    	case 'T':
    		putchar('7'); y++;
    		break;
    	case 'A':
    	case 'a':
    		putchar('4'); y++;
    		break;
    	case 's':
    	case 'S':
    		putchar('$'); y++;
    		break;
    	default:
    		putchar(*y); y++;
    		break;
    	}	
        }
        putchar('\n');
        ask();
    }
    I tried to keep it as similar to what you had as possible while still within the bounds of reason. I got rid of your x variable since it wasn't doing anything useful, fixed your switch to remove redundancies, killed the first switch because it was just taking up space and replaced it with an if-else, and just scrunched everything up and made it a bit tidier. I could make it smaller, but then it wouldn't look anything like your program, so I didn't do that.

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

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void leet(char *y){
        while(*y != '\0'){
    	switch(*y)
    	{
    	case 'e':
    	case 'E':
    		putchar('3'); y++;
    		break;
    	case 'o':
    	case 'O':
    		putchar('0'); y++;
    		break;
    	case 'T':
    		putchar('7'); y++;
    		break;
    	case 'A':
    	case 'a':
    		putchar('4'); y++;
    		break;
    	case 's':
    	case 'S':
    		putchar('$'); y++;
    		break;
    	default:
    		putchar(*y); y++;
    		break;
    	}	
        }
        putchar('\n');
        ask();
    }
    Compact? Use 'toupper' and a single y++ to make it shorter.
    Code:
    void leet(char *y){
        while(*y != '\0'){
    	switch(toupper(*y))
    	{
    	case 'E': putchar('3'); break;
    	case 'O': putchar('0'); break;
    	case 'T': putchar('7'); break;
    	case 'A': putchar('4'); break;
    	case 'S': putchar('$'); break;
    	default: putchar(*y);
    	}
    	y++;
        }
        putchar('\n');
        ask();
    }
    Besides, you forgot to replace the L with 1

    Quzah.
    Hope is the first step on the road to disappointment.

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 silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  3. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  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