Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Homework HelpRSS Feeds

Program to encode and decode string

Posted By: Alisha Hughes     Category: C Programming     Views: 4408

Assume that the text consists of only A...Z and blank characters. The replacement should wrap around" the end of the character set A ..Z so that there is a well-defined replacement for each alphabet. Read text and print its decoded form until a character :: is found.

Code for Program to encode and decode string in C Programming

#include <stdio.h>
#include <conio.h>
#include <ctype.h>

void entryData();

void decodeFile();
int indexPosition(char);
char getEncodeChar(int);
char getDecodeChar(int);
void getWriteFileName(char *str);
void getReadFileName(char *str);

  char alpha[26] = "abcdefghijklmnopqrstuvwxyz";
  char filename[14];
  char readname[14];
  int option;
  FILE *fp;

  void main()
  {
    clrscr();
    printf("\nSelect any of the option from below.");
    printf("\n1. Create Encode File");
    printf("\n2. Reade Encode File\n");

    scanf("%d", &option);
    fflush(NULL);

      if( option == 1)
     getWriteFileName(filename);
      elseif( option == 2)
      {     getReadFileName(readname);
     decodeFile();
      }

    getch();
  }

   void entryData()
   {
       char chr;
       char prevchr;
       int index=-1;
       prevchr = chr;
       printf("\n Just type '::' to terminate typing. Start typing\n");
       fflush(NULL);

       while(1)
       {
       scanf("%c", &chr);
       chr = tolower(chr);

       if( chr == ':')
          if( prevchr == chr)
         break;

       if( isalpha(chr) )
       {
          index = indexPosition(chr);
          chr = getEncodeChar(index);
       }

       putc(chr, fp);
       prevchr = chr;
       }
   }

   void getWriteFileName(char *str)
   {
     printf("Enter Output File name maxium 14 character : ");
     scanf("%s", str);

     if( ( fp = fopen(str, "wt") ) == NULL  )
     {  fclose(fp);
    printf("\nInvalid file name entered.");
     }
     else
    entryData();
   }

   void getReadFileName(char *str)
   {
     printf("Enter Input File name maxium 14 character : ");
     scanf("%s", str);

     if( ( fp = fopen(str, "r+t") ) == NULL  )
     {  fclose(fp);
    printf("\nInvalid file name entered.");
     }
   }

   void decodeFile()
   {   char rchar;
       int index=-1;
       while( feof(fp) == 0 )
       {
       rchar = getc(fp);
       if( isalpha(rchar) )
       {
         index = indexPosition(rchar);
         rchar = getDecodeChar(index);
      }
      printf("%c", rchar);

       }
   }

   char getEncodeChar(int index)
   {
      int tmp=0;
      tmp = index + 5;
      if ( tmp > 25)
    tmp = tmp - 26;

      return alpha[tmp];
   }

   char getDecodeChar(int index)
   {
      int tmp=0;
      tmp = index - 5;
      if ( tmp < 0)
    tmp = 26 + tmp;

      return alpha[tmp];
   }

   int indexPosition(char chr)
   {
      int count=0;
      for( ; count < 26; count++)
      {
     if ( alpha[count] == chr)
       break;
      }
      return count;
   }

   /*******************Input*******************************
Select any of the option from below.
1. Create Encode File
2. Reade Encode File

1
Enter Output File name maxium 14 character : output.txt

contain of output.txt is
ymnx nx jcfruqj tk knqj jshtinsl.
:

2 Read Encoded File
******************* OutPut*******************************
Enter Input File name maxium 14 character : output.txt

this is example of file encoding.
:
*/
  
Share: 


Didn't find what you were looking for? Find more on Program to encode and decode string Or get search suggestion and latest updates.

Alisha Hughes
Alisha Hughes author of Program to encode and decode string is from London, United Kingdom.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!