Open In App

C Program to Determine the Unicode Code Point at a Given Index

Improve
Improve
Like Article
Like
Save
Share
Report

Unicode is the encoding mechanism to assign a unique number to each character. The first 128 characters are assigned as ASCII value means Unicode and ASCII value for the first 128 characters is the same. The first 128 characters contain the English Alphabet, Digits, Special Characters, etc. ASCII values are different for uppercase English alphabets and lowercase alphabets.

Types Starting Value Ending Value
Uppercase Alphabets 65 90
Lowercase Alphabets 97 122

From the table above one can understand that starting Uppercase letter i.e ‘A’ will have a value of 65 and ending at ‘Z’ with 90. Similarly lowercase ‘a’ is 97 and ends at ‘z’ with 122.

The C language has an implicit mapping of the ‘char’ data type to int which uses these Unicode values or ASCII values.

There are 2 ways to determine the unicode code point at a given index in C:

  1. Returning value at index after storing in “int” datatype.
  2. Using for Loop

1. Returning value at index after storing in int datatype

Below is the C++ program to determine Unicode code point at a given index:

C




// C++ program to determine unicode 
// code point at a given index
#include <stdio.h>
  
// Driver code
int main()
{
  char arr[10] = "GeEkS";
  int index1 = 0, index2 = 1, 
      index3 = 2, index4 = 3,
      index5 = 4, code;
  printf("The String is %s\n", arr);
  
  code = arr[index1];
  printf("The Unicode Code Point At %d is:%d\n"
          index1, code);
  code = arr[index2];
  printf("The Unicode Code Point At %d is:%d\n"
          index2, code);
  code = arr[index3];
  printf("The Unicode Code Point At %d is:%d\n"
          index3, code);
  code = arr[index4];
  printf("The Unicode Code Point At %d is:%d\n"
          index4, code);
  code = arr[index5];
  printf("The Unicode Code Point At %d is:%d\n"
          index5, code);
  return 0;
}


Output

The String is GeEkS
The Unicode Code Point At 0 is:71
The Unicode Code Point At 1 is:101
The Unicode Code Point At 2 is:69
The Unicode Code Point At 3 is:107
The Unicode Code Point At 4 is:83
  • Time Complexity: O(n) 
  • Space Complexity: O(2*n+1) = O(n)

2. Using for loop

If the string value is increased then it is not feasible to declare an individual variable for the index. Also if the string size is decreased then the fixed variables can give Out of Bound Error. To handle these situations we will use for loop to traverse the string and print the corresponding code point.

Below is the C program to determine unicode code point at a given index:

C




// C program to determine the unicode
// code point at a given index
#include <stdio.h>
  
// Driver code
int main()
{
  char arr[10] = "GeEkS";
  int code;
  printf("The String is %s\n"
          arr);
    
  for (int i = 0; arr[i] != '\0'; i++) 
  {
    code = arr[i];
    printf("The Unicode Code Point At %d is:%d\n"
            i, code);
  }
  return 0;
}


Output

The String is GeEkS
The Unicode Code Point At 0 is:71
The Unicode Code Point At 1 is:101
The Unicode Code Point At 2 is:69
The Unicode Code Point At 3 is:107
The Unicode Code Point At 4 is:83
  • Time Complexity: O(n)
  • Space Complexity: O(1)


Last Updated : 04 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads