Untitled

mail@pastecode.io avatar
unknown
plain_text
24 days ago
1.2 kB
2
Indexable
Never
#include <stdio.h>
#define MAX_SIZE 10000
void encrypte(char *current_string, char *key);
int main(int argc, char *argv[])
{

	if (argc <= 1 || argc > 2) {
		return 0;
	}

	char *substituted_alphabet = argv[1];
	char current_string[MAX_SIZE];

	printf("Enter text:\n");

	while(fgets(current_string, MAX_SIZE, stdin) != NULL) {
		encrypte(current_string, substituted_alphabet);
		current_string[0] = '\0';
	}
	return 0;
}

void encrypte(char *current_string, char *key) {
	char result_string[MAX_SIZE];
	int index = 0;
	int output_index = 0;
	int i = 0;

	for(i = 0; current_string[i] != '\0'; i++) {
        if (current_string[i] == '\n') {
            current_string[i] = '\0';
            break;
        }
    }

	for(i = 0; current_string[i] != '\0'; i++) {
		if (current_string[i] >= 'A' && current_string[i] <= 'Z') {
			index = current_string[i] - 'A';
			result_string[output_index++] = key[index] - 'a' + 'A';
		} 
		else if(current_string[i] >= 'a' && current_string[i] <= 'z') {
			index = current_string[i] - 'a';
			result_string[output_index++] = key[index];
		} 
		else {
			result_string[output_index++] = current_string[i];
		}
	}
	result_string[i] = '\0';

	printf("%s\n", result_string);
}
Leave a Comment