Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6252565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:49:42+00:00 2026-05-24T13:49:42+00:00

I’m trying to convert only the digits from a string to an int vector,

  • 0

I’m trying to convert only the digits from a string to an int vector, but that gives me the ASCII codes for numbers 0 to 9 instead.

Is there any way to convert only the digits to integers? I guess I’ll have to use a char array since atoi() don’t work with std::string and the c_str() method don’t work for every character, only the entire string.

#include <cctype>
#include <iostream>
#include <vector>

using namespace std;

int main() {
    string chars_and_numbers = "123a456b789c0";
    vector<int> only_numbers;

    for (int i = 0; i < chars_and_numbers.length(); i++) {
        if (isdigit(chars_and_numbers[i])) {
            cout << chars_and_numbers[i] << " ";
            only_numbers.push_back(int(chars_and_numbers[i]));
        }
    }

    cout << endl;

    for (vector<int>::iterator i = only_numbers.begin(); i != only_numbers.end(); i++) {
        cout << *i << " ";
    }

    return 0;
}

Output:

1 2 3 4 5 6 7 8 9 0
49 50 51 52 53 54 55 56 57 48
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:49:43+00:00Added an answer on May 24, 2026 at 1:49 pm
    ASCII Character    ASCII Code(decimal)  Literal Integer
          '0'               48                      0
          ...               ...                    ...
          '9'               57                      9
    

    int(chars_and_numbers[i]) returns you the underlying ASCII code of ASCII character instead of the literal integer what you want.

    Generally, 'i' - '0' results in i if i belongs to [0, 9].

    E.g., '1' - '0' returns you the distances between values of two ASCII characters(49 – 48), which is 1.

    int main() {
        string chars_and_numbers = "123a456b789c0";
        vector<int> only_numbers;
    
        for (int i = 0; i < chars_and_numbers.length(); i++) {
            if (isdigit(chars_and_numbers[i])) {
                cout << chars_and_numbers[i] << " ";
                // here is what you want
                only_numbers.push_back(chars_and_numbers[i] - '0');
            }
        }
    
        cout << endl;
    
        for (vector<int>::iterator i = only_numbers.begin(); i != only_numbers.end(); i++) {
            cout << *i << " ";
        }
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.