Ghosh
Ghosh

Reputation: 19

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 ?

Upvotes: 0

Views: 74

Answers (1)

Yimin Rong
Yimin Rong

Reputation: 2027

Modern gcc seems to be able to handle UTF-8, so you can create a C header file like this:

বাংলা.h

#ifndef _BENGALI_H
#define _BENGALI_H

#include <stdio.h>

/* Please excuse my translation errors! */
#define পূর্ণসংখ্যা int
#define প্রধান  main
#define যখন while
#define প্রিন্ট printf
#define ফিরে    return

#endif /* BENGALI_H */

Then you can write programs like this with only minimal English:

বাংলা.c

#include "বাংলা.h"

পূর্ণসংখ্যা প্রধান()
{
    পূর্ণসংখ্যা পরিমাণ;
    পরিমাণ = 5;
    যখন (পরিমাণ--)
        প্রিন্ট("হ্যালো, বিশ্ব!\n");
    ফিরে 0;
}

Testing it out:

$ gcc -o বাংলা বাংলা.c 
$ ls
বাংলা.c    বাংলা.h    বাংলা             
$ ./বাংলা
হ্যালো, বিশ্ব!
হ্যালো, বিশ্ব!
হ্যালো, বিশ্ব!
হ্যালো, বিশ্ব!
হ্যালো, বিশ্ব!

If you're teaching to a beginner, this should be an okay start. You can even do the include in the gcc command line and wrap it with a script for even less English.

I'm not sure if this approach will work using Bengali–Assamese numerals as opposed to Hindu–Arabic numerals.

Upvotes: 1

Related Questions