- Joined
- Feb 4, 2009
- Messages
- 2
- Reaction score
- 0
I'm somewhat new to C and have been given an assignment to encrypt text by taking this code and adding the FilterInput() function to change lowercase text to uppercase and uppercase to lowercase, as well as remove non-alphabetic characters. I can only add this function and modify the encrypt() and decrypt() functions, but not main(). I'm not sure how to make these changes without editing main() and would appreciate any suggestions. I'm also having trouble sending a full string to the encode() function and from there to FilterInput(). My code is below.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//prototypes
char FilterInput( char str[], char out[] );
char encode ( char in );
char decode ( char in );
int main()
{
char orig[256], secret[256], show[256];
int i;
int len;
printf("Enter word: ");
fgets(orig, 255, stdin); //get the string to encode from the user
len = strlen( orig );
len--; //subtract 1 for \n
printf("Len is %d\n", len);
// Encode
for (i=0; i < len; ++i)
secret = encode(orig);
secret = '\0';
printf(" Encoded: ");
printf("%s\n", secret);
//Decode
for ( i=0; i < len; ++i)
show = decode(secret);
show = '\0';
printf(" Decoded: ");
printf("%s\n", show); //print the decoded string
return 0;
}
void FilterInput( char str[], char out[] )
{
int i = 0;
int j = 0;
int len = strlen( str ) - 1;
char c;
for (i = 0; i < len; ++i)
{ c = str;
if (c >= 'a' && c <= 'z')
out[j] = c;
j++;
}
return out;
}
char encode ( char )
{
char out;
out = FilterInput(in); //send the character through the FilterInput function for encoding
return out;
}
char decode ( char in )
{
char out;
out = FilterInput(in); //send the character through the FilterInput function for decoding
return out;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//prototypes
char FilterInput( char str[], char out[] );
char encode ( char in );
char decode ( char in );
int main()
{
char orig[256], secret[256], show[256];
int i;
int len;
printf("Enter word: ");
fgets(orig, 255, stdin); //get the string to encode from the user
len = strlen( orig );
len--; //subtract 1 for \n
printf("Len is %d\n", len);
// Encode
for (i=0; i < len; ++i)
secret = encode(orig);
secret = '\0';
printf(" Encoded: ");
printf("%s\n", secret);
//Decode
for ( i=0; i < len; ++i)
show = decode(secret);
show = '\0';
printf(" Decoded: ");
printf("%s\n", show); //print the decoded string
return 0;
}
void FilterInput( char str[], char out[] )
{
int i = 0;
int j = 0;
int len = strlen( str ) - 1;
char c;
for (i = 0; i < len; ++i)
{ c = str;
if (c >= 'a' && c <= 'z')
out[j] = c;
j++;
}
return out;
}
char encode ( char )
{
char out;
out = FilterInput(in); //send the character through the FilterInput function for encoding
return out;
}
char decode ( char in )
{
char out;
out = FilterInput(in); //send the character through the FilterInput function for decoding
return out;
}