[ create a new paste ] login | about

Link: http://codepad.org/pf6YPo5W    [ raw code | output | fork ]

mohit_at_codepad - C++, pasted on Jun 14:
#include <string>
#include <iostream>
#include <sstream>
#include <cassert>
using namespace std;

bool isQuotation(char ch) {
  return ch == '"' || ch == '\'';
}

void testString(string &s)
{
  stringstream ssIn(s), ssOut;
  string t;
  int depth = 0;
  while(ssIn >> t) {
    if( isQuotation(t[0]) ) ++depth;
    else if( isQuotation( t[t.size() - 1] ) ) --depth;
    ssOut << t << " ,"[!depth];
  }
  assert(0 == depth);
  assert( ssOut.str().size() == 1 + s.size() );
  ssOut.str().substr( 0, s.size() ).swap(s);
}

int main()
{
 string input1("This is a test");
 string input2("This is 'an example of 'recursive quotes' in \"string question series\" homework' targeting \"some abc\" program");
 string input3("This is \"quoted text\"");

 testString(input1);
 cout << input1 << endl;
 testString(input2);
 cout << input2 << endl;
 testString(input3);
 cout << input3 << endl;
 
 return 0;
}


Output:
1
2
3
This,is,a,test
This,is,'an example of 'recursive quotes' in "string question series" homework',targeting,"some abc",program
This,is,"quoted text"


Create a new paste based on this one


Comments: