-1

-I'm trying to create a very basic translator in c (greek to english). I have a specific table to match character:
Α=A, Β=V, Γ=G, Δ=D, Ε=E, Ζ=Z, Η=H, Θ=8, Ι=I, Κ=K, Λ=L, Μ=M, Ν=N, Ξ=KS, Ο=O, Π=P, Ρ=R, Σ=S, Τ=T, Υ=Y, Φ=F, Χ=X, Ψ=PS, Ω=W.

Basically I want to translate iso8859-7 characters to iso8859-1 characters

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

char map_table_capital[] = {'a', 'v', 'g', 'd', 'e', 'z', 'h', '8', 'i', 'k', 'l', 'm', 'n', 'ks', 'o', 'p', 'r', 's', 't', 'y', 'f', 'x', 'ps', 'w'};

int main() {

  setlocale(LC_ALL, "el_GR.ISO8859-7");
  printf("Enter a string of Greek alphabet characters: ");
  char input[100];
  scanf("%s", input);

  int length = strlen(input);
  int i;

  for (i = 0; i < length; i++) {
    if (input[i] >= '\u03B1' && input[i] <= '\u03C9') {
      int index = input[i] - '\u03B1';
      input[i] = map_table_capital[index];
    }
  }
  printf("Mapped string: %s\n", input);

  return 0;
}

My problem is that whatever input I give ('α') I have the same output ('α') when I should get ('a')

So input: 'αβγ', should have as output: 'avg'. But I get as output: 'αβγ'

Τhe compiler gives me a warning about having 'ks' and 'ps' but I don't know how to fix it, also its giving a warning about "multi-character character constant" about the:

if (input_string[i] >= '\u03B1' && input_string[i] <= '\u03C9') {
      int index = input_string[i] - '\u03B1';

\u03B1 and \u03C9 which are basically 'α' and 'ω'. I tried basing this code of a simillar one that just reverses the english alphabet:

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

int main() {

  char input[100];
  char map_table[26] = {'z','y','x','w','v','u','t','s','r','q',
                        'p','o','n','m','l','k','j','i','h','g',
                        'f','e','d','c','b','a'};

  int i;
  printf("Enter a string: ");
  fgets(input, sizeof(input), stdin);

  int length = strlen(input);
  for (i = 0; i < length; i++) {
    if (input[i] >= 'a' && input[i] <= 'x') {

      int index = input[i] - 'a';
      input[i] = map_table[index];
    }
  }

  printf("Mapped string: %s", input);

  return 0;
}

If anyone has any idea I'd really appriciate it, thanks a lot!

EDIT1: I've gone a completly different route with the code:

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

#define MAX_LEN 8

int main() {
    char input_letter[MAX_LEN];
    char *mapping_table[] = {"A", "V", "G", "D", "E", "Z", "H", "8", "I", "K", "L", "M", "N", "KS", "O", "P", "R", "S", "T", "Y", "F", "X", "PS", "W"};
    char *greek_alphabet[] = {"Α", "Β", "Γ", "Δ", "Ε", "Ζ", "Η", "Θ", "Ι", "Κ", "Λ", "Μ", "Ν", "Ξ", "Ο", "Π", "Ρ", "Σ", "Τ", "Υ", "Φ", "Χ", "Ψ", "Ω"};

printf("Enter a Greek letter: ");
fgets(input_letter, MAX_LEN, stdin);
int len = strlen(input_letter);


    int num_alphabets = sizeof(greek_alphabet) / sizeof(greek_alphabet[0]);

    printf("%d\n", num_alphabets);
    int index = -1;
    int found = 0;

    for (int i = 0; i < num_alphabets; i++) {
        if (strcmp(input_letter, greek_alphabet[i]) == 0 ||
            strlen(input_letter) == len)) {
            printf("%d\n", i);
            index = i;
            found = 1;
            break;
        }
    }
    if (found == 1) {
        printf("Mapped to: %s\n", mapping_table[index]);
    } else {
        printf("Invalid input.\n");
    }

    return 0;
  }

Now my problem is that whatever greek character I give as input the output remains 'A', basically 'i' doesn't increase

1 Answers1

0

if (input[i] >= '\u03B1' && input[i] <= '\u03C9') is false because you compare the char input[i] with a universal character name which is larger than a char. gcc complaints that they are too large to fit a character literal, so you want to enclose those in double quotes instead. You use strcmp() to compare strings.

'ks' and 'ps' are not single characters. You either need to use a single character encoding or use a bigger type like char16_t or char32_t and possible universal character name in the same encoding as your input. You probably want your input and mapping table to use the same encoding.

Allan Wind
  • 23,068
  • 5
  • 28
  • 38