Question

In: Computer Science

Letter Separated Numbers (C++) I've encountered a corrupted database that has a series of important numbers,...

Letter Separated Numbers (C++)

I've encountered a corrupted database that has a series of important numbers, but instead of being separated by a space, the spaces have been randomly changed in alphabetic characters. Write a function named "my_numbers" that can take a string like "18y5638b-78" and return a vector of ints {18, 5638, -78}.

Thanks!

Solutions

Expert Solution

#include<iostream>

#include<vector>

#include<string>

using namespace std;

vector<int> my_numbers(string data){

    vector<int> numbers;

    string temp;

    // looping through string

    for(int i=0;(i<data.size() && data[i]!='\0');i++){

        // if digit then adding to temp

        if(isdigit(data[i])){

            temp += data[i];

            continue;

        }

        // for negative sign

        else if(data[i]=='-'){

            if(temp.size()>=1 && temp != "-"){

            numbers.push_back(stoi(temp));}

            temp = "-";

        }

        // adding to vecror and resetting temp

        else{

            if(temp.size()>=1){

                numbers.push_back(stoi(temp));}

            temp = "";

        }

    }

    if(temp.size()>=1)

    numbers.push_back(stoi(temp));

    

    return numbers;

}


// test function

int main(){

    vector<int> numbers = my_numbers("18y5638n-78");

    for(int i=0;i<numbers.size(); i++){

        cout<<numbers.at(i)<<"  ";

    }

    cout<<endl;

    return 0;

}

#compile and run

$ g++ -std=c++11 filterint.cpp

$ ./a.out

18 5638 -78


Related Solutions

Ask the user to input a series of numbers, write a C# program to output the...
Ask the user to input a series of numbers, write a C# program to output the sum, max, and min. Be sure to do error checking if the user input is not a number.
Write a C++ program that asks the user to enter a series of single-digit numbers with...
Write a C++ program that asks the user to enter a series of single-digit numbers with nothing separating them. Read the input as a C-string or a string object. The program should display the sum of all the single-digit numbers in the string. For example, if the user enters 2514, the program should display 12, which is the sum of 2, 5, 1, and 4. The program should also display the highest and lowest digits in the string. It is...
File input.txt contains 100,000 numbers separated by new line. Do the following: Write a C++ program...
File input.txt contains 100,000 numbers separated by new line. Do the following: Write a C++ program to compute the summation of these 100,000 numbers using single thread. Write a C++ program to compute the summation of these 100,000 numbers using 10 threads, meaning each thread compute 10,000 summations and the main thread sum the result of the 10 threads. Using the time syscall to compare the time spent for 1.1 and 1.2 Turn Ins: Source code of two programs and...
Every COBOL program I've browsed professionally has had Paragraph numbers. It is somewhat of Programmer Preference/Art...
Every COBOL program I've browsed professionally has had Paragraph numbers. It is somewhat of Programmer Preference/Art within some general guidelines established by the organization. Reference the references based on your searches, and provide: 1) What you think are the purposes & advantages of numbering paragraphs. 2) Guidelines you suggest for numbering paragraphs. Please do not cut/paste website text. I've already read that, and that is really the only wrong answer here. The idea of the assignment is assimilate, ascertain &...
(C++) Write a program to read from a grade database (data.txt). The database (text file) has...
(C++) Write a program to read from a grade database (data.txt). The database (text file) has students names, and grades for 10 quizzes.Use the given function prototypes to write the functions. Have main call your functions. The arrays should be declared in main() and passed to the functions as parameters. This is an exercise in parallel arrays, int and char 2 dim arrays. Function prototypes: int readData(ifstream &iFile, int scores[][10], char names[][30]); This functions takes the file stream parameter inFile...
Write a letter to the operations manager of your company who has omitted important procedures in...
Write a letter to the operations manager of your company who has omitted important procedures in operations. You work as a manufacturing operations analyst in an aerospace company
C programming language only! a file is given that has comma-separated integers. the files contents are...
C programming language only! a file is given that has comma-separated integers. the files contents are -1,-9,1,45,3,2,1,-1... Create a function that takes as an input a filename and returns an array containing the list of integers in the file
Code in C# Part 1 Build a Series class that has the following attributes and behaviors:...
Code in C# Part 1 Build a Series class that has the following attributes and behaviors: Attributes 1.) A name -- this can be any string value (i.e. “Firefly”) 2.) A number of episodes -- this can be any non-negative integer value 3.) An episode length -- this can be a double value, in hours, representing the length of an episode (i.e. 0.5 for a half-hour episode, or 0.37 for a 23 minute episode) 4.) A forKids attribute that holds...
A Scrabble game has identical copies of each letter tile numbering as follows: A-9, B-2, C-2,...
A Scrabble game has identical copies of each letter tile numbering as follows: A-9, B-2, C-2, D-4, E-12, F-2, G-3, H-2, I-9, J-1, K-1, L-4, M-2, N-6, O-8, P-2, Q-1, R-6, S-4, T-6, U-4, V-2, W-2, X-1, Y-2, Z-1 and Blanks-2. Seven tiles are randomly sampled without replacement from the above 100 tiles.If a letter appears exactly once in this seven-tile sample, we call it a singleton in this problem. a. Consider a letter that has n copies in total...
A bandpass series filter has parameter values of R=150Ω , C=2μF and L=3mH . The filter...
A bandpass series filter has parameter values of R=150Ω , C=2μF and L=3mH . The filter is energized with a voltage source which has an internal resistance equal to 100Ω. Using Matlab, write a code to print Bode magnitude plots for the filter transfer function by itself and the transfer function of the filter modified by the source resistance?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT