Useful Functions - Part 1

◀ Operator Precedence▶ Useful Functions - Part 2
Amazon In this section we will see some very basic but useful functions that a programmer may need at any point of writing a program. As a note, some of the basic functions are already provided by standard libraries of C or C++. If you are uncertain what functions which library provides, simply do a search on the Internet.

For example, if you are not sure what functions math.h provides, google math.h in the search field.

Random Number Generator
Here is how to make a random number generator with seed being current date and time:
#include <ctime>  /* or <time.h> for time() */
#include <stdlib.h>  /* for srand() and rand() */
srand(time(0));  /* set up random number generator based on current time */
int random = rand() % 10;    /* from 0 to 9 inclusively */
rand() produces a random integer ranged from 0 to RAND_MAX, which is defined in <stdlib.h>.

Parsing a string
This function has been covered in-depth by Chapter 12.8, but in case you miss it, let me introduce to you this useful function again. If you have information stored in one single string, separated by a delimiter, you can write a function to parse the string and retrieve relevant data. For example, you can have the following string representing a person’s bank account information, with # being the delimiter:
[email protected]#Amr#99999.99#”
The first substring is the person’s id; the second one is the person’s birthday; the third one is the person’s account type; the fourth one is the person’s email address; the fifth one is the person’s login password; the sixth one is the person’s current balance.

I want to write a function that takes as arguments a string, like the one in the above example, and an index (starting at 1, not 0), specifying which substring to return. The delimiter the function uses is #. The function returns a string which is the substring specified by the index. Now you can start writing this function on your own as an exercise. Here is my version:
using namespace std;
#include<string>  /* or <string.h> */

string parse(string s, int i) {
	string t="";
	int counter=0;
	bool start=false;

	for(int j=0;j<s.length();j++) {
        return t;
However, if you know C++ string class very well, you probably know you can take advantage of some of the functions it provides such as find(). Here is another version of the function that utilizes C++ string functions:
using namespace std;
#include<string> 	/* or <string.h> */

string parse(string s, int i) {
	if(i<1) return "";
	int d, second, first, pos;

	for(d=0;d<i-1;d++) {
			return "";
	return s.substr(first+1,second-first-1);

Conversion Between char* and string
Here the term “string” refers to C++ string class. In general, even though char array is string’s underlying implementation, string is a better choice than char* because of easy manipulations such as appending a string to another string, extracting a string from a long string, and inserting a string into another string.

However, some functions provided by C++ libraries can be called only with arguments of certain types. In this case, conversion char* to string and vice versa will come in handy.
To convert char* to string, simply use the assignment operator. To convert string to const char*, simply use c_str() function provided by <string> or <string.h>. Here is a sample program to demonstrate both conversions:
#include<string>  /* or <string.h> */
int main() {
	char* c="firstString";
	string s;
	const char* c2;
	string s2="secondString";


	cout<<"c is "<<c<<endl;
	cout<<"s is "<<s<<endl;
	cout<<"c2 is "<<c2<<endl;
	cout<<"s2 is "<<s2<<endl;
	return 0;
Note that c2 is of type const char*, not char*. Conversion between string and const char* comes in handy when, for example, the program expects a string entered by user, then the program converts the string to const char* by using c_str() so that open() that <fstream> provides can take that argument.

If you really need to convert string to char*, not const char*, you can use the following function:
#include<string.h> 	// or <string>
char* convertStringToCharStar(string s){
	int i;
	char* tempc=new char[s.length()];

	for(i=0; i<s.length(); i++)
	return tempc;
Next we'll look at more utility functions!
◀ Operator Precedence▶ Useful Functions - Part 2

Questions? Let me know!