OiO.lk Blog C# How to write a program which can convert Bengali words to equivalent keywords in C program?
C#

How to write a program which can convert Bengali words to equivalent keywords in C program?


I’m planning to teach basic programming to my 4 year old son who knows Bengali and very basic English. As at this age he will not be able to express his thoughts in a secondary language; I’m planning to write a program which will convert the Bengali phrases to C keywords.
Below is a very basic version

#include <stdio.h>
#include <string.h>


void translate_bengali_code(const char *bengali_code, char *translated_code) {
    const char *bengali_keywords[] = {"প্রিন্ট", "যদি", "অন্যথায়", "চলাও", "ফাংশন"};
    const char *english_keywords[] = {"print", "if", "else", "run", "function"};
    int num_keywords = sizeof(bengali_keywords) / sizeof(bengali_keywords[0]);

    char buffer[1024] = "";
    char *token;
    char code_copy[1024];

    strcpy(code_copy, bengali_code);
    
    token = strtok(code_copy, " ");
    while (token != NULL) {
        int found = 0;
        // Check if the token is a Bengali keyword and replace it
        for (int i = 0; i < num_keywords; i++) {
            if (strcmp(token, bengali_keywords[i]) == 0) {
                strcat(buffer, english_keywords[i]);
                found = 1;
                break;
            }
        }
        if (!found) {
            strcat(buffer, token);
        }
        strcat(buffer, " "); // Add space after each token
        token = strtok(NULL, " ");
    }

    buffer[strcspn(buffer, " ")] = 0;
    strcpy(translated_code, buffer);
}

int main() {
    const char bengali_code[] = "প্রিন্ট 'Hello, World!' যদি অন্যথায়";
    char translated_code[1024];

    translate_bengali_code(bengali_code, translated_code);
    
    printf("Original Bengali Code: %s\n", bengali_code);
    printf("Translated English Code: %s\n", translated_code);

    return 0;
}

How can I implement keywords/functions like array, for, do-while, switch-case ?



You need to sign in to view this answers

Exit mobile version