Thread: Stuck on trying to code Ceasar Cipher

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    5

    Lightbulb Stuck on trying to code Ceasar Cipher

    Hi all!
    I'm trying to do ceasar cipher from a python code. My problem is, I don't know why my C code doesn't work and I need proper guidance. Below is the code for C, the loops work but the if statement seem to be set to false for each iteration. If i but an else statement like printf("fail"), the else is executed and "Fail" is output on the screen each time it loops.


    Code:
    void crypter(){
        char alphabet[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
        char alphabetcap[26] = {'A','B','C','D','E','F','G','H','I','G','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
        int cle,i = 0, j;
        int temp1;
        char newTexte, temp;
        char texte[1000];
        FILE *fp;
        printf("Insert a number: ");
        scanf("%d", &cle);
        printf("Insert a text: ");
        scanf("%c",&temp);
      fgets(texte,1000,stdin);
    
    
        for (i; texte != '\0' ; i++)
        {
            for (j = 0; j < 26; j++)
            {
                if (texte[i] == alphabet[j])
                {
                    temp1 = (j + cle) % 26;
                    newTexte += alphabet[temp1];
                }
            }
        }
        memset(texte,0,strlen(texte));
        texte[1000] = newTexte;
        fp = fopen("foo.txt", "w");
        fwrite(&texte, sizeof(texte), 1, fp);
        fclose(fp);
    }
    Here the code of python i'm trying to convert into C.

    Code:
    alphabet = 'abcdefghijklmnopqrstuvwxyz'key = 3
    newMessage = ''
    message = input('Please enter a message: ')
    for character in message:
        if character in alphabet:
            position = alphabet.find(character)
            newPosition = (position + key) % 26
            newCharacter = alphabet[newPosition]
            newMessage += newCharacter
        else:
            newMessage += character
    print(newMessage)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > newTexte += alphabet[temp1];
    C doesn't have the ability to build up a string from a set of characters through addition.

    Code:
      printf("%c\n", 'A' + '$' );
    This will print 'e', not "A$".

    make newTexte an array and write each new character to a subscript of that array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    5
    Quote Originally Posted by Salem View Post
    > newTexte += alphabet[temp1];
    C doesn't have the ability to build up a string from a set of characters through addition.

    Code:
      printf("%c\n", 'A' + '$' );
    This will print 'e', not "A$".

    make newTexte an array and write each new character to a subscript of that array.
    Thanks for the help i did correct this part. The error i'm getting now is, when I decrypt it, I get special characters instead of letters.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code? Also, post your test input, expected output, and (as far as you can) actual output.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2019
    Posts
    5
    Let say i input aaaa bbbb cccc with a cle = 1, the output should be bbbb cccc dddd, it move each letter by one place as the cle(key) is 1.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You didn't post your current code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2019
    Posts
    5
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    
    
    void crypter();
    void decrypter();
    
    
    int main()
    {
    	int choix;
    	do
    	{
    		printf("Bienvenue\n");
    		printf("Appuyez sur 1 pour crypter un fichier\n");
    		printf("Appuyez sur 2 pour decrypter un fichier\n");
    		scanf("%d", &choix);
    		switch(choix)
    		{
    			case 1: crypter();
    			break;
    			case 2: decrypter();
    			break;
    			default: printf("Erreur le programme va s'arreter");
    			break;
    		}
    	} while(choix != 3);
    	return 0;
    }
    
    
    void crypter()
    {
    	char alphabet[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
    	char alphabetcap[26] = {'A','B','C','D','E','F','G','H','I','G','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    	int cle,i = 0, j;
    	char temp1;
    	char temp,newTexte[1000];
    	char texte[1000];
    	FILE *fp;
    	memset(newTexte,0,strlen(newTexte));
    	printf("Inserez un nombre: ");
    	scanf("%d", &cle);
    	printf("Inserez un texte: ");
    	scanf("%c",&temp);
      fgets(texte,1000,stdin);
    
    
    	for (i; i < 1000 ; i++)
    	{
    		for (j = 0; j < 26; j++)
    		{
    			if (texte[i] == alphabet[j])
    			{
    				temp1 = texte[i] + j;
    				newTexte[i] = temp1;
    			}
    		}
    		texte[i] = newTexte[i];
    	}
    	fp = fopen("test.txt", "w");
    	fwrite(&texte, sizeof(texte), 1, fp);
    	fclose(fp);
    }
    void decrypter()
    {
    	char alphabet[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
    	char alphabetcap[26] = {'A','B','C','D','E','F','G','H','I','G','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    	int i, j, cle;
    	char texte[1000],ch, temp;
    	char newTexte[1000];
    	FILE *fp;
    	fp = fopen("test.txt", "r");
    	printf("Inserez un nombre: ");
    	scanf("%d", &cle);
    	while (fgets(texte, sizeof(texte), fp)!=NULL)
    	{
    		for (i = 0; i < 1000 ; i++)
    		{
    			ch = texte[i];
    			for(j = 0; j < 26; j++)
    			{
    				if (ch == alphabet[j])
    				{
    					temp = (j - cle);
    					texte[i] = temp;
    				}
    			}
    		}
    		printf("%c", texte);
    	}
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In this revised version of your code, you don't appear to be using cle after reading it. Surely that is a bug since the key is essential to the encryption.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2019
    Posts
    5
    thanks for taking your time to help, sorry for having disturb, i will restart from scratch for this program

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. slightly stuck with C code
    By suspense in forum C Programming
    Replies: 6
    Last Post: 05-11-2016, 09:39 AM
  2. Can anyone see any errors in this cipher code?
    By dodgetech in forum C Programming
    Replies: 25
    Last Post: 12-07-2012, 02:15 AM
  3. still problems with ceasar shift
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 09-14-2008, 01:49 AM
  4. Ceasar Shift program
    By trevordunstan in forum C Programming
    Replies: 11
    Last Post: 09-11-2008, 09:40 PM

Tags for this Thread