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:
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:
The Solution:
#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.
Comments
Post a Comment