Results 1 to 8 of 8

Thread: [SOLVED] Caesar cipher in C

  1. #1
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    [SOLVED] Caesar cipher in C

    Hi there!
    I've been experimenting these days with some C just because of fun and need some help.

    This code tries to substitute one single letter into a number (A=0, B=1, C=2, etc.), but for some reason it doesn't work and gets stuck into an infinite loop. This is the first step for a little Caesar Cipher encoder, where letters are "summed" by a key. So, I need to convert letters into numbers first. (I don't want any help on the rest)

    Code:
    #include <stdio.h>
    #include <string.h> //for strlen();
    #include <ctype.h> //for toupper();
    
    char *abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //Alphabet defined as global variable
    
    int subst(char x); //Function defined
    
    int main(void)
    {
    	char key;
    	int key2;
    
    	puts("Enter a 1 character long text");
    	scanf("%c", &key);
    	
    	key = toupper(key); //converts all letters to uppercase
    	key2 = subst(key); //calling substitution function
    	
    	printf("%d\n", key2);
    	
    	return 0;
    }
    
    int subst(char x)
    {
    	int out, i, len;	
    	len = strlen(abc); //do len be abc string's length
    	
    	for((i = 0); (i = len); i++)
    	{
    		if(abc[i] == x)
    		{
    			out = i;
    		}
    	/*This should search each abc's characters and compare it with x (which is the key variable) and, if they're the same, the function will return the corresponding character's place in the array as result (so, A = 0, B = 1, etc.)*/
    	}
    	
    	return out;
    }
    Thanks!
    Last edited by nvteighen; March 23rd, 2008 at 03:55 PM. Reason: format

  2. #2
    Join Date
    Dec 2007
    Location
    UK
    Beans
    571
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Caesar cipher in C

    for((i = 0); (i = len); i++)
    Here you set i to len each loop. You want:
    Code:
    for((i = 0); (i < len); i++)

  3. #3
    Join Date
    Dec 2006
    Location
    Glasgow, Scotland
    Beans
    470
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Caesar cipher in C

    The "for" loop looks weird, try this:

    Code:
    for (i = 0; i < len; i++)
    or using pointer arithmetic:

    Code:
    /* include <string.h> */
    int subst(char x)
    {
           char *p;
    
           p = index(abc, x);
           return (p - abc);
    }
    Last edited by ruy_lopez; March 23rd, 2008 at 04:16 PM.
    Don't be too sure every 'thanks' you receive is genuine.

  4. #4
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: Caesar cipher in C

    Thank you very much! Compiled and substitution works perfectly. Now I'll try to make it ignore numbers (if I enter "2", I get "0", which is an "a").

    But, just curious, why shouldn't I use (i <= len) instead?

  5. #5
    Join Date
    Dec 2006
    Location
    Glasgow, Scotland
    Beans
    470
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Caesar cipher in C

    Quote Originally Posted by nvteighen View Post
    But, just curious, why shouldn't I use (i <= len) instead?
    array indexes start at 0. So when you test using "< len", it stops at len - 1, which is correct for an array.

    In other words, the length of str = "string" is 6 chars, but the last char is at str[5].
    Last edited by ruy_lopez; March 23rd, 2008 at 04:30 PM.
    Don't be too sure every 'thanks' you receive is genuine.

  6. #6
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Caesar cipher in C

    Tip: You know that you can get the ascii code of any character by using 'something'. So use that, and for each character on a string, substracting the first letter:
    PHP Code:
    char string[20] = "abcdef"

    //blah blah loop
    numb string[i] - 'a' 
    it would produce something like:
    'a' - 'a' = 0
    'b' - 'a' = 1
    'c' - 'a' = 2
    ...

    And you can filter that the input is not < 'a' and > 'z'
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  7. #7
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: Caesar cipher in C

    Quote Originally Posted by Can+~ View Post
    Tip: You know that you can get the ascii code of any character by using 'something'. So use that, and for each character on a string, substracting the first letter:
    PHP Code:
    char string[20] = "abcdef"

    //blah blah loop
    numb string[i] - 'a' 
    it would produce something like:
    'a' - 'a' = 0
    'b' - 'a' = 1
    'c' - 'a' = 2
    ...

    And you can filter that the input is not < 'a' and > 'z'
    Well, that's easier and avoids using that global array. And also avoids the using ctype's isalpha()! Thanks!

  8. #8
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: Caesar cipher in C

    OK, the program works great! Thank you all for your advices!

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
  •