String Caesar Cipher Program in C and C++

 String Caesar Cipher Program in C

           It is one of the simplest encryption technique in which each character in plain text is replaced by a character some fixed number of positions down to it.
#include <stdio.h>
void main()
{
    char s[50];
    int i, j=0, n;
   
    printf("Enter a String: ");
    gets(s);
    for(i=0; s[i]!=NULL; i++)
    {
      s[i] = s[i] + 1;
    if (s[i] > 'z') 
        s[i] = s[i] - 26;
    }
    printf("%s",s);
}

        Output:-


        Here I also change the case of Vowels in Upper Case.(Optional)
 #include <stdio.h>
void main()
{
    char s[50];
    int i, j = 0, n;
     int shift = 1;
    printf("Enter a String: ");
    gets(s);
    for(i=0; s[i]!=NULL; i++)
    {
      s[i] = s[i] + shift; //shift the character like h to e
    if (s[i] > 'z') 
        s[i] = s[i] - 26;
    }
    for(j=0; s[j]!=NULL; j++)
    {
        if(s[j]!='a' && s[j]!='e' && s[j]!='i' && s[j]!='o' && s[j]!='u')
        {
            continue;
        }
        else
        {
            s[j]=s[j]-32; //convert vowels in uppercase
        }
    }
    printf("%s",s);
}

        Output:- 

 

String Caesar Cipher Program in C ++

        It will also convert the case of vowels. 

#include <iostream>
using namespace std;
int main()
{
    char s[50];
    int i, j=0, n;
   
    cout<<"Enter a String: ";
    cin>>s;
    for(i=0; s[i]!=NULL; i++)
    {
      s[i] = s[i] + 1;
    if (s[i] > 'z') 
        s[i] = s[i] - 26;
    }
    for(j=0; s[j]!=NULL; j++)
    {
        if(s[j]!='a' && s[j]!='e' && s[j]!='i' && s[j]!='o' && s[j]!='u')
        {
            continue;
        }
        else
        {
            s[j]=s[j]-32;
        }
    }
    cout<<s;
    return 0;
}

        Output:- 

        

Comments