Results 1 to 5 of 5

Thread: c problem = string to char?

  1. #1
    Join Date
    Jan 2007
    Location
    heaven , Malaysia
    Beans
    806

    c problem = string to char?

    i try to create a program that should give output=
    Code:
    -->enter words = Defend the east wall of the castle
    -->enter key(26 characters not repeat)    = cdefghiklmnopqrstuvwxyzab
    -->original:   Defend the east wall of the castle.
    -->cleaned:    defendtheeastwallofthecastle
    -->ciphertext: efgfocsiffxvusfqiqdvkcfxtukf
    -->plaintext:  defendtheeastwallofthecastle
    but i get error when want to make program interact with user input.any help is really appreciated

    PHP Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include<string.h>


    void playfair_encipher(char *key, const char *plaintextchar *ciphertext);
    void playfair_decipher(char *key, const char *ciphertextchar *plaintext);
    void prepare_for_playfair(char *originaltextchar *plaintext);

    int main(int argcchar *argv[]){
       


    /************************!
        /*
        printf("Please enter Text that want to encrypt = ");
        scanf("%c\n", &Text);
        printf("data = %c\n", Text);
        char *originaltext = Text;
        */

        /* the string to encipher / decipher (want to modified to interactive code!)*/
        
        
    char *originaltext "Defend the east wall of the castle.";
    //***********************!    
        



        /* allocate some space for results of enciphering/deciphering */
        
    char *plaintext malloc(strlen(originaltext)+2);
        
    char *ciphertext malloc(strlen(originaltext)+2);
        
    char *result malloc(strlen(originaltext)+2);
        
        
    /* we have to remove punctuation, spacing etc. and make the string an
        even number of bytes long */
        
    prepare_for_playfair(originaltext,plaintext);
        




        
    //**************************!
        /* the following lines do all the enciphering and deciphering */
        //hope this can be user input too

        
    playfair_encipher("cdefghiklmnopqrstuvwxyzab"plaintext,ciphertext);
        
    playfair_decipher("cdefghiklmnopqrstuvwxyzab"ciphertext,result);
        
    //**************************!



        /* print our results */
        
    printf("-->original:   %s\n-->cleaned:    %s\n-->ciphertext: %s\n-->plaintext:  %s\n",
                
    originaltext,plaintext,ciphertext,result);
        
        
    free(plaintext);
        
    free(ciphertext);
        
    free(result);
        
        
    system("PAUSE");
        return 
    0;
    }

    /*******************************************************************
    void playfair_encipher(char *key, const char *plaintext, char *ciphertext)
    - Uses playfair cipher to encipher some text.
    - takes a key, string of plaintext, result returned in ciphertext.
    - ciphertext should be an array the same size as plaintext.
    - The key is a string containing each of the 26 letters in the alphabet.
    *******************************************************************/
    void playfair_encipher(char *key, const char *plaintextchar *ciphertext){
        
    int i 0length strlen(plaintext); 
        
    char a,b;   
        
    int a_indb_inda_rowb_rowa_colb_col;   
         
        for(
    0lengthi+=2){
            
    tolower(plaintext[i]);
            
    tolower(plaintext[i+1]);
            
            
    a_ind = (int)strchr(key,a) - (int)key;
            
    b_ind = (int)strchr(key,b) - (int)key;
            
    a_row a_ind 5;
            
    b_row b_ind 5;
            
    a_col a_ind 5;
            
    b_col b_ind 5;

            if(
    a_row == b_row){
                if(
    a_col == 4){
                    
    ciphertext[i] = key[a_ind 4];
                    
    ciphertext[i+1] = key[b_ind 1];
                }else if(
    b_col == 4){
                    
    ciphertext[i] = key[a_ind 1];
                    
    ciphertext[i+1] = key[b_ind 4];
                }else{
                    
    ciphertext[i] = key[a_ind 1];
                    
    ciphertext[i+1] = key[b_ind 1];
                }
            }else if(
    a_col == b_col){
                if(
    a_row == 4){
                    
    ciphertext[i] = key[a_ind 20];
                    
    ciphertext[i+1] = key[b_ind 5];
                }else if(
    b_row == 4){
                    
    ciphertext[i] = key[a_ind 5];
                    
    ciphertext[i+1] = key[b_ind 20];
                }else{
                    
    ciphertext[i] = key[a_ind 5];
                    
    ciphertext[i+1] = key[b_ind 5];
                }
            }else{
                
    ciphertext[i] = key[5*a_row b_col];
                
    ciphertext[i+1] = key[5*b_row a_col];
            }       
        }while (
    length);
        
    ciphertext[length] = '\0';
    }

    /*******************************************************************
    void playfair_decipher(char *key, const char *ciphertext, char *plaintext)
    - Uses playfair cipher to decipher some text.
    - takes a key, string of ciphertext, result returned in plaintext.
    - plaintext should be an array the same size as ciphertext.
    - The key is a string containing each of the 26 letters in the alphabet.
    *******************************************************************/
    void playfair_decipher(char *key, const char *ciphertextchar *plaintext){
        
    int ilength strlen(ciphertext); 
        
    char a,b;   
        
    int a_indb_inda_rowb_rowa_colb_col;   
         
        for(
    0lengthi+=2){
            
    tolower(ciphertext[i]);
            
    tolower(ciphertext[i+1]);
            
            
    a_ind = (int)strchr(key,a) - (int)key;
            
    b_ind = (int)strchr(key,b) - (int)key;
            
    a_row a_ind 5;
            
    b_row b_ind 5;
            
    a_col a_ind 5;
            
    b_col b_ind 5;
            if(
    a_row == b_row){
                if(
    a_col == 0){
                    
    plaintext[i] = key[a_ind 4];
                    
    plaintext[i+1] = key[b_ind 1];
                }else if(
    b_col == 0){
                    
    plaintext[i] = key[a_ind 1];
                    
    plaintext[i+1] = key[b_ind 4];
                }else{
                    
    plaintext[i] = key[a_ind 1];
                    
    plaintext[i+1] = key[b_ind 1];
                }
            }else if(
    a_col == b_col){
                if(
    a_row == 0){
                    
    plaintext[i] = key[a_ind 20];
                    
    plaintext[i+1] = key[b_ind 5];
                }else if(
    b_row == 0){
                    
    plaintext[i] = key[a_ind 5];
                    
    plaintext[i+1] = key[b_ind 20];
                }else{
                    
    plaintext[i] = key[a_ind 5];
                    
    plaintext[i+1] = key[b_ind 5];
                }
            }else{
                
    plaintext[i] = key[5*a_row b_col];
                
    plaintext[i+1] = key[5*b_row a_col];
            }
        }
        
    plaintext[length] = '\0'/* Null terminate */  
    }

    /************************************************************************
    void prepare_for_playfair(char *originaltext, char *plaintext)
    - removes all characters that are not letters. i.e. all numbers, punctuation,
     spaces etc. are removed (uppercase is also converted to lowercase). If the length
     of the string is odd, an x is appended to make it an even length
    - playfair can only encrypt even length strings.
    - If you want numbers, punctuation etc. you must spell it out e.g.
     'stop' for period, 'one', 'two' etc.
    **************************************************************************/
    void prepare_for_playfair(char *originaltextchar *plaintext){
        
    int ijlength strlen(originaltext);
        
        for(
    00lengthi++, j++){
            while(!
    isalpha(originaltext[i])) i++;
            if(
    >= length) break;
            
    plaintext[j] = tolower(originaltext[i]);
        }
        
    /* if the string is an even length, leave it */
        
    if (== 0plaintext[j] = '\0';
        
    /* else append an 'x' */
        
    else {
            
    plaintext[j] = 'x';
            
    plaintext[j+1] = '\0';
        }

    もう誰かのためじゃなくて 自分のために笑っていいよ
    ~ Please do mark as resolved thread if your problem is solved,thanks~

  2. #2
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: c problem = string to char?

    Quote Originally Posted by AlexenderReez View Post
    PHP Code:
    /************************!
        /*
        printf("Please enter Text that want to encrypt = ");
        scanf("%c\n", &Text);
        printf("data = %c\n", Text);
        char *originaltext = Text;
        */

        /* the string to encipher / decipher (want to modified to interactive code!)*/
        
        
    char *originaltext "Defend the east wall of the castle.";
    //***********************! 
    I assume you are referring to this part that you have commented? First of all, Text is never defined. Secondly %c in scanf will only match a single character. %s will match a word (delimited by whitespace), which is probably still not what you want. If you want to read in a whole line of text, you should use fgets.

  3. #3
    Join Date
    Mar 2005
    Location
    Haarlem, The Netherlands
    Beans
    363

    Re: c problem = string to char?

    Quote Originally Posted by AlexenderReez View Post
    i try to create a program that should give output=
    Code:
    -->enter words = Defend the east wall of the castle
    -->enter key(26 characters not repeat)    = cdefghiklmnopqrstuvwxyzab
    -->original:   Defend the east wall of the castle.
    -->cleaned:    defendtheeastwallofthecastle
    -->ciphertext: efgfocsiffxvusfqiqdvkcfxtukf
    -->plaintext:  defendtheeastwallofthecastle
    but i get error when want to make program interact with user input.any help is really appreciated
    I saved your code in a file test.c, compiled an ran it. The only error I see is that the command PAUSE doesn't exist (it is a windows command, not a linux command). You mention that you use user input, I don't see any attempt to read some text, can you publish that code?
    Code:
    [martin@sony ~]$ gcc test.c
    [martin@sony ~]$ ./a.out 
    -->original:   Defend the east wall of the castle.
    -->cleaned:    defendtheeastwallofthecastle
    -->ciphertext: efgfocsiffxvusfqiqdvkcfxtukf
    -->plaintext:  defendtheeastwallofthecastle
    sh: PAUSE: command not found
    [martin@sony ~]$

  4. #4
    Join Date
    Jan 2007
    Location
    heaven , Malaysia
    Beans
    806

    Re: c problem = string to char?

    Quote Originally Posted by Martin Witte View Post
    I saved your code in a file test.c, compiled an ran it. The only error I see is that the command PAUSE doesn't exist (it is a windows command, not a linux command). You mention that you use user input, I don't see any attempt to read some text, can you publish that code?
    Code:
    [martin@sony ~]$ gcc test.c
    [martin@sony ~]$ ./a.out 
    -->original:   Defend the east wall of the castle.
    -->cleaned:    defendtheeastwallofthecastle
    -->ciphertext: efgfocsiffxvusfqiqdvkcfxtukf
    -->plaintext:  defendtheeastwallofthecastle
    sh: PAUSE: command not found
    [martin@sony ~]$
    yes..the code working,but it is not interact with user...which it doesn't ask for user input.i 'm asking how to do that.ok.i try fgets.
    もう誰かのためじゃなくて 自分のために笑っていいよ
    ~ Please do mark as resolved thread if your problem is solved,thanks~

  5. #5
    Join Date
    Jan 2007
    Location
    heaven , Malaysia
    Beans
    806

    Re: c problem = string to char?

    i solved problem to get input but having hard time to get key for processing the encryption or decryption..help me please

    encryption success..but i still have problem with decryption

    PHP Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include<string.h>


    void playfair_encipher(char *key, const char *plaintextchar *ciphertext);
    void playfair_decipher(char *key, const char *ciphertextchar *plaintext);
    void prepare_for_playfair(char *originaltextchar *plaintext);

    int main(int argcchar *argv[]){
        
    //get user input for text!
        
    char anskeyED[26] ,Text[1000];
        
    printf("Please enter Text that want to encrypt or decrypt= ");
        
    fgets(Textsizeof (Text), stdin);
        
    char *originaltext Text;
        
        
    printf("Please enter Key  for encryption or decryption= ");
        
    fgets(keyEDsizeof (keyED), stdin);
         
        



        
    /* allocate some space for results of enciphering/deciphering */
        
    char *plaintext malloc(strlen(originaltext)+2);
        
    char *ciphertext malloc(strlen(originaltext)+2);
        
    char *result malloc(strlen(originaltext)+2);
        
        
    /* we have to remove punctuation, spacing etc. and make the string an
        even number of bytes long */
        
    prepare_for_playfair(originaltext,plaintext);
        




        
    //**************************!
        /* the following lines do all the enciphering and deciphering */
        
        
    printf("Do you want to encryped or decrypt the numbers[e to encrype and d to decrypt]\n=");//ask to encrypt or decrypt
    scanf(" %c", &ans);        
        if (
    ans == 'e' || ans == 'E'){ //for encryption
        
        //something wrong with this=
        
    playfair_encipher(keyEDplaintext,ciphertext);
        
        
    /* print our results */
        
    printf("-->original:   %s\n-->cleaned:    %s\n-->ciphertext: %s\n-->plaintext:  %s\n",
                
    originaltext,plaintext,ciphertext,result);
        


        }
        else if(
    ans == 'd' || ans == 'D'){ //for decryption
        
        //something wrong with this=
        
    playfair_decipher(keyEDciphertext,result);
        
        
    /* print our results */
        
    printf("-->original:   %s\n-->cleaned:    %s\n-->ciphertext: %s\n-->plaintext:  %s\n",
                
    originaltext,plaintext,ciphertext,result);
        

        
        }
        
    //**************************!



        
        
        
    free(plaintext);
        
    free(ciphertext);
        
    free(result);
        
        
    //system("PAUSE");
        
    return 0;
    }

    //Encryption
    void playfair_encipher(char *key, const char *plaintextchar *ciphertext){
        
    int i 0length strlen(plaintext); 
        
    char a,b;   
        
    int a_indb_inda_rowb_rowa_colb_col;   
         
        for(
    0lengthi+=2){
            
    tolower(plaintext[i]);
            
    tolower(plaintext[i+1]);
            
            
    a_ind = (int)strchr(key,a) - (int)key;
            
    b_ind = (int)strchr(key,b) - (int)key;
            
    a_row a_ind 5;
            
    b_row b_ind 5;
            
    a_col a_ind 5;
            
    b_col b_ind 5;

            if(
    a_row == b_row){
                if(
    a_col == 4){
                    
    ciphertext[i] = key[a_ind 4];
                    
    ciphertext[i+1] = key[b_ind 1];
                }else if(
    b_col == 4){
                    
    ciphertext[i] = key[a_ind 1];
                    
    ciphertext[i+1] = key[b_ind 4];
                }else{
                    
    ciphertext[i] = key[a_ind 1];
                    
    ciphertext[i+1] = key[b_ind 1];
                }
            }else if(
    a_col == b_col){
                if(
    a_row == 4){
                    
    ciphertext[i] = key[a_ind 20];
                    
    ciphertext[i+1] = key[b_ind 5];
                }else if(
    b_row == 4){
                    
    ciphertext[i] = key[a_ind 5];
                    
    ciphertext[i+1] = key[b_ind 20];
                }else{
                    
    ciphertext[i] = key[a_ind 5];
                    
    ciphertext[i+1] = key[b_ind 5];
                }
            }else{
                
    ciphertext[i] = key[5*a_row b_col];
                
    ciphertext[i+1] = key[5*b_row a_col];
            }       
        }while (
    length);
        
    ciphertext[length] = '\0';
    }


    //decryption!
    void playfair_decipher(char *key, const char *ciphertextchar *plaintext){
        
    int ilength strlen(ciphertext); 
        
    char a,b;   
        
    int a_indb_inda_rowb_rowa_colb_col;   
         
        for(
    0lengthi+=2){
            
    tolower(ciphertext[i]);
            
    tolower(ciphertext[i+1]);
            
            
    a_ind = (int)strchr(key,a) - (int)key;
            
    b_ind = (int)strchr(key,b) - (int)key;
            
    a_row a_ind 5;
            
    b_row b_ind 5;
            
    a_col a_ind 5;
            
    b_col b_ind 5;
            if(
    a_row == b_row){
                if(
    a_col == 0){
                    
    plaintext[i] = key[a_ind 4];
                    
    plaintext[i+1] = key[b_ind 1];
                }else if(
    b_col == 0){
                    
    plaintext[i] = key[a_ind 1];
                    
    plaintext[i+1] = key[b_ind 4];
                }else{
                    
    plaintext[i] = key[a_ind 1];
                    
    plaintext[i+1] = key[b_ind 1];
                }
            }else if(
    a_col == b_col){
                if(
    a_row == 0){
                    
    plaintext[i] = key[a_ind 20];
                    
    plaintext[i+1] = key[b_ind 5];
                }else if(
    b_row == 0){
                    
    plaintext[i] = key[a_ind 5];
                    
    plaintext[i+1] = key[b_ind 20];
                }else{
                    
    plaintext[i] = key[a_ind 5];
                    
    plaintext[i+1] = key[b_ind 5];
                }
            }else{
                
    plaintext[i] = key[5*a_row b_col];
                
    plaintext[i+1] = key[5*b_row a_col];
            }
        }
        
    plaintext[length] = '\0'/* Null terminate */  
    }


    void prepare_for_playfair(char *originaltextchar *plaintext){
        
    int ijlength strlen(originaltext);
        
        for(
    00lengthi++, j++){
            while(!
    isalpha(originaltext[i])) i++;
            if(
    >= length) break;
            
    plaintext[j] = tolower(originaltext[i]);
        }
        
    /* if the string is an even length, leave it */
        
    if (== 0plaintext[j] = '\0';
        
    /* else append an 'x' */
        
    else {
            
    plaintext[j] = 'x';
            
    plaintext[j+1] = '\0';
        }

    Last edited by AlexenderReez; February 23rd, 2009 at 01:32 AM.
    もう誰かのためじゃなくて 自分のために笑っていいよ
    ~ Please do mark as resolved thread if your problem is solved,thanks~

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •