Thinking in C++

My C++ code snips.

Archive for February, 2012

Personal Dictionary App

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.


HTML Screenshot

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: , | 2 Comments »

C++ Split String

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.

#include <string>
#include <vector>

using std::vector;
using std::string;

void splitString(vector<string> &v_str,const string &str,const char ch)
	string sub;
	string::size_type pos = 0;
	string::size_type old_pos = 0;
	bool flag=true;
		if(pos == string::npos)
			flag = false;
			pos = str.size();
		sub = str.substr(old_pos,pos-old_pos);  // Disregard the '.'
		old_pos = ++pos;


#include <string>
#include <vector>
#include <iostream>
#include <iterator>
#include "splitString.h"       // Header file for the splitString function.

using std::vector;
using std::string;
using std::cout;

int main()
	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)
	return 0;



Remember to put the necessary #include if you decide to put the codes in separate files.

Posted in C++ | Tagged: , , , | 1 Comment »

C++ Trim

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.

#include <string>

std::string trim(const std::string& str)
	using std::string;
	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);
	return trimmed;

Usage :

#include <iostream>
#include <string>
int main()
	std::string mystr = trim(" hello  ");

Output :


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: , , | Leave a Comment »