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 6885753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:40:26+00:00 2026-05-27T05:40:26+00:00

I need to read a string in a following order: Read any amount of

  • 0

I need to read a string in a following order:

  1. Read any amount of numbers separated by spaces, discard all but the last one, saving it to n
  2. Read a space followed by n characters followed by space, save only the characters
  3. Read two more numbers separated by spaces and save them as well

I thought of using string stream to read the numbers and stop at the string, but I don’t know how to predict a string in string stream and stop reading numbers without “reading” the string as number and killing the string stream.
How predict a string and stop reading numbers before it?
Is there a better way to read this whole pattern?
I use C++11.

Edit:
Example input:

1 2 3 4 6 abc de 7 8

Excepted output:

The string: 'abc de'
Number 1: 7
Number 2: 8
  • 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-27T05:40:27+00:00Added an answer on May 27, 2026 at 5:40 am

    You can do that without using any regex, just by using standard C++ streams functionality. Here is an example using std::cin as input stream, but you can use a string stream if you want to read from a string.

    #include <iostream>
    #include <iomanip>
    #include <vector>
    
    int main(int argc, char* const argv[]) {
    
            int n,tmp;
    
            /// read integers, discarding all but the last
            while(std::cin >> tmp)
                    n = tmp;
            if(std::cin.bad()) {
                    std::cout << "bad format 1" << std::endl;
                    return -1;
            }
    
            /// skip whitespaces
            std::cin >> std::ws;
            std::cin.clear();
    
            /// read a string of 'n' characters
            std::vector<char> buffer(n+1, '\0');
            if(! std::cin.read(buffer.data(), n) ) {
                    std::cout << "bad format 2" << std::endl;
                    return -1;
            }
            std::string s(buffer.data());
    
            /// Read 2 numbers
            int nb1, nb2;
            if(! (std::cin >> nb1 >> nb2)) {
                    std::cout << "bad format 3" << std::endl;
                    return -1;
            }
    
            std::cout << "The string: " << s << std::endl;
            std::cout << "Number 1: " << nb1 << std::endl;
            std::cout << "Number 2: " << nb2 << std::endl;
            return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine the following: you read in a string with scanf() but you only need
I am writing a function in which I need to read a string contains
I need to read webpage and store its content in string for further processing.
I need a function to return a string that will only be accessed read-only.
I have read Java String class is immutable and thread-safe but I am still
Please how to read the following xml string. <NewDataset> <Table> <Id>1</Id> </Table> </NewDataset> i
i have the following string: http://pastebin.com/d29ae565b i need to separate each value and put
I need, in ANSI C, to open a file, to read all of its
I need to read a stream two times, from start to end. But the
Sometimes we deploy applications behind customer firewall and we need read only access to

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.