Java Program to Determine the Unicode Code Point at a given index

JavaCampus InterviewServer Side ProgrammingProgramming

In this article, we will understand how to determine the unicode code point at a given index. Each character is represented by a unicode code point. A code point is an integer value that uniquely identifies the given character. Unicode characters can be encoded using different encodings, like UTF-8 or UTF-16.

Below is a demonstration of the same −

Suppose our input is

Input String: Java Program
Index value: 5

The desired output would be

Unicode Point: 80

Algorithm

Step 1 - START
Step 2 - Declare a string value namely input_string and two integer values namely index and result
Step 3 - Define the values.
Step 4 - Use the function codePointAt() to fetch the code point value. Store the value as result.
Step 5 - Display the result
Step 6 - Stop

Example 1

Here, we bind all the operations together under the ‘main’ function.

import java.io.*;
public class UniCode {
   public static void main(String[] args){
      System.out.println("Required packages have been imported");
      String input_string = "Java Program";
      System.out.println("\nThe string is defined as: " +input_string);
      int result = input_string.codePointAt(5);
      System.out.println("The unicode point at index 5 is : " + result);
   }
}

Output

Required packages have been imported

The string is defined as: Java Program
The unicode point at index 5 is : 80

Example 2

Here, we encapsulate the operations into functions exhibiting object-oriented programming.

import java.io.*;
public class UniCode {
   static void unicode_value(String input_string, int index){
      int result = input_string.codePointAt(index);
      System.out.println("The unicode point at index " +index +"is : " + result);
   }
   public static void main(String[] args) {
      System.out.println("Required packages have been imported");
      String input_string = "Java Program";
      System.out.println("\nThe string is defined as: " +input_string);
      int index = 5;
      unicode_value(input_string, index);
   }
}

Output

Required packages have been imported

The string is defined as: Java Program
The unicode point at index 5is : 80
raja
Updated on 29-Mar-2022 11:39:21

Advertisements