Question

In: Computer Science

c++ The input items are given in one line and each item is separated by white...

c++

The input items are given in one line and each item is separated by white spaces (blank-space and tab).

how can I implement?

and

input:

34 peach 7

output:

number: 34 7

string: peach

How can I separate the numbers and characters that I type at once?

Solutions

Expert Solution

#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
  
// function to count total words in input string
int countWords(string str)
{
istringstream is(str);
int count = 0;
// Traverse through the input string
do {
string word;
is >> word;
count++; // increase count
} while (is);
  
return count; // return count
}

void printNumbers(string str,int count){
istringstream is(str);
cout<<"number: ";
// Traverse through the input string
do {
string word;
is >> word;
if (isdigit(word[0]) == true){ // check if word is number
cout<<word<<" "; // if yes print number
}
} while (is);
  
}

void printStrings(string str, int count){
istringstream is(str);
cout<<"string: ";
// Traverse through the input string
do {
string word;
is >> word;
if (isdigit(word[0]) == false){ // check if word is number
cout<<word<<" "; // if not print string
}
} while (is);
}


  
// Main function
int main()
{
string input;
  
cout << "Please enter your input: \n";
getline (cin, input);
  
int count = countWords(input); // count words in string
printNumbers(input,count); // print numbers
cout<<endl;
printStrings(input,count); // print strings
return 0;
}

// Output


Related Solutions

The input file Each line of the input file will contain a sentence with words separated...
The input file Each line of the input file will contain a sentence with words separated by one space. Read a line from the listed below  and use a StringTokenizer to extract the words from the line. The input file . Mary had a little lamb whose fl33ce was white as sn0w And everywhere that @Mary went the 1amb was sure to go. Read the above that contains a paragraph of words. Put all the words in an array, put the...
/** * Loads a list of items from the given file. Each line is in the...
/** * Loads a list of items from the given file. Each line is in the format * <item type>~<item data>. The createFromString() methods in the item * classes will be used to parse the item data. * @param filename the filename * @return the list of items * @throws IOException if the file can't be opened */ public static List<Item> loadItems(String filename) throws IOException { List<Item> items = new ArrayList<>(); Scanner inPut = new Scanner(new FileReader(filename));        while(inPut.hasNextLine())...
, we are given a set of n items. Each item weights between 0 and 1....
, we are given a set of n items. Each item weights between 0 and 1. We also have a set of bins (knapsacks). Each bin has a capacity of 1, i.e., total weight in any bin should be less than 1. The problem is to pack the items into as few bins as possible. For example Given items: 0.2, 0.5, 0.4, 0.7, 0.1, 0.3, 0.8 Opt Solution: Bin1[0.2, 0.8], Bin2[0.5, 0.4, 0.1], Bin3[0.7, 0.3] Each item must be placed...
Java. Given an input file with each line representing a record of data and the first...
Java. Given an input file with each line representing a record of data and the first token (word) being the key that the file is sorted on, we want to load it and output the line number and record for any duplicate keys we encounter. Remember we are assuming the file is sorted by the key and we want to output to the screen the records (and line numbers) with duplicate keys. We are given a text file and have...
Determining Amounts for Items Omitted from Income Statement One item is omitted in each of the...
Determining Amounts for Items Omitted from Income Statement One item is omitted in each of the following four lists of income statement data. Determine the amounts of the missing items. Chase Company Jessup Inc. Osterman Company      Snyder Co. Sales $463,200 $ $1,077,000 $ Cost of goods sold $ $408,400 $ $411,800 Gross profit $87,800 $290,600 $295,400 $266,400
in the c programming language input is given in the form The input will be of...
in the c programming language input is given in the form The input will be of the form [number of terms] [coefficient k] [exponent k] … [coefficient 1] [exponent 1] eg. 5 ─3 7 824 5 ─7 3 1 2 9 0 in this there are 5 terms with -3x^7 being the highest /* Initialize all coefficients and exponents of the polynomial to zero. */ void init_polynom( int coeff[ ], int exp[ ] ) { /* ADD YOUR CODE HERE...
Write a program in C++ (parking.cc) that reads a group of input lines. Each line contains...
Write a program in C++ (parking.cc) that reads a group of input lines. Each line contains an A for arrival or a D for departure, which is terminated by a :, and a license plate number, which is terminated by a :. The program should print a message each time a car arrives or departs. When a car arrives, the message should specify when the garage is full. If there is no room for a car, the car simply leaves....
Write a C++ function called parse that reads one line of user input from the keyboard...
Write a C++ function called parse that reads one line of user input from the keyboard and creates an array of the strings found in the input.  Your function should be passed the array and a reference variable that is to be assigned the length of the array.  Prompt the user and read the input from within the function. For example:  If the user inputs copy this that, the resulting array would have length 3 and contain the strings “copy”, “this”, and “that”....
Question: For each of the following items, determine whether the item would be:
Question: For each of the following items, determine whether the item would be:a. added to the bank balanceb. subtracted from the bank balancec. added to the book balanced. subtracted from the book balance11. Interest revenue earned12. NSF check13. Deposit in transit14. Service charge15. Outstanding check 
Given the following list of items: a. Classify the items as A, B, or C b....
Given the following list of items: a. Classify the items as A, B, or C b. Determine the economic order quantity for each item (round to the nearest whole unit) Item Estimated Annual Demand Ordering Cost Holding Cost (%) Unit Price H4-010 20,000 55 25 4.5 H5-201 60,200 65 25 6 P6-400 9,800 85 35 30.5 P6-401 14,500 55 35 14 P7-100 6,250 55 35 11 P9-103 7,500 55 45 24 TS-300 21,000 45 30 47 TS-400 45,000 45 30...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT