Question

C++ Coding Question on parsing a string If i have a string like "ADD X1, X2,...

C++ Coding Question on parsing a string

If i have a string like "ADD X1, X2, X3"

and i wanted to split the string as follows:
ADD
X1
X2
X3

what is a style of code i can use to parse the string into obtaining each of the following without taking things such as commas and spaces.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
using namespace std;
int main()
{
// enter a string from the user
cout<<"Enter a string:\n";
string str;
getline(cin,str);
// initialize a temporary word as empty string
string temp_word ="";
// iterate through the string
for(auto i : str)
{
// if space or comma occur then print the temporary word and initialize it back to empty string
if(i == ' ' || i == ',') // here you can write more special characters to consider
{
cout << temp_word << "\n";
temp_word = "";
}
// if space or comma doesn't occur then simply add another character to the temporary word
else
{
temp_word = temp_word + i;
}

}
// print the last word
cout<<temp_word<<"\n";
return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ Coding Question on parsing a string If i have a string like "ADD X1, X2,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 3) Let (x, y), (X2, y2), and (X3. Y3) be three points in R2 with X1...

    3) Let (x, y), (X2, y2), and (X3. Y3) be three points in R2 with X1 < x2 < X3. Suppose that y = ax + by + c is a parabola passing through the three points (x1, yı), (x2, y), and (x3, Y3). We have that a, b, and c must satisfy i = ax + bx + C V2 = ax + bx2 + c y3 = ax} + bx3 + c Let D = x X2 1....

  • Hi, I have a question about Atmel 8-bit AVR coding, so if you know the subject...

    Hi, I have a question about Atmel 8-bit AVR coding, so if you know the subject please help me! Please if you can, write code that will output an arbitrary string by blinking the LED(LED of the AVR Microcontroller board) in Morse code. The method you choose must output any standard null-terminated string containing characters A-Z or 0-9 in Morse code on the board's LED. For the demo, a child function, called from main( ) should output the string in...

  • 0 2.2.8. Suppose X1 and X2 have the joint pdf I e-le-22 21 > 0, X2...

    0 2.2.8. Suppose X1 and X2 have the joint pdf I e-le-22 21 > 0, X2 > 0 f(x1, x2) = elsewhere. For constants w1 > 0 and W2 > 0, let W = W1X1 + W2X2. (a) Show that the pdf of W is 1 -(e-w/wi – e-W/W2) W > 0 fw(W) = { W1-W2 C 10 elsewhere. (b) Verify that fw(w) > 0 for w > 0. (c) Note that the pdf fw(w) has an indeterminate form when...

  • /************************************************************************************ * Program: PRG/420 Week 5 * Purpose: Week 5 Coding Assignment * Programmer: TYPE YOUR...

    /************************************************************************************ * Program: PRG/420 Week 5 * Purpose: Week 5 Coding Assignment * Programmer: TYPE YOUR NAME HERE * Class: PRG/420 * Creation Date: TYPE TODAY'S DATE HERE ************************************************************************************* * Program Summary: * This program converts a given date to a string. * The code includes exception handling for a ParseException. ************************************************************************************/ package prg420week5_codingassignment; import java.util.*; // wildcard to import all the util. classes import java.text.*; // wildcard to import all the text classes public class PRG420Week5_CodingAssignment { public static...

  • I am needing to code a coffee mug in OpenGL using c++. I have found the coding for a tea kettle b...

    I am needing to code a coffee mug in OpenGL using c++. I have found the coding for a tea kettle but we have been told we cannot use a tea kettle. I have attached a photo for reference. Thank you for any help you can provide!

  • Some Extra Definitions Recall that, for a nonrandom real number c, and a random variable X,...

    Some Extra Definitions Recall that, for a nonrandom real number c, and a random variable X, we have Var (cX) = e Var (X). In this problem we'll generalize this property to linear combinations! Let be a vector of real nonrandom numbers, and let be a vector of random variables (sometimes called a random vector). Last, define the covariance matrix to be the matrix with all the covariances ar- ranged into a matrix. When we talk about taking the taking...

  • I am stuck with this coding problem from edx coding python 4.4.6: #This is a long...

    I am stuck with this coding problem from edx coding python 4.4.6: #This is a long one -- our answer is 20 lines of code, but #yours will probably be longer. That's because it's one of the #more authentic problems we've done so far. This is a real #problem you'll start to face if you want to start creating #useful programs. # #One of the reasons that filetypes work is that everyone #agrees how they are structured. A ".png" file,...

  • Please use C++ only Purpose : This stage involves recursion and recursion-like reasoning in coding while...

    Please use C++ only Purpose : This stage involves recursion and recursion-like reasoning in coding while also actively preparing a critical component necessary. Description : Computers don’t natively understand mathematical expressions in the way we prefer to write them. While we prefer “infix” notation, where the operators come between the values being operated upon, computers prefer either “prefix” or “postfix” notation, as these are unambiguous and easier for them to operate upon. As the project, at its core, is to...

  • C== LANGUAGE. Only use iostream and string header file. A bunch of checks i am also...

    C== LANGUAGE. Only use iostream and string header file. A bunch of checks i am also giving that needs to be satisfied for the code to be successful if (add("123", "456") != "579") if (add("123", "4") != "127") if (add("1234", "9") != "1243") if (add("88", "12") != "100") if (add("1234567890123456789", "10000000000000999") != "1244567890123457788") string longone(120, '2'); longone[0] = '3'; string longother(123, '1'); longother[0] = '4'; longother[3] = '2'; string longresult(123, '3'); longresult[0] = '4'; longresult[1] = '1'; longresult[2] = '1';...

  • I post this question but C, G, and H was not answered...can I have an answer for them please as soon as possible. Interpreting an LP output after solving the problem using the software. The follow...

    I post this question but C, G, and H was not answered...can I have an answer for them please as soon as possible. Interpreting an LP output after solving the problem using the software. The following linear programming problem has been solved using the software. Use the output to answer the questions below LINEAR PROGRAMMING PROBLEM: MAX 25X1+30X2+15X3 1) 4X1+5X2+8X3<1200 2) 9x1+15X2+3X3<1500 OPTIMAL SOLUTION: Objective Function Value- 4700.000 ariabl X1 x2 X3 0s 0.000 10.000 0.000 140.000 0.000 80.000 Less...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT