Separating Letters by Cases in C - Awesome Implementation

Have you ever wanted to decode a secret message hidden within a string of characters? Perhaps you enjoy unraveling mysteries or love the challenge of cracking codes. Well, get ready to reveal the hidden messages.

In this blog post, we'll explore a fascinating implementation where we separate letters by their cases using the C programming language. By doing so, we'll know the true meaning behind a seemingly random string of characters. So, let's dive right in and see how it's done!


The Challenge:

Imagine you're presented with the following string of characters:


HEif-yLLoO_Tu-dHERontE_-knMYo_w-NAjMohEn-do_IeS_PO-yoNu-D_aJArMe-noES_t-a-reaPl-prOogNramDmer

At first glance, it may look like a jumble of letters, numbers, and symbols with no discernible pattern. However, hidden within this chaos lies a secret message waiting to be revealed.


The Approach:

To uncover the hidden message, we'll utilize the power of C programming. Our strategy involves separating the letters based on their cases - uppercase and lowercase. By doing this, we'll isolate the components of the message and bring clarity to the chaos.

The Solution:

Let's take a look at the code snippet below:

#include <stdio.h>
#include <ctype.h>
// We have used ctype header file to access function that distinguishes cases

int main() {
    char *filename = "test.txt"; // File where the cipher text is located
    char *fileupper = "Uppercase.txt";
    char *filelower = "Lowercase.txt";
	
    // open the file for reading
    FILE *fr = fopen(filename, "r");
    // open the files for writing (append)
    FILE *fu = fopen(fileupper, "a");
    FILE *fl = fopen(filelower, "a");

    // checking if test file exist or not
    if (fr == NULL) {
        printf("Error: could not open file %s", filename);
        return 1;
    }

    // read one character at a time and
    // check if upper or not, and write it to designated file
    char ch;
    while ((ch = fgetc(fr)) != EOF){
    	if (isupper(ch) || ch == '_') fprintf(fu, "%c", ch); // The word is separated in UPPERCASE with an 'underscore'
    	if (islower(ch) || ch == '-') fprintf(fl, "%c", ch); // The word is separated in lowercase with a 'hyphen'
	}
      
    // close the file
    fclose(fr);
    fclose(fu);
    fclose(fl);
    
    return 0;
}

In this implementation, we start by reading the given string from a file named "test.txt". Then, we create two additional files: "Uppercase.txt" and "Lowercase.txt" to store the uppercase and lowercase letters respectively.

Next, we iterate through each character in the input file and check whether it is uppercase, lowercase, or specific separators like '_' and '-'. Based on the case of the letter, we write it to the corresponding output file - uppercase letters to "Uppercase.txt" and lowercase letters to "Lowercase.txt".

Finally, we close all the files and voila! We've successfully separated the letters by cases, revealing the hidden message within the string.


The Revelation:

After running the program, we can open the "Uppercase.txt" and "Lowercase.txt" files to uncover the hidden message. As we observe the contents of these files, the once mysterious string of characters transforms into a clear and meaningful message, waiting to be decoded.

In Further Post:

In the upcoming C program, we'll craft a fun twist on encryption by making a program that takes two meaningful messages and combine the letters from both the 'Uppercase.txt' file and 'Lowercase.txt' file at random, making it gibberish. Lastly, this program we made today will decipher the string that the upcoming program will make.

Thanks For Coding!       []~( ̄▽ ̄)~*

Comments

Popular posts from this blog

Simple Encryption using C Programming Language