#include <iostream>
#include <vector>
#include <string>
int main() {
std::string s = "ADARSH"; // Example string
std::vector<int> charIndex(128, -1); // 128 for ASCII characters, initialized to -1
// Iterate over the string and store the first occurrence of each character
for (int i = 0; i < s.length(); i++) {
char currentChar = s[i];
int asciiValue = (int)currentChar; // Get ASCII value
// If this is the first occurrence of the character, store its index
if (charIndex[asciiValue] == -1) {
charIndex[asciiValue] = i;
}
}
// Print the first occurrence of each character, skipping characters that never appeared (-1)
std::cout << "Character\tASCII Value\tFirst Occurrence Index" << std::endl;
for (int i = 0; i < 128; i++) { // Loop over all possible ASCII values (0-127)
if (charIndex[i] != -1) { // If the character appeared in the string
char character = (char)i; // Convert ASCII value back to character
std::cout << character << "\t\t" << i << "\t\t" << charIndex[i] << std::endl;
}
}
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: