Thread: cipher program need alil help

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    16

    cipher program need alil help

    hey, i need this encrypt function to accept more than one word as input spaces and punctuation. When i enter more than one word it skips to the end of the program. Any idea


    Code:
     
    
    #include <stdio.h>  
    #include <string.h> 
     
    // Declare the function 
    char ShiftCharacter(char unencrypt,int shift_value,int offset); 
     
     
    int main(int argc, char* argv[]) 
    { 
     
    	char buff[1024];  
    	int shift_value;  
    	int encrypt_text_length; 
    	int i; 
     
    	// Say hello  
    	puts("**initializing.....**\n\n**linking..........**\n\n");  
    	puts("Welcome agent Brown\n\n");  
    	puts("You have accessed the Caesar Cipher program\n\n");  
    	puts("Please enter the text you wish to encrypt: "); 
     
    	// Get input from the user 
    	// It is possible that the user may exceed our allocated space. 
    	// We could address that but it would cloud the issue. TODO: for later 
    	scanf("%s",&buff);   
     
    	puts("\nEnter your encryption shift value (anything from +-1 to 25): ");  
    	scanf("%d",&shift_value);  
     
    	// We now have a string and a shift value 
    	// TODO: Check for empty string and out of range value 
     
     
    	// Determine how long the string is 
    	encrypt_text_length = strlen(buff); 
     
    	// Encrypted the text 
    	for(i = 0; i < encrypt_text_length; i++) 
    	{ 
    		buff[ i] = ShiftCharacter(buff[ i],shift_value,i); 
    	}  
     
    	// Display the encrypted text for the user 
    	printf("\nYour encrypted text is: %s\n",buff);  
     
     
    	return 0; /* indicate successful completeion */  
    } 
     
     
    // This function will shift the character by the determined amount 
    // For our purpose we are only concerned with the letters a - z 
    char ShiftCharacter(char unencrypt,int shift_value,int offset) 
    { 
    
    int alphanumber; // The character as a number ( A = 1, B = 2, Z = 26 etc.)
    char encrypted_char = unencrypt;
     
    // Capital A - Z 
    if(unencrypt >= 'A' && unencrypt <= 'Z') 
    { 
    	alphanumber = unencrypt - 'A'; 
    	alphanumber += shift_value + offset; 
    	alphanumber = (alphanumber % 26); 
    	encrypted_char  = (char)(alphanumber + 'A'); 
     
    } 
     
    // Lowercase a - z 
    else if(unencrypt >= 'a' && unencrypt <= 'z') 
    { 
    	alphanumber = unencrypt - 'a'; 
    	alphanumber += shift_value + offset; 
    	alphanumber = (alphanumber % 26); 
    	encrypted_char  = (char)(alphanumber + 'a'); 
    } 
     
    return encrypted_char; 
     
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    	scanf("%s",&buff);
    Getting input this way will stop at the first whitespace character. Try using fgets() instead.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    // Capital A - Z 
    if(unencrypt >= 'A' && unencrypt <= 'Z') 
    {
    Try isupper(), in <ctype.h>. There's an islower() as well.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    16
    haha yeah i fixed it. Ive been trying to reset the shift value for every word entered. This is what i have so far but it isnt working.


    Code:
     
    
    #include <stdio.h>  
    #include <string.h> 
     
    // Declare the function 
    char ShiftCharacter(char unencrypt,int shift_value,int offset); 
     
     
    int main(int argc, char* argv[]) 
    { 
     
    	char buff[1024];  
    	int shift_value;  
    	int encrypt_text_length; 
    	int i; 
    	int x; 
    	int y = 0; 
     
    	// Say hello  
    	puts("**initializing.....**\n\n**linking..........**\n\n");  
    	puts("Welcome agent Brown\n\n");  
    	puts("You have accessed the Caesar Cipher program\n\n");  
    	puts("Please enter the text you wish to encrypt: "); 
     
    	// Get input from the user 
    	// It is possible that the user may exceed our allocated space. 
    	// We could address that but it would cloud the issue. TODO: for later 
    	 fgets(buff, sizeof buff, stdin);   
     
    	puts("\nEnter your encryption shift value (anything from +-1 to 25): ");  
    	scanf("%d",&shift_value);  
     
    	// We now have a string and a shift value 
    	// TODO: Check for empty string and out of range value 
     
     
    	// Determine how long the string is 
    	encrypt_text_length = strlen(buff); 
     
    	// Encrypted the text 
    	for(i = 0; i < encrypt_text_length; i++) 
    	{ 
    		buff[ i] = ShiftCharacter(buff[ i],shift_value,i); 
    	}
    
     
    // Encrypted the text  
    	for(x = 0; x < encrypt_text_length; x++)  
    	{  
         // if the current character is a space reset the offset to zero 
         // otherwise add one to the offset 
         if(buff[i] == ' ') 
         { 
              y = 0; 
         } 
         else 
         { 
              y++; 
         } 
     
         buff[i] = ShiftCharacter(buff[i],shift_value,i);   
    	}
    
     
    	// Display the encrypted text for the user 
    	printf("\nYour encrypted text is: %s\n",buff);  
     
     
    	return 0; /* indicate successful completeion */  
    } 
     
     
    // This function will shift the character by the determined amount 
    // For our purpose we are only concerned with the letters a - z 
    char ShiftCharacter(char unencrypt,int shift_value,int offset) 
    { 
    
    int alphanumber; // The character as a number ( A = 1, B = 2, Z = 26 etc.)
    char encrypted_char = unencrypt;
     
    // Capital A - Z 
    if(unencrypt >= 'A' && unencrypt <= 'Z') 
    { 
    	alphanumber = unencrypt - 'A'; 
    	alphanumber += shift_value + offset; 
    	alphanumber = (alphanumber % 26); 
    	encrypted_char  = (char)(alphanumber + 'A'); 
     
    } 
     
    // Lowercase a - z 
    else if(unencrypt >= 'a' && unencrypt <= 'z') 
    { 
    	alphanumber = unencrypt - 'a'; 
    	alphanumber += shift_value + offset; 
    	alphanumber = (alphanumber % 26); 
    	encrypted_char  = (char)(alphanumber + 'a'); 
    } 
     
    return encrypted_char; 
     
    }
    Last edited by Uber Fr0g; 12-14-2005 at 09:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM