Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Strings
//Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x86 #include <iostream> #include <string> #include <vector> using namespace std; int main() { string a = "<thing> <> <yeah/> <another/> </thing> <last></last>"; vector<string> tokens; int rightBraces = 0; int leftBraces = 0; /* Search for imbalanced brackets */ for(int i=0; i < a.length(); i++) { if(a[i]=='<') leftBraces++; if(a[i]=='>') rightBraces++; } if(leftBraces!=rightBraces) { cout << "Imbalanced brackets <> in string." << endl; return 0; } /* Gather possible tokens */ for(int i=0; i < a.length(); i++) { if(a[i]=='<') { cout << "i: " << i << endl; int pos = a.find('>', i); int badBrace = a.find('<', i+1); cout << "pos: " << pos << endl; cout << "badBrace: " << badBrace << endl; if(badBrace < pos && !(badBrace==string::npos)) { cout << "Nested brackets not allowed." << endl; return 0; } cout << "First substring character: " << a[i+1] << endl; // Note: not always true; for <> first substring char is a[i] cout << "Last substring character: " << a[pos-1] << endl; // last substring char is a[pos] string substring = a.substr(i+1, pos-i-1); cout << "Possible tag: " << substring << endl; tokens.push_back(substring); i = pos; cout << endl << endl; } } cout << "*END EXAMPLE*" << endl << endl; cout << "Elements of tokens:" << endl; for(int i=0; i < tokens.size(); i++) { cout << tokens[i] << endl; } /*string c = "10"; string one = "1"; string zero = "0"; bool check1 = one[0] == c[0]; bool check2 = zero[0] == c[1]; cout << check1 << " " << check2 << endl; */ /*bool valid = true; string one = "1"; string zero = "0"; string test = "1b"; for(int i = 0; i < test.length(); i++) { if(one[0] != test[i]) { if(zero[0] != test[i]) { valid = false; break; } } } cout << valid; */ /*string empty1 = ""; string empty2 = ""; cout << empty1.compare(empty2); */ }
run
|
edit
|
history
|
help
0
ToString library for C++
Visual C++ template instantiation
Chord Note Finder
Eight Queen
assignment
Dices by GOOSE
hangman
Error log b is an undeclared identifier...
cv2
ambiguity does not count as ambiguity