Latin alphabet cipher

The Latin Alphabet Cipher Encryption Technique is one of the earliest and simplest techniques of encrypting data. It’s simply a type of substitution cipher technique, i.e., each letter of a given text is substituted by its corresponding number as represented in its alphabetical order. For Example, we have given a string as “hello everyone” then its Latin Cipher Encryption will be “8 5 12 12 15 5 22 5 18 25 15 14 5”.

Examples:

Input :  geeksforgeeks
Output : Encrypted Code using Latin Alphabet
         7 5 5 11 19 6 15 18 7 5 5 11 19 

Input :  hello everyone
Output : Encrypted Code using Latin Alphabet
         8 5 12 12 15  5 22 5 18 25 15 14 5 

Prerequisite : isalpha() and isdigit() functions in C/C++ with example

Below is the program to convert a given string to its Latin Alphabet cipher:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Latin Alphabet Cipher Encryption header files
#include <bits/stdc++.h>
  
// function for calculating the encryption
void cipher(char str[])
{
    for (int i = 0; str[i] != '\0'; i++) {
        if (isalpha(str[i]) == 0 && str[i] != ' ') {
            printf("Enter only alphabets and space\n");
            return;
        }
    }
  
    printf("Encrypted Code using Latin Alphabet\n");
    for (int i = 0; str[i] != '\0'; i++) {
  
        if (str[i] >= 'A' && str[i] <= 'Z')
            printf("%d ", str[i] - 'A' + 1);
  
        else if (str[i] >= 'a' && str[i] <= 'z')
            printf("%d ", str[i] - 'a' + 1);
        if (str[i] == ' ')
            printf("%c", str[i]);
    }
    printf("\n");
}
  
// driver code
int main()
{
    char str[] = "geeksforgeeks";
    cipher(str);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate 
// Latin Alphabet Cipher
  
class LatinCipher 
{
    // function for calculating the encryption
    static void cipher(String str)
    {
        for (int i = 0; i < str.length(); i++)
        {
            if (!Character.isLetter(str.charAt(i)) &&
            str.charAt(i) != ' '
            {
                System.out.println("Enter only alphabets and space");
                return;
            }
        }
  
        System.out.println("Encrypted Code using Latin Alphabet");
        for (int i = 0; i < str.length(); i++)
        {
            if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z')
            {
                System.out.print(str.charAt(i) - 'A' + 1 + " ");
            }
            else if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z'
            {
                System.out.print(str.charAt(i) - 'a' + 1 + " ");
            }
            if (str.charAt(i) == ' ')
                System.out.print(str.charAt(i));
  
        }
        System.out.println();
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        String str = "geeksforgeeks";
        cipher(str);
    }
}
  
// This code is contributed by Vivekkumar Singh

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate 
# Latin Alphabet Cipher
  
# function for calculating the encryption
def cipher(str):
  
    for i in range(len(str)):
        if str[i].isalpha() == 0 and str[i] != " ":
            print("Enter only alphabets and space")
            return
  
  
    print("Encrypted Code using Latin Alphabet")
    for i in range(len(str)):
  
        if str[i] >= "A" and str[i] <= "Z":
            print(ord(str[i])-ord("A")+1, end=" ")
          
        elif str[i] >= "a" and str[i] <= 'z':
            print(ord(str[i])-ord("a")+1, end=" ")
          
        if str[i] == " ":
            print(str[i])
      
    print()
  
  
# Driver Code
if __name__ == "__main__":
    str = "geeksforgeeks"
    cipher(str)
  
# This code is contributed by 
# sanjeev2552

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate 
// Latin Alphabet Cipher
using System;
      
public class LatinCipher 
{
    // function for calculating the encryption
    static void cipher(String str)
    {
        for (int i = 0; i < str.Length; i++)
        {
            if (!char.IsLetter(str[i]) &&
            str[i] != ' '
            {
                Console.WriteLine("Enter only alphabets and space");
                return;
            }
        }
  
        Console.WriteLine("Encrypted Code using Latin Alphabet");
        for (int i = 0; i < str.Length; i++)
        {
            if (str[i] >= 'A' && str[i] <= 'Z')
            {
                Console.Write(str[i] - 'A' + 1 + " ");
            }
            else if (str[i] >= 'a' && str[i] <= 'z'
            {
                Console.Write(str[i] - 'a' + 1 + " ");
            }
            if (str[i] == ' ')
                Console.Write(str[i]);
  
        }
        Console.WriteLine();
    }
  
    // Driver Code
    public static void Main(String[] args)
    {
        String str = "geeksforgeeks";
        cipher(str);
    }
}
  
// This code has been contributed by 29AjayKumar

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Latin Alphabet Cipher 
// Encryption header files
  
// function for calculating
// the encryption
function cipher($str)
{
        if (!ctype_alpha($str))
        {
            printf("Enter only "
                   "alphabets and space\n");
            return;
        }
  
    printf("Encrypted Code using ");
    printf("Latin Alphabet\n");
    for ($i = 0; $i < strlen($str); $i++) 
    {
        if ($str[$i] >= 'A' && 
            $str[$i] <= 'Z')
            echo (ord($str[$i]) - 
                    65 + 1). " ";
  
        else if ($str[$i] >= 'a' && 
                 $str[$i] <= 'z')
            echo (ord($str[$i]) - 
                    97 + 1). " ";
  
    }
    echo "\n";
}
  
// Driver Code
$str = "geeksforgeeks";
cipher($str);
  
// This code is contributed by mits.
?>

chevron_right


Output:

Encrypted Code using Latin Alphabet
7 5 5 11 19 6 15 18 7 5 5 11 19


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.