-4

Let's say I had a string. How would I be able to get the value of the first character of that string?

For example:

string str = "apple";

I want to get the first character of str which would return 97.

John
  • 1
  • 2

1 Answers1

1

What you're looking for is the ASCII-Value/Code of the specific character.

string str = "apple";
char c1;
char c2;

c1 = str[0];
c2 = str.at(0);

std::cout << static_cast<int>(c1) << std::endl;
std::cout << static_cast<int>(c2) << std::endl;
nikoksr
  • 342
  • 4
  • 10