Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //looptest.cpp
- /*does wordwrap on some input, needs const line length added, special characters need to be taken into consideration*/
- #include <vector>
- #include <string>
- #include <iostream>
- const int lineWidth = 60;
- struct Input {
- std::string text;
- std::vector<int> wwrap;
- };
- Input addWordWrap(Input input) {
- int space = -1; //position of last space in each set of 60 chars
- int b = 0; //resets every 60 chars or at newline placed by user
- std::string input2;
- for(int a = 0; a < input.text.length(); a++) {
- b++;
- if(input.text[a] == '\n') { //user wrote newline
- b = 0;
- space = -1;
- }
- if(input.text[a] == ' ') //last space in 60 chars found
- space = a;
- if(b >= lineWidth) {
- b = 0;
- if(space == -1) //if line is all one word
- input.wwrap.push_back(a);
- else { //line is more than 1 word
- input.wwrap.push_back(space);
- a = space;
- }
- space = -1;
- for(int c = 0; c < input.text.length(); c++) { //adds wwrap newline
- if(c == input.wwrap[input.wwrap.size()-1])
- input2 += '\n';
- input2 += input.text[c];
- }
- input.text = input2;
- input2 = std::string();
- }
- }
- return input;
- }
- Input subWordWrap(Input input) {
- std::string input2;
- for(int a = input.wwrap.size()-1; a >= 0; a--) {
- for(int b = 0; b < input.text.length(); b++) {
- if(b != input.wwrap[a])
- input2 += input.text[b];
- }
- input.text = input2;
- input2 = std::string();
- }
- input.wwrap.clear();
- return input;
- }
- int main() {
- Input input;
- input.text = "Ginger (Zingiber officinale Roscoe) is a flowering plant in the family Zingiberaceae whose rhizome, ginger root or simply ginger, is widely used as a spice or a folk medicine.\n\nIt is a herbaceous perennial which grows annual stems about a meter tall bearing narrow green leaves and yellow flowers. Ginger is indigenous to south China, and was spread eventually to the Spice Islands, other parts of Asia and subsequently to West Africa and the Caribbean.[2] Ginger was exported to Europe via India in the first century AD as a result of the lucrative spice trade.[2][3] India is now the largest producer of ginger.[2]\n\nOther members of the family Zingiberaceae include turmeric, cardamom, and galangal. The distantly related dicots in the genus Asarum are commonly called wild ginger because of their similar taste.";
- std::cout << input.text << std::endl;
- input = addWordWrap(input);
- std::cout << input.text << std::endl;
- input = subWordWrap(input);
- std::cout << input.text << std::endl;
- return 0;
- }
- //to erase just get rid of all the \n characters at wwrap positions
- //then do the erase. then run the above code again to add them back in as needed.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement