Write a program to encode text and to decode the encoded text.

Perfrom the encoding so that every character is replaced by its next character. For example reaplce a by b, b by c and so on. Replace z by a.

Example:
plain text: Program
Encoded text: Qsphsbn
Decoded text: Program


Difficulty level
This exercise is mostly suitable for students
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define SIZE 100
 
void main()
{
	char plain[SIZE], encoded[SIZE], decoded[SIZE];
    	int i, j;
 
	printf("Enter  a string : ");
	gets(plain);	
 
    	for(i=0; plain[i]!='\0'; i++)
	{
		switch(plain[i])
		{
			case 'Z': encoded[i]='A'; break;
			case 'z': encoded[i]='a'; break;
			default: encoded[i]=plain[i]+1;
		}
	}
	encoded[i]='\0';

    	for(i=0; encoded[i]!='\0'; i++)
	{
		switch(encoded[i])
		{
			case 'A': decoded[i]='Z'; break;
			case 'a': decoded[i]='z'; break;
			default: decoded[i]=encoded[i]-1;
		}
	}
	decoded[i]='\0';

	printf("Plain text: %s\n", plain);
	printf("Encoded text: %s\n", encoded);
	printf("Decoded text: %s\n", decoded);
	getch();
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Number of occurrence of a string into another one