Encoding/Decoding

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2004
Posts: 11
Reputation: tyalangan is an unknown quantity at this point 
Solved Threads: 0
tyalangan tyalangan is offline Offline
Newbie Poster

Encoding/Decoding

 
0
  #1
Dec 1st, 2004
Hey everyone. Been doing C for a little while now and this website has helped me in advancing my knowledge and skills in C programming.

I have another question now... it's really bugging me actually... I'm trying to make a program basically just encode and decode. It seems too simple for me not to understand but... I'm teaching myself so that's probably the problem.

I'm trying to get something like this...

First Sentence: See Spot Run
Second Sentence: FSS FIJW BNK

but then I also want to output the opposite...
so if I enter

First Sentence: FSS FIJW BNK
I'll get
Second Sentence: See Spot Run or (SEE SPOT RUN, whichever)

If anyone could push me in the right direction or have any programs they could show me that'd be great. Then I can build on it from there!
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,442
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Encoding/Decoding

 
0
  #2
Dec 1st, 2004
Consider recursion to "get to the bottom" before starting.
  1. #include <stdio.h>
  2.  
  3. void foo(FILE *file)
  4. {
  5. char line [ 128 ], *ch;
  6. if ( fgets(line, sizeof line, file) )
  7. {
  8. foo(file);
  9. /*
  10.   * Reached bottom.
  11.   */
  12. fputs(line, stdout);
  13. }
  14. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,109
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 943
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Encoding/Decoding

 
0
  #3
Dec 2nd, 2004
One of the ways to encode and decode using the same function is with a bitwise xor. Now you are making it more difficult demanding that the encoded sentence be in characters that are on the keyboard. This little code sample does it using a set pass-key and an exception handler.
[php]
// this simple xor crypt may fool mom, but not the FBI
// all encoded characters should be on the keyboard
// Pelles C

// VPW = 4 only gives one non-key char
#define VPW 4
#define EOS 0

#include <stdio.h>
#include <string.h>

char *crypt(char *str);

int main()
{
static char str[80];

while(1)
{
printf("Enter a string (nothing to exit): ");
gets(str);
if (strlen(str) == 0)
break;
printf("Original : %s\n",str);
printf("Encrypted : %s\n",crypt(str));
}

getchar(); // wait
return 0;
}

char *crypt(char *str)
{
int k, n;
char c1, c2;
char cpw; // password char
static char temps[80]; // hold the crypted string

for (k = 0, n = 0; k < strlen(str); k++, n++)
{
c1 = str[k];
cpw = VPW;
// ^ is the bitwise xor operator
c2 = (c1 ^ cpw);

// keep the spaces
if (c1 == ' ')
c2 = ' ';
// FOR VPW = 4 one exception needed, d gives non-key char
if (c1 == 'd')
c2 = '>';
if (c1 == '>')
c2 = 'd';

temps[k] = c2; // build the temporary string
}
temps[k++] = EOS; // end it correctly
return temps;
}
[/php]
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,442
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Encoding/Decoding

 
0
  #4
Dec 2nd, 2004
Originally Posted by Dave Sinkula
Consider recursion to "get to the bottom" before starting.
Man. I've just seen too many questions with common-looking wording but different topics. I think it's time to give these eyes a rest. Geez. Bad me. :rolleyes:
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 11
Reputation: tyalangan is an unknown quantity at this point 
Solved Threads: 0
tyalangan tyalangan is offline Offline
Newbie Poster

Re: Encoding/Decoding

 
0
  #5
Dec 6th, 2004
So I don't need an array for each sentence? Normal sentence and coded sentence?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,109
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 943
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Encoding/Decoding

 
0
  #6
Dec 6th, 2004
Originally Posted by tyalangan
So I don't need an array for each sentence? Normal sentence and coded sentence?
Well, the function crypt(str) acts like your coded sentence. You can assign it to a string variable to give it a more permanent home.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 11
Reputation: tyalangan is an unknown quantity at this point 
Solved Threads: 0
tyalangan tyalangan is offline Offline
Newbie Poster

Re: Encoding/Decoding

 
0
  #7
Dec 8th, 2004
I appologize but I really do not understand this code you've given me. I just know basic C and "crypt" and "VPW" and "EOS" all make no sense to me. Is there anyway you could show me a more basic code? That way once I understand the basic code it will allow me to understand and make more advanced programs. Thank you.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,109
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 943
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Encoding/Decoding

 
0
  #8
Dec 9th, 2004
Originally Posted by tyalangan
I appologize but I really do not understand this code you've given me. I just know basic C and "crypt" and "VPW" and "EOS" all make no sense to me. Is there anyway you could show me a more basic code? That way once I understand the basic code it will allow me to understand and make more advanced programs. Thank you.
Okay, crypt is the function that does the encrypting/decrypting, sort of a toggle. VPW is a fixed passkey, and EOS is the end of string character, a zero, used by any C string. So, where ever it says VPW think a 4 and where ever it says EOS think a 0 (zero). Just one way to give constants a more meaningfull name. I will try to come up with a simpler code for you.

For a good intro to C see:
http://gd.tuwien.ac.at/languages/c/p...own/cstart.htm
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC