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!

Consider recursion to "get to the bottom" before starting.

#include <stdio.h>

void foo(FILE *file)
{
   char line [ 128 ], *ch;
   if ( fgets(line, sizeof line, file) )
   {
      foo(file);
      /*
       * Reached bottom.
       */
      fputs(line, stdout);
   }
}

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.

// 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;
}

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:

So I don't need an array for each sentence? Normal sentence and coded sentence?

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.

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.

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/programming-bbrown/cstart.htm

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.