Posted by Abhi on February 11, 2012
Over the past couple of days, I’ve been busy writing a small personal dictionary program that will allow me to add words that I’ve learnt to a file and then access them later if need be.
You can add words using the “add <word> <meaning> <notes>” command via the linux terminal. <notes> is optional. You can then view the words that have been added using the “define <word>” command. To easily view all the words that have been added to the file, I created the “exportdict” command that transforms the file into a cleanly formatted html document that can be viewed through your preferred browser.
The entire program took me about two days to write. It employs a few basic checks such as – not allowing multiple similar entries but nothing major. The database file that the program uses is called dict-data.rog and it will be created in the same folder as the executable. Now this application isn’t exactly fault tolerent so if you make any changes to the databse file, make sure that the format is kept intact. If that is not possible then simply delete the file. Needless to say your data will be lost, hence it might be a good idea to back it up first.
General Format of the data in the database: “<entry>|<value>|<notes>” where “|” is the seperator.
Unless you want your database to be screwed up I suggest not adding “|” in your entries. In case incompatible data is found, the program will inform you of the entry which contains the incompatible data.
Currently the only way to edit existing entries is to edit them manually by opening the database file. I may write a small add-on to remedy that depending on whether or not I need it.
The executables that I’ve provided will run on a 64-bit Linux machines. I’ve also provided the source code so if you wish you can download and compile them for your platform. They should run on Windows too but I’m not entirely sure.
Feel free to make changes to the source code, and any suggestions. If you use the program and find any bugs, let me know.
Source Code : Download Here
Executables : Download Here
Posted in C++, Projects | Tagged: C++ Project, personal dicitionary | 2 Comments »
Posted by Abhi on February 9, 2012
Here is a simple string split function that I wrote a while back. The function takes a string such as “How.Are.You.”, a divider – “.” and a reference to a vector of strings as input. The vector is then filled with the strings after they have been split.
void splitString(vector<string> &v_str,const string &str,const char ch)
string::size_type pos = 0;
string::size_type old_pos = 0;
if(pos == string::npos)
flag = false;
pos = str.size();
sub = str.substr(old_pos,pos-old_pos); // Disregard the '.'
old_pos = ++pos;
#include "splitString.h" // Header file for the splitString function.
string str= "How.Are.You"; // String to be split.
char ch = '.'; // The divider.
vector<string> v_str; // Vector to store the divided strings.
for(vector<string>::iterator iter= v_str.begin();iter!=v_str.end();++iter)
Remember to put the necessary #include if you decide to put the codes in separate files.
Posted in C++ | Tagged: C++ string split, split string, string break, string divider | 1 Comment »
Posted by Abhi on February 8, 2012
C++ standard library lacks a trim() function similar to what Java provides. Here is a simple function in C++ which can achieve similar results.
std::string trim(const std::string& str)
string::size_type pos = str.find_first_not_of(" ");
string::size_type pos1 = str.find_last_not_of(" ");
string trimmed = str.substr(pos,pos1-pos+1);
std::string mystr = trim(" hello ");
Pardon me for the lack of tabs, apparently WordPress does not allow you to add tabs to your posts.
Edit: My bad. WordPress does provide support to paste code. Read more here
Posted in C++ | Tagged: C++ trim, Java trim() in C++, trim() | Leave a Comment »