Welcome back,
I am back with another explanation blog for the program we shared recently. The explanation is for the Jumble Crypter Program.
Here is the program itself,
You can see the program in written form below :
void jumbleString(char* str) {
int length = strlen(str);
for (int i = 0; i < length - 1; i += 2) {
char temp = str[i];
str[i] = str[i + 1];
str[i + 1] = temp;
}
}
void encryptString(char* str, int shift) {
int length = strlen(str);
for (int i = 0; i < length; i++) {
if (str[i] >= 'A' && str[i] <= 'Z') {
str[i] = ((str[i] - 'A') + shift) % 26 + 'A';
} else if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = ((str[i] - 'a') + shift) % 26 + 'a';
}
}
}
int main() {
char input_string[100];
char encrypted_form[100];
int shift;
printf("Enter a string: ");
fgets(input_string, sizeof(input_string), stdin);
input_string[strcspn(input_string, "\n")] = '\0'; // Remove the trailing newline from fgets
printf("Enter the encryption shift (a number between 1 and 25): ");
scanf("%d", &shift);
// Jumble the input string
jumbleString(input_string);
// Encrypt the jumbled string
strcpy(encrypted_form, input_string); // Make a copy of the jumbled string
encryptString(encrypted_form, shift);
// Output the results
printf("\nJumbled String: %s\n", input_string);
printf("Encrypted Form: %s\n", encrypted_form);
return 0;
}
First, we include the necessary header files: stdio.h for standard input/output functions and string.h for string manipulation functions.
Next, we define two functions: jumbleString() and encryptString(). These functions will be used to jumble the input string and encrypt it, respectively.
The jumbleString() function takes a character array (string) str as input and modifies it to jumble the characters. It does this by swapping every two adjacent characters in the string. For example, "hello" becomes "ehlol".
The encryptString() function takes two parameters: the character array str and an integer shift. It encrypts the string using a simple Caesar cipher. The Caesar cipher shifts each character in the string by the given shift value.
For uppercase letters, the shift is done within the range 'A' to 'Z', and for lowercase letters, it's done within the range 'a' to 'z'. The % 26 operation ensures that the shifted character wraps around the alphabet if it goes beyond 'Z' or 'z'.
In the main() function, we declare the necessary variables:
input_string: An array to store the user-provided input string.
encrypted_form: An array to store the encrypted form of the input string.
shift: An integer to hold the encryption shift value provided by the user.
The user is prompted to enter a string using printf() and fgets(). The fgets() function reads the input string and ensures it doesn't exceed the size of the input_string array.
The trailing newline character from fgets() is removed using strcspn() and replaced with '\0' to terminate the string properly.
The user is then prompted to enter the encryption shift value using printf() and scanf().
The input string is jumbled using the jumbleString() function.
A copy of the jumbled string is created using strcpy() and stored in the encrypted_form array.
The jumbled string in encrypted_form is encrypted using the encryptString() function and the provided shift value.
The jumbled string and the encrypted form are printed to the console using printf().
The program ends with return 0;
If you also want to learn basics and start your C programming journey then find a teacher today for free on Youtube.
I learnt alot from a teacher who teaches c in hindi and if you are finding someone who teaches in hindi then this person will definitely help you.
I am happy to call myself a trader and small programmer at the same time now.
Happy trading and keep learning what you love.