Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
csv parser
/*Parse an escaped string into csvformat Input: csvformat John,Smith,john.smith@gmail.com,Los Angeles,1 Jane,Roberts,janer@msn.com,"San Francisco, CA",0 "Alexandra ""Alex""",Menendez,alex.menendez@gmail.com,Miami,1 """Alexandra Alex""" Output: escaped string John|Smith|john.smith@gmail.com|Los Angeles|1 Jane|Roberts|janer@msn.com|San Francisco, CA|0 Alexandra "Alex"|Menendez|alex.menendez@gmail.com|Miami|1 "Alexandra Alex" */ #include <iostream> #include <string> #include <vector> using namespace std; string parseCSV(string s){ string res = ""; int n = s.length(); bool inside = false; for(int i=0; i<n; i++){ char c = s[i]; if(inside) { if(c == '\"') { if(i+1< n && s[i+1] =='\"'){ // multiple quote res += '\"'; i++; // 2->1, 4->2 } else inside = false; } else res += s[i]; } else{ if(c == ',') { res += '|'; } else if( c == '\"') { inside = true; } else{ res += c; } } } return res; } int main(){ // test1 string s = "Jane,Roberts,janer@msn.com,\"San Francisco, CA\",0"; cout << s << endl; cout << "-> " << parseCSV(s) << endl; //test 2 s = "\"Alexandra \"\"\"\"Alex\"\"\"\"\",Menendez,alex.menendez@gmail.com,Miami,1 \"\"\"Alexandra Alex\"\"\""; cout << s << endl; cout << "-> " << parseCSV(s) << endl; return 0; }
run
|
edit
|
history
|
help
0
water drop/ water land
Stock buy/sell
Zahra_matrix
pranav pandey
Boost phoenix. e.g 3 phoenix: functor
Test 4(2020)
Stream8
Stream4
codechef
creating triangle using nested loop