answersLogoWhite

0


Best Answer

The following example shows one possible solution using C++.

The list_to_csv function performs the initial conversion, converting a given list to a comma-separated values (CSV) string. Thus the list {a, b, c} would become the string "a, b, c".

The list_to_paragraph function takes a list, converts it to a CSV and then formats the CSV for output using "and" or "or" for the final element. Thus the list {a, b, c} becomes "a, b and c" or "a, b or c" depending on the and_or value (with "and" being the default).

Note the paragraph is not terminated with a period or a new line character since it would be useful to include the paragraph in another string which may itself be a paragraph. This is demonstrated by the print_list helper function which prints a given list in both "and" and "or" variants as part of two larger paragraphs (in this case just a single-line paragraph).

Finally, the main function tests the program, starting with an empty list and building up to a list of 4 elements, printing each in turn.

#include<iostream>

#include<list>

#include<sstream>

enum class and_or {and, or};

std::string list_to_csv (const std::list<std::string>& list) {

std::stringstream ss;

auto it=list.cbegin();

while (it!=list.cend())

{

ss << *it++;

if (it!=list.cend())

ss << ", ";

}

return ss.str();

}

std::string list_to_paragraph (std::string csv, const and_or andor = and_or::and) {

size_t pos = csv.find_last_of(',');

if (pos != std::string::npos)

{

csv.replace(pos, 1, " ");

csv.insert(pos+1, andor==and_or::and?"and":"or");

}

return csv;

}

void print_list (const std::list<std::string>& list) {

std::cout << "List of " << list.size() << " item(s): {" << csv_to_paragraph(list_to_csv(list), and_or::and) << "}\n";

std::cout << "List of " << list.size() << " item(s): {" << csv_to_paragraph(list_to_csv(list), and_or::or) << "}\n";

}

int main(){

std::list<std::string> list;

print_list (list);

list.push_back ("a");

print_list (list);

list.push_back ("b");

print_list (list);

list.push_back ("c");

print_list (list);

list.push_back ("d");

print_list (list);

}

User Avatar

Wiki User

8y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you convert a list to a paragraph with a comma separating each item in the list?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Why is the serial comma also called the oxford comma?

The serial comma is also called the Oxford comma because it is commonly used by writers and editors at Oxford University Press. The use of the serial comma helps clarify lists by separating each item with a comma, including before the conjunction.


How do you Order from least to greatest 25 23 415?

To order from least to greatest, simply list each number from the smallest size to the biggest size, separating each number by a comma. The sequence would be 23, 25, 415.


Each paragraph should have this type?

Each paragraph should have this type of what.


How do you cite more than one degree after your name?

In the order the degrees are earned with a comma separating each. Example: If you earned a medical degree, and then a doctorate degree,and then went to dental school you would be Jane Smith M.D., Ph.D., D.D.S.


How do you cite an authors name in APA?

In APA style, you should cite an author's name by including their last name followed by the first initial (and middle initial if available), separated by a comma. For example: Smith, J. If there are multiple authors, list them all in the same format, separating each author with a comma and an ampersand before the last author's name.


What is a paragraph providing relevant information about each character that appears between the introductory paragraph and the first body paragraph?

Summary paragraph


How do you make sure you are using the correct english when writing a papragraph?

it's nothing out of the ordinary just the way that you're writing is structured, just use the same punctuation as you would writing in any format, keep each paragraph about roughly the same topic throughout, if you find it hard to do this freely, plan it, there's no shame in that at all. start your second paragraph either with a line separating each paragraph... ...like this. or some people prefer to write paragraphs out like this. And just have a space between the start of the next line and where the word would traditionally begin (I would recommend the first way of separating paragraphs) keep these things in mind, and you will be writing paragraphs with correct English


What will you achieve by sticking to one point in each paragraph?

You will ensure unity in the paragraph.


What should you do at the beginning of every paragraph?

In this essay I will be talking about...


Why are paragraphs of text formatted with space after each paragraph?

Adding space after each paragraph helps improve readability by visually separating distinct blocks of text. It provides a clear visual cue that one thought or idea has ended and another is beginning, making the text easier to scan and comprehend.


Would separating dogs upset them?

It's like separating 2 humans. Depends if they are friendly to each other or not mostly.


How are body paragraphs related to the introductory paragraphs?

The introductory paragraph presents the overall thesis, and each body paragraph focuses on an individual claim.