Simple Cipher Program

This is a discussion on Simple Cipher Program within the C Programming forums, part of the General Programming Boards category; Hello everyone, i am in the beginning stages of creating my own caesar cipher and i am running into trouble ...

  1. #1
    Registered User PunchOut's Avatar
    Join Date
    Jun 2008
    Location
    norfolk, va
    Posts
    16

    Simple Cipher Program

    Hello everyone, i am in the beginning stages of creating my own caesar cipher and i am running into trouble with my cipher array.

    If you run the program below and just input something simple like "my name is john doe" . "array" returns what it is supposed to, but "cipher" returns the encrypted message plus some other garbage as shown below:
    "p|qdphlvu|dq�Rп���ȁ�Rп�".

    What can i do to correct this?

    here is the code:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define MAX 100
    
    main()
    {
    	char array[MAX], cipher[MAX];
    	int i, x, c;
    	i = x = 0;
    	while((c=getchar()) != '\n')
    	{
    		array[i] = c;
    		cipher[x] = (int) c + 3;
    		x++;
    		i++;
    	}
    	printf("%s\n", array);
    	printf("%s\n", cipher);
    }

  2. #2
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    You have to init your char arrays from garbage to clean empty fills of zeros
    Code:
    #include <stdio.h>
    
    int main() {
    	char array[100]={0}, cipher[100]={0};
    
    	int c;
    	while((c=getchar()) != '\n') {
    		static int x=0, i=0;
    
    		array[i++]=(char)c;
    		cipher[x++]=(char)(c+3);
    	}
    
    	printf("&#37;s\n",cipher);
    
    	return 0;
    }
    Last edited by Tux0r; 11-22-2008 at 10:11 AM.

  3. #3
    Registered User PunchOut's Avatar
    Join Date
    Jun 2008
    Location
    norfolk, va
    Posts
    16
    wow, that was quick!

    thanks for the insight, much appreciated
    Last edited by PunchOut; 11-22-2008 at 09:44 AM.

  4. #4
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Well the best way is to just init your char array to 0 as I did.
    edit: np
    Last edited by Tux0r; 11-22-2008 at 09:48 AM.

  5. #5
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    It's not really necessary to initiate the arrays to zero.

    You just need to NUL terminate the string.

  6. #6
    Registered User PunchOut's Avatar
    Join Date
    Jun 2008
    Location
    norfolk, va
    Posts
    16
    Quote Originally Posted by MeTh0Dz View Post
    It's not really necessary to initiate the arrays to zero.

    You just need to NUL terminate the string.
    is this what you mean?


    cipher[x++] = '\0'

  7. #7
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Quote Originally Posted by MeTh0Dz View Post
    It's not really necessary to initiate the arrays to zero.

    You just need to NUL terminate the string.
    It might not be necessary but if you do init char arrays, there is no need to worry about their usage later when it comes to this kind of problem.

    Quote Originally Posted by PunchOut View Post
    is this what you mean?


    cipher[x++] = '\0'
    This is the way he/she proposed:

    Code:
    #include <stdio.h>
    
    int main() {
    	char array[100], cipher[100];
    	int c=0, x=0, y=0;
    
    	while((c=getchar()) != '\n') {
    		array[x++]=(char)c;
    		cipher[y++]=(char)(c+3);
    	}
    
    	array[x]=0;
    	cipher[y]=0;
    
    	printf("&#37;s\n",cipher);
    
    	return 0;
    }
    Last edited by Tux0r; 11-22-2008 at 10:09 AM.

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    you need to add the size limit check - to prevent writing beyong bounds of the arrays
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  3. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 05:06 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM

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