DEV Community

PONVEL M
PONVEL M

Posted on

Printing Characters and Their ASCII Values (Java, Python, JavaScript)

Introduction

In programming, every character has a numeric value called an ASCII value.

ASCII stands for American Standard Code for Information Interchange

It is used to represent characters like letters, numbers, and symbols in computers.


What is ASCII Value?

Each character is mapped to a number.

Examples:

  • 'A' → 65
  • 'a' → 97
  • '0' → 48
  • '@' → 64

Problem Statement

Given a string:

ponvelm2@gmail.com
Enter fullscreen mode Exit fullscreen mode

Print each character along with its ASCII value.


Logic Explanation

We need to:

  1. Traverse the string (loop)
  2. Take each character
  3. Convert it to ASCII value
  4. Print the result

Java Solution

public class Main {
    public static void main(String[] args) {
        String str = "ponvelm2@gmail.com";

        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            int ascii = (int) ch;

            System.out.println(ch + " -> " + ascii);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Python Solution

str = "ponvelm2@gmail.com"

for ch in str:
    print(ch, "->", ord(ch))
Enter fullscreen mode Exit fullscreen mode

JavaScript Solution

let str = "ponvelm2@gmail.com";

for (let i = 0; i < str.length; i++) {
  let ch = str[i];
  console.log(ch + " -> " + ch.charCodeAt(0));
}
Enter fullscreen mode Exit fullscreen mode

Sample Output

p -> 112
o -> 111
n -> 110
v -> 118
e -> 101
l -> 108
m -> 109
2 -> 50
@ -> 64
g -> 103
m -> 109
a -> 97
i -> 105
l -> 108
. -> 46
c -> 99
o -> 111
m -> 109
Enter fullscreen mode Exit fullscreen mode

Real-World Use Cases

This concept is useful in:

  • Data encoding
  • Encryption basics
  • Input validation
  • String manipulation problems
  • Competitive programming

Common Mistakes

Forgetting to loop through the string
Not converting character properly
Using wrong method (ord, charCodeAt, etc.)


Interview Tips

This is a basic but important question.
Interviewers may extend it to:

  • Count vowels and consonants
  • Remove special characters
  • Reverse a string
  • Find duplicate characters

Conclusion

Understanding ASCII values helps in building strong fundamentals in programming.

By practicing simple problems like this, you can improve your logic and problem-solving skills.

Top comments (0)