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
Print each character along with its ASCII value.
Logic Explanation
We need to:
- Traverse the string (loop)
- Take each character
- Convert it to ASCII value
- 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);
}
}
}
Python Solution
str = "ponvelm2@gmail.com"
for ch in str:
print(ch, "->", ord(ch))
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));
}
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
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)