Home > OS >  How to change each specific char to an int in C
How to change each specific char to an int in C

Time:01-13

It might be a really dumb question, but I have tried to look it up, and have googled a bunch, but still can't figure out an easy way...

In C , saying that using namespace std;:

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    String N;

    cin >> N;
}

When user input is 123, N will be "123".

How do I cast '1' to int 1, and '2' to int 2, and '3' to int 3?

I cannot use %.

It would be awesome if I were to use an index approach in the string.

I would like to have a function that receives N and its index as parameters. For instance:

int func(string N, int curr_ind)
{
   // change curr_ind of N to a single int
   // for instance, "123" and 1, it would return 2.
}

CodePudding user response:

#include <iostream>
#include <string>

int get_digit_from_string(const std::string&s, int idx) {
  return static_cast<int>(s[idx] - '0');
}

int main() {
  std::string num{"12345"};

  for (std::size_t i = 0; i < num.length();   i) {
    std::cout << get_digit_from_string(num, i) << '\n';
  }
}

Just get the character at the index, subtract '0', and cast to int.

The subtraction is necessary, otherwise the character of a digit will be cast to the ASCII value of that character. The ASCII value of '0' is 48.

Output:

❯ ./a.out 
1
2
3
4
5

Now, just for fun, let's say you need frequent access to these digits. Ideally, you'd just do the conversion all at once and have these ints available to you. Here's one way of doing that (requires C 20):

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

std::vector<int> get_digits_from_string(const std::string& s) {
  std::vector<int> v;
  std::ranges::transform(s, std::back_inserter(v),
                         [](auto c) { return static_cast<int>(c - '0'); });

  return v;
}

int main() {
  std::string num{"12345"};
  std::vector<int> digits = get_digits_from_string(num);

  for (auto i : digits) {
    std::cout << i << '\n';
  }
}

We use the string to create a std::vector where each element is an int of the individual characters. I can then access the vector and get whatever digit I need easily.

CodePudding user response:

Another possibility:

#include <iostream>
#include <string>

int main()
{
    std::string input;
    std::cin >> input;

    // allocate int array for every character in input
    int* value = new int[input.size()];

    for (int i = 0; i < input.size();   i)
    {
        std::string t(1, input[i]);
        value[i] = atoi(t.c_str());
    }

    // print int array
    for (int i = 0; i < input.size();   i)
    {
        std::cout << value[i] << std::endl;
    }

    delete[] value;
}

Output:

x64/Debug/H1.exe
123
1
2
3

CodePudding user response:

Try this:

int func(string N, int curr_ind)
{
   return static_cast<int>(N[curr_ind]-'0');
}

Since the ASCII representation of consecutive digits differs by one, all you need to do to convert a character (char c;) representing a digit to the corresponding integer is: c-'0'

  • Related