Thread: Character encoding: encoded_character = 10 + (plaintext_character - 'a') + 'A'

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    12

    Character encoding: encoded_character = 10 + (plaintext_character - 'a') + 'A'

    This is my code so far

    char encoded_character = NULL, plaintext_character = NULL;




    //3.1 Get encoded_character and plaintext_character from user
    printf("Please input your plaintext character ");
    //3.2 Scan
    scanf("%c", &plaintext_character);
    //3.3 Charcater encoded
    encoded_character = 10 + (plaintext_character - 'a') + 'A';
    //3.4 printf encoded character
    printf("encoded_character is = %c\n",encoded_character);




    but it does not run. What did i do wrong?

  2. #2
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    NULL is defined as the pointer type ((void *)0) in libio.h, but you are assigning it to a char type. And the assignment is redundant anyway because you scan in and compute other values for your chars before printing them. This code works fine if you just leave out the = NULL.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by christophergray View Post
    NULL is defined as the pointer type ((void *)0) in libio.h, but you are assigning it to a char type. And the assignment is redundant anyway because you scan in and compute other values for your chars before printing them. This code works fine if you just leave out the = NULL.
    To make this code fine - need to check return value of scanf before using any variable it filled. And also there is library function for converting to upper case that works even if the encoding layout is different from the supposed in this code.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main() {
    
        char encoded_character = 0;
        char plaintext_character = 0;
        int ret;
        //3.1 Get encoded_character and plaintext_character from user
        printf("Please input your plaintext character ");
        //3.2 Scan
        ret = scanf("%c", &plaintext_character);
        if(ret != 1){
            printf("Read failed\n");
            return -1;
        }
    
        //3.3 Charcater encoded
        encoded_character = toupper(plaintext_character);
        //3.4 printf encoded character
        printf("encoded_character is = %c\n",encoded_character);
        
        return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using facet to covert from encoding to encoding
    By Dave11 in forum C++ Programming
    Replies: 6
    Last Post: 07-28-2015, 07:33 AM
  2. Encoding a data structure based on TLV encoding
    By Sajas K K in forum C++ Programming
    Replies: 2
    Last Post: 02-15-2013, 10:39 PM
  3. How to convert string in url encoding to html encoding?
    By Jerel2k11 in forum C Programming
    Replies: 6
    Last Post: 11-06-2011, 09:05 AM
  4. <string> to LPCSTR? Also, character encoding: UNICODE vs ?
    By Kurisu33 in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 12:48 AM
  5. Character Encoding
    By bithub in forum Windows Programming
    Replies: 3
    Last Post: 01-07-2006, 01:39 AM

Tags for this Thread