Like Tree1Likes
  • 1 Post By iMalc

Simple Caesar cyptography

This is a discussion on Simple Caesar cyptography within the C Programming forums, part of the General Programming Boards category; Hey guys I am working on implementing a simple cryptography program called Caesar. I got this program to work perfectly ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    49

    Simple Caesar cyptography

    Hey guys I am working on implementing a simple cryptography program called Caesar. I got this program to work perfectly when I was writing a function to handle the cryptography and just declaring the string that I wanted to encrypt and the integer I wanted to move the letters by to encrypt it. maybe you can help me out and tell me what I am doing wrong here. I am just trying to get the user to enter the key into main and then be prompted for the string to encrypt.

    I ended up with a segmentation fault.. I have absolutely no idea what I am looking at but when I ran GDP I get this

    #0 0x001bd283 in ____strtol_l_internal (nptr=0x0, endptr=0x0, base=10, group=0,
    loc=0x3123a0) at strtol_l.c:298..


    Anyways here is my code..

    Code:
    #include <stdio.h>
    #include <cs50.h>
    #include <stdlib.h>
    
    
    int main (int argc, char *argv[])
    {
    
        // turn string k into int k
        int k = atoi(argv[1]);
        k = k % 26;
        
        int i = 0;
        
        // prompt user for string
        string word = GetString();
        
        // below here all works fine when I had it as a stand alone function that I 
        // passed arguments to from main.. should encrypt by "k" letters
        while (word[i] != '\0')
        {
           if (word[i] >= 'a' && word[i] <= 'z')
           {
               word[i] = (word[i] + k - 97) % 26 + 97; 
               i++;
           }
           
          else  if (word[i] >= 'A' && word[i] <= 'Z')
            {
                word[i] = (word[i] + k - 65) % 26 + 65;
                i++;
            }
            else
                i++;
        }
        word[i] = '\0';
        
        printf("%s\n", word); 
            
    }

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,384
    It's possible that GetString() is not putting a '\0' at the end of you string.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    ok so I replaced the getString portion of the code with simply declaring a string called "word". That way I could determine if that was the issue. Apparently it is not because I am still getting a segmentation fault. So it has nothing to do with GetString. Instead it has do with "k". Can someone please tell em what is wrong with how I have written the main function and k?
    Last edited by sdbuilt; 10-01-2012 at 10:09 PM.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    I figured it out never mind. Anyone know how to delete a post?

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,384
    You do not want to delete this thread, because it might help someone

    Post your solution for everyone who is having the same problem
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    You don't. It is not your place to decide that nobody else should benefit from finding the same problem and solution at a later date.
    The more courteous posters actualy post what the cause and solution was.
    rags_to_riches likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    sorry I just read this.. the solution was that I was not inputting the initial values into the main function right. I thought that I type ./caesar and press enter and then it will prompt me for the input to main. I did not realize I had to type it like ./caesar input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help on what I think is a Caesar cipher......
    By CaesarCipher in forum C++ Programming
    Replies: 8
    Last Post: 11-28-2011, 03:13 PM
  2. Caesar Cipher help!!
    By darshan10 in forum C Programming
    Replies: 6
    Last Post: 10-19-2011, 04:58 PM
  3. Caesar Cipher
    By dldsob in forum C++ Programming
    Replies: 7
    Last Post: 07-06-2009, 06:06 PM
  4. caesar cypher
    By me77 in forum C Programming
    Replies: 17
    Last Post: 04-03-2009, 07:48 AM
  5. Caesar Cypher
    By Tommo in forum C Programming
    Replies: 10
    Last Post: 06-29-2007, 12:21 PM

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