Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 445539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:22:52+00:00 2026-05-12T21:22:52+00:00

I’m just learning about C and got an assignment where we have to translate

  • 0

I’m just learning about C and got an assignment where we have to translate plain text into morse code and back. (I am mostly familiar with Java so bear with me on the terms I use).

To do this, I have an array with the strings for all letters.

char *letters[] = {
".- ", "-... ", "-.-. ", "-.. ", ".", "..-." etc

I wrote a function for returning the position of the desired letter.

int letter_nr(unsigned char c)
{
    return c-97;
}

This is working, but the assignment specifications require the handling of the Swedish umlauted letters åäö. The Swedish alphabet is the same as the English with these three letters in the end. I tried checking for these, like so:

int letter_nr(unsigned char c)
{
    if (c == 'å')
        return 26;
    if (c == 'ä')
        return 27;
    if (c == 'ö')
        return 28;
    return c-97;
}

Unfortunately, when I tried testing this function, I get the same value for all of these three: 98. Here is my main, testing function:

int main()
{   
    unsigned char letter;

    while(1)
    {
        printf("Type a letter to get its position: ");
        scanf("%c", &letter);
        printf("%d\n", letter_nr(letter));
    }
    return 0;
}

What can I do to resolve this?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T21:22:53+00:00Added an answer on May 12, 2026 at 9:22 pm

    In general encoding stuff is quite complicated. On the other hand if you just want a dirty solution specific to your compiler/platform than add something like this to your code:

    printf("letter 0x%x is number %d\n", letter, letter_nr(letter));
    

    It will give hex value for your umlauts. Than just replace in if statements your letter with number.

    EDIT You say that you are always getting 98 so your scanf got 98 + 97 = 195 = 0x3C from console. According to this table 0x3C is start of UTF8 sequence for common LATIN SMALL LETTER N WITH Something in Latin1 block. You are on Mac OS X ?

    EDIT This is my final call. Quite hackery but it works for me 🙂

    #include <stdio.h>
    
    // scanf for for letter. Return position in Morse Table. 
    // Recognises UTF8 for swedish letters.
    int letter_nr()
    {
      unsigned char letter;
      // scan for the first time,
      scanf("%c", &letter);
      if(0xC3 == letter)
      {
        // we scanf again since this is UTF8 and two byte encoded character will come
        scanf("%c", &letter);
        //LATIN SMALL LETTER A WITH RING ABOVE = å
        if(0xA5 == letter)
          return 26;
        //LATIN SMALL LETTER A WITH DIAERESIS = ä
        if(0xA4 == letter)
          return 27;
       // LATIN SMALL LETTER O WITH DIAERESIS = ö
        if(0xB6 == letter)
          return 28;
    
        printf("Unknown letter. 0x%x. ", letter);
        return -1;
      } 
      // is seems to be regular ASCII
      return letter - 97;
     } // letter_nr
    
    int main()
    {   
        while(1)
        {
            printf("Type a letter to get its position: ");
    
            int val = letter_nr();
            if(-1 != val)
              printf("Morse code is %d.\n", val);
            else
              printf("Unknown Morse code.\n");
    
            // strip remaining new line
        unsigned char new_line;
        scanf("%c", &new_line);         
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.