Advertisement
Guest User

wordwrapfixed

a guest
Oct 8th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. //looptest.cpp
  2. /*does wordwrap on some input, needs const line length added, special characters need to be taken into consideration*/
  3.  
  4. #include <vector>
  5. #include <string>
  6. #include <iostream>
  7.  
  8. const int lineWidth = 60;
  9.  
  10. struct Input {
  11.     std::string text;
  12.     std::vector<int> wwrap;
  13. };
  14.  
  15. Input addWordWrap(Input input) {
  16.     int space = -1; //position of last space in each set of 60 chars
  17.     int b = 0; //resets every 60 chars or at newline placed by user
  18.     std::string input2;
  19.     for(int a = 0; a < input.text.length(); a++) {
  20.         b++;
  21.         if(input.text[a] == '\n') { //user wrote newline
  22.             b = 0;
  23.             space = -1;
  24.         }
  25.         if(input.text[a] == ' ') //last space in 60 chars found
  26.             space = a;
  27.         if(b >= lineWidth) {
  28.             b = 0;
  29.             if(space == -1) //if line is all one word
  30.                 input.wwrap.push_back(a);
  31.             else { //line is more than 1 word
  32.                 input.wwrap.push_back(space);
  33.                 a = space;
  34.             }
  35.             space = -1;
  36.             for(int c = 0; c < input.text.length(); c++) { //adds wwrap newline
  37.                 if(c == input.wwrap[input.wwrap.size()-1])
  38.                     input2 += '\n';
  39.                 input2 += input.text[c];
  40.             }
  41.             input.text = input2;
  42.             input2 = std::string();
  43.         }
  44.     }
  45.     return input;
  46. }
  47.  
  48. Input subWordWrap(Input input) {
  49.     std::string input2;
  50.     for(int a = input.wwrap.size()-1; a >= 0; a--) {
  51.         for(int b = 0; b < input.text.length(); b++) {
  52.             if(b != input.wwrap[a])
  53.                 input2 += input.text[b];
  54.         }
  55.         input.text = input2;
  56.         input2 = std::string();
  57.     }
  58.     input.wwrap.clear();
  59.     return input;
  60. }
  61.  
  62. int main() {
  63.     Input input;
  64.     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.";
  65.     std::cout << input.text << std::endl;
  66.     input = addWordWrap(input);
  67.     std::cout << input.text << std::endl;
  68.     input = subWordWrap(input);
  69.     std::cout << input.text << std::endl;
  70.     return 0;
  71. }
  72.  
  73. //to erase just get rid of all the \n characters at wwrap positions
  74. //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