Simple algorithm problem

This is a discussion on Simple algorithm problem within the C Programming forums, part of the General Programming Boards category; I want to write a simple encryption program that does the following: Take a file as input (redirected at the ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Location
    unf
    Posts
    16

    Question Simple algorithm problem

    I want to write a simple encryption program that does the following: Take a file as input (redirected at the Unix prompt), and encrypt/decrypt and print each character as directed. Convert uppercase characters to the lowercase character 3 down in the alphabet, EXCEPT A, B, C which become x, y, z respectively. Convert lowercase characters to the uppercase character 2 down in the alphabet, EXCEPT a, b which become Y, Z respectively. Convert all punctuation characters (ASCII 33-47, 58-64, 91-96, and 123-126) to the punctuation character 1 down in the ASCII chart; e.g., 34 " becomes 33 !, 35 # becomes 34 ", 58 : becomes 47 /;
    EXCEPT 33 ! which becomes 126 ~.

    here is some sample output:
    Code:
    If the file which is redirected in contains the following text:
    
    ABCDEFGHIJKLMNOPQRSTUVWXYZ
    abcdefghijklmnopqrstuvwxyz
    
    01234567890
    
    The quick brown fox jumped
    
    
    over the lazy     dogs.
    
    
    
    !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
    
    Then the output of the program should be::
    
    xyzabcdefghijklmnopqrstuvw
    YZABCDEFGHIJKLMNOPQRSTUVWX
    
    01234567890
    
    qFC OSGAI ZPMUL DMV HSKNCB
    
    
    MTCP RFC JYXW     BMEQ-
    
    
    
    ~!"#$%&'()*+,-./:;<=>?@[\]^_`{|}
    now my code looks like:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    #define TRUE 1;
    #define False 0;
    
    //function prototypes
    char convert(char inputChar, int *whtSpc, int *charCnt);
    void display(int numWords, int whtSpc);
    char convUpper(char inputChar);
    char convLower(char inputChar);
    char convPunct(char inputChar);
    
    int main(void)
    {
       //initialize vars
       char inputChar;
       char convertedChar;
       int charCnt = 0;
       int numWords = 0;
       int whtSpc = 0;
    
       //while((inputChar = getchar()) != EOF)
       //{
       convertedChar = convert(inputChar, &whtSpc, &charCnt);
       //}//end while
       
    
       display(numWords, whtSpc);
    
       return 0;
    }//end of main
    
    
    char convert(char inputChar, int *whtSpc, int *charCnt)
    {
       char convertedChar;
       
       while((inputChar = getchar()) != EOF)
       {
       if(isalnum(inputChar) == 1)
       {
          charCnt++;
       } else if(isspace(inputChar) == 1) {
          charCnt--;
       }
       if((charCnt >0) && (isspace(inputChar)))
       {
          whtSpc++;
       }//end if   
    
       if(isupper(inputChar))
       {
       convertedChar = convLower(inputChar);
       }//end if
     else if(islower(inputChar))
       {
       convertedChar = convUpper(inputChar);
       }//end if
       else if(ispunct(inputChar))
       {
       convertedChar = convPunct(inputChar);
       }
       printf("%c", convertedChar);
       }//end of while
    
       return convertedChar;
    }//end of convert()
    
    /////////////////////////////////////////////////////////////////////////////
    //Name: display()
    //Purpose: displays the results of the conversion
    /////////////////////////////////////////////////////////////////////////////
    void display(int numWords, int whtSpc)
    {
       printf("\n%20s%20s", "Description", "Quantity");
       printf("\n%20s%20s", "-----------", "--------");
       printf("\n%20s%20d", "Words", numWords);
       printf("\n%20s%20d", "Whitespace", whtSpc);
    
       printf("\n\n");
    }//end of display()
    
    //////////////////////////////////////////////////////////////////////////////
    //Name: convUpper()
    //Purpose: converts lowercase chars to an uppercase char 2 down
    //////////////////////////////////////////////////////////////////////////////
    char convUpper(char inputChar)
    {
       
       char convertedChar = inputChar;   
       
       if(inputChar >= 97 && inputChar <=98)
       {
       if(inputChar == 97)
       {
          convertedChar = 'Y';
       } else if(inputChar == 98){
          convertedChar = 'Z';
       }
       }//end if
       
       if(!(isupper(convertedChar)))
       {
        convertedChar = toupper(inputChar + 2);
       }//end if
    
       return convertedChar;
    }//end of convUpper()
    
    //////////////////////////////////////////////////////////////////////////////
    //Name: convLower()
    //Purpose: converts uppercase chars to a lowercase char 3 down
    /////////////////////////////////////////////////////////////////////////////
    char convLower(char inputChar)
    {
       char convertedChar = inputChar;
       
       if(inputChar >= 65 && inputChar <= 67)
       {
       if(inputChar == 65)
       {
          convertedChar = 97;
       } else if(inputChar == 66){
          convertedChar = 98;
       } else if(inputChar == 67){
          convertedChar = 99;
       } 
       }//end if
       
       if(!(islower(convertedChar)))
       {
          convertedChar = tolower(inputChar + 3);
       }//end if
       
       
       return convertedChar;
    }//end of convLower()
    
    /////////////////////////////////////////////////////////////////////////////
    //Name: convPunct()
    //Purpose: wrap the puctioation arround + 1 times
    /////////////////////////////////////////////////////////////////////////////
    char convPunct(char inputChar)
    {
       char convertedChar;
       if(inputChar == '!')
       {
          convertedChar = '~';
       } else {
          
          convertedChar = (int)inputChar - 1;
       }
       return convertedChar;
    }//end of convPunct()
    any help would be greatly appreciated and thank you in advance. This has been bugging me all night long.

  2. #2
    Registered User computerfreaks's Avatar
    Join Date
    Jan 2005
    Posts
    30
    The first thing that I would note about this is the following:

    Code:
       while((inputChar = getchar()) != EOF)
       {
       convertedChar = convert(inputChar, &whtSpc, &charCnt);
       }
    
    while((inputChar = getchar()) != EOF)
       {
       if(isalnum(inputChar) == 1)
       {

    You appear to be sending a character to your convert function, and then you are asking for the user to input another char which will overwrite the one sent to the function.

    Code:
    display(numWords, whtSpc);
    Has this part not been completed yet??.



    I am not entirely sure in fact what you are asking for here.... have you written this far and are now stuck? or is there a certain problem that you cannot overcome?

  3. #3
    Registered User
    Join Date
    Feb 2005
    Location
    unf
    Posts
    16
    its reading data from a file using a linux command like this:
    Code:
    a.out < t1
    yea display is not quite finnished. My main problem is getting the characters to convert properly t1 is also a text file
    Last edited by stodd04; 03-04-2005 at 07:37 AM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by computerfreaks
    Code:
    while((inputChar = getchar()) != EOF)
       {
       if(isalnum(inputChar) == 1)
       {
    There is no guarintee that isalnum, or any of the other is* functions will ever return 1. It's only guarinteed to be zero and "non zero". Therefore you should be using:
    Code:
    if( isalnum( inputChar ) != 0 )
    (Is this like the theme of the day or what?)

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

  5. #5
    Registered User computerfreaks's Avatar
    Join Date
    Jan 2005
    Posts
    30
    I was just wondering about the theme thing myself..... you guys all from the same school or something with the same assignment??

    I think you need better teachers!

  6. #6
    Registered User
    Join Date
    Feb 2005
    Location
    unf
    Posts
    16
    ah... thank you very much that was very helpful

  7. #7
    Registered User
    Join Date
    Feb 2005
    Location
    unf
    Posts
    16
    Nah... thats not an assignment from my class. Thank ya'll very much for you help

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by computerfreaks
    I was just wondering about the theme thing myself..... you guys all from the same school or something with the same assignment??

    I think you need better teachers!
    Actually you misunderstood. You need a better teacher. Reread my post to you.

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

  9. #9
    Registered User
    Join Date
    Feb 2005
    Location
    unf
    Posts
    16
    I would say it would be more my own stupidity than the ability of the teacher

  10. #10
    Registered User computerfreaks's Avatar
    Join Date
    Jan 2005
    Posts
    30
    Quzah,


    ??? I'm not following?? I need a better teacher? which post?

    I am on these boads to learn..... if I am getting something wrong, then please tell me so that I can learn from my mistakes

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by computerfreaks
    Quzah,


    ??? I'm not following?? I need a better teacher? which post?

    I am on these boads to learn..... if I am getting something wrong, then please tell me so that I can learn from my mistakes
    Here is the post in question:
    Quote Originally Posted by computerfreaks
    Code:
    while((inputChar = getchar()) != EOF)
       {
       if(isalnum(inputChar) == 1)
       {
    There is no guarintee that isalnum, or any of the other is* functions will ever return 1. It's only guarinteed to be zero and "non zero". Therefore you should be using:
    Code:
    if( isalnum( inputChar ) != 0 )
    (Is this like the theme of the day or what?)

    Quzah.
    You seem to have misread something here I guess. I was commenting on the fact that your comparison for isalnum was wrong, and that this is the second time today that I have corrected people on incorrect return value testing on the is* functions.

    To clarify once again, there is no guarintee that the return value of any of the is* functions will ever be 1. It could be 40, it could be -1, it could be any non-zero value, to mean that the test was successful.

    The functions have two return states, and so, you should test them correctly:

    1) If the character is not part of the set of characters being test for, then the return value is zero.
    2) If the character is part of the set of characters being test for, the return value is non-zero.

    That means, it can be anything at all other than zero. So testing for "1" is wrong. You should test only for "equals zero" or "not equals zero".
    Code:
    if( isalnum( foo ) == 0 )
    
    ...
    
    if( isalnum( foo ) != 0 )
    Testing for anything else may or may not work.

    I could create a compiler which was 100% ANSI compataible, an have the return values of these functions be -12345 or 0, and it would be perfectly legal. In which case, your testing for "== 1" wouldn't work as you expect it to. And yet, my compiler would still be 100% ANSI compatable.


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

  12. #12
    Registered User
    Join Date
    Feb 2005
    Location
    unf
    Posts
    16
    Quzah, I cannot thank you enough for your infinite programming wisdom. I am going back to the drawing board and restructring the program to add some stuff and get rid of some redundancy. I will post it fixed later. Once again thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what do you think about this simple sorting algorithm?
    By mariano_donati in forum C Programming
    Replies: 9
    Last Post: 02-23-2008, 03:03 AM
  2. Simple Variable Problem
    By Cthulhu in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2005, 03:07 PM
  3. Problem with simple XOR program
    By spike_ in forum C++ Programming
    Replies: 8
    Last Post: 08-17-2005, 12:09 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. a simple algorithm and questions
    By ustuzou in forum C++ Programming
    Replies: 0
    Last Post: 02-18-2002, 10:12 AM

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