Frequently used header files ( string.h , algorithm ) in C/C++

I am starting this briefing assuming that the reader is already known about the basic structure of programming with C. Here you will find a brief collection which are used frequently in programming.

Let’s start from #include
strlen(): int strlen(consts char *array)
A frequently used function and quite familiar to everyone is strlen() which returns the length of a character array passed to the function.

Example:


strcmp(): int strcmp(const char *a,const char *b)

This one is used for comparing two strings. It can be used on Big number comparison also.
Exapmle:

Now what is the basis of comparison of this function and what does this comparison actually mean? This comparison actually means lexicographically comparison.
Each of the character has a ASCII value . and ASCII value thus ‘A’ is less than ‘a’
So if we compare them ‘a’ >’A’

Now let’s have a practice about this type of comparison .
1. strcmp( “abc”,”Abc” ) Output : >0
2. strcmp(“cow”,”woc”) Output: 0
4. strcmp(“cow”,”cowa”) Output: <0
Now have a look closely
A[]= “cow”
B[]= ”cowa”
If we see the memory we would see like this :

A[] :‘c’ ‘o’ ‘w’ 0 0
B[] :‘c’ ‘o’ ‘w’ ‘a’ 0

Now see place 0 : A[0]==B[0] // ‘c’
A[1]==B[1] // ‘o’
A[2]==B[2] // ‘c’
A[3]<B[3] // 0<’a’ by ascii value ( here 0 ,not ‘0’)
So,now strcmp return a value less than 0.
Thus strcmp(A,B)’y’ so it returns >0 value.
It will return 0 if and only if two strings are equal.

strcpy(): char *strcpy(char *destination,const char *source)
This function is used to copy 2nd string to 1st string.
Example:

destination: “CSE”
source : ”CSE”
One thing is very important and should be remembered that Array size of destination must be greater than array of source, Else program may crash.
Now the question is why?
Look closely at the memory.
Say,
char dest=”cse”; // size is 4 considering the NULL
char source=”department”; // size is 11 considering the NULL
strcpy(dest,source);
Is it possible ?

dest: memory size 4 (considering the NULL)
‘c’ ‘s’ ‘e’ 0

Source : memory size 11 (considering the NULL)
‘d’ ‘e’ ‘p’ ‘a’ ‘r’ ‘t’ ‘m’ ‘e’ ‘n’ ‘t’ 0

Now think is it possible to keep this 11 elements including NULL (0) in 4 sized array of “dest” ? so you have to ensure that the destination will be long enough so that no overflow occurs .

strcat(): char * strcat(char *a,const char *n)
This function is generally used for concatenation. If two strings are str1,str2 then,

1st string is changed and no change to 2nd one .
But here we also have to be careful about size of 1st parameter . As 2nd parameter is added to 1st one , size should be available otherwise overflow will occur.

memset() : void * memset ( void * ptr, int value, size_t num )
This function is very much helpful.If you want to initialize an array with a desired value then what you will do, just iterate.but memset support this with a single statement.

Lets see

this may be done by also this:

if any one want to insert whole array a single value than

A complete c code is also provided here for clarification. 

strtok: char * strtok ( char * str, const char * delimiters )
strtok is a superb function indeed. According to me this function is more valuable than any other cstring’s functions. Now let’s see ,what is it ?

char str[]=”I am a cse student. Oh! Shit, I forgot to say my high CGPA.”
strtok= string tokenizer (:P)

If you want to tokenize a string by space ,’!’ ,’.’, ‘,’ or any characters then you can do it by strtok. Now what does tokenize means ? sometimes we take input of an array of a paragraph and we need to separate the words form it. In this case words are our token .
You can use strtok in this case.
char * p =strtok(str,” .,!.”);
now we are ready to get tokens :
1. I
2. am
3. a
4. cse
5. student
6. Oh
7. shit
8. I
9. forgot
10. to
11. say
12. my
13. High
14. CGPA
We have 14 tokens as we has given delimiters ” .,!.”
Here a c code is also provided for more clarification.

This were frequently used string manupulating functions.

In c++ there is anothet header file algorithm.h which provides us many bullt-in function Which are frequently used by us. Here I am giving an example for describing some of them.

There are many of these types of usuful functions in this header file.For further information see books.

There is another thing called STL which means Standard Template Library,another useful library of functions which makes programmer’s job easier. This function provides with decrease of line of codes.

Frequently used container classes of STL :
1. vector
2. queue
3. string
4. stack
5. map
6. list
7. set
8. pair

Fore more about STL click here

Thanking for reading this,
Ujjal Suttra Dhar
sssujjal@gmail.com

Programming,Mathematics and Newbies

I am not sure if I am the right person or not to write this. Now I am going to share something with you that I know. Yes, These all are about the “Importance of programming” for the newbies. Here I will inspire you to be a good programmer, not the “Boss” of one of the C/C++/JAVA/PHP or web developments and many others. Being a programmer means being well-developped logically i.e mathematically who can use any language whatever it is,to solve a real life problem efficiently. As I know, and I think everyone will agree with me that mathematics is the one and only way to solve any real-life problems (which are not defined impossible by the experts). As a Bangladeshi , It is a matter of shame that we are far from other countries in the race of mathematics which is an obstacle in the race of technological development. If you say,“God didn’t give me a good brain,what can I do?”, I will not agree with you. As my opinion, It’s all about practice.We,the lazy people,passes our time doing nothing. A Proverb is said to be “Practice makes a man perfect”.
So, What are you thinking now? Wanna start practicing mathematics and programming now? An Eiffel Tower can not be built in one night.So,you have to spend some of your precious times to gain the power to control your PC,the coming world of technology.now what to do? Let’s go through some steps.

Step 1:
Find some books on basic mathematics/Number theory.
* Elementary Number Theory with Applications
-Thomas Koshy
*Concrete mathematics
-Donald Knuth

Step 2:
Select an programming language to implement those mathematics you have learnt. Now-a-days, C,C++,JAVA are the most popular and widely used languages. If you are newbie ,I will suggest you to learn C because it is the most efficient and easiest language. Most of important softwares and Some of operating systems are developed in C.
If you are not a student of CSE background,don’t be afraid,because you can learn it easily. You can learn this from this book. Ya, all the newbies who are related to CSE,this book is also for you too.
*Art Of Programming contest
* Programming with C
-Bayron Gottfried
Step 3:
Now you have learnt a little bit of mathematics, at least one of the programming languages, it’s time go now. What to do? You have to solve problems.There are many Online judges,In which there are thousands of problem descriptions with the facility to judge your solution ,is it ok or not,I will mention “Is the solution efficient or not”.
As you are newbie I will suggest you to use UVA at first . For this you have to open an account on this site.you can browse problems of their site. The first job of you to read the problem carefully,find the solution ,then code it.
What is coding? If you have learnt any of the programming languages, don’t ask it again.You have to write a code which will take input and provide with outputs as the problem description. After login you have to submit your solution to the onlinejudge.That machine will judge your solution in RE (runtime error),TE (time limit exceede),WA (wrong answer),PA (presentation error) and AC (accepted).
When submitting,you have take some measures which will be described below.
You can track your progress from here .
After solving some of problems,you should not be limited only in a single judges.

Here are some others OJs.
*SPOJ
*Light OJ
*Topcoder
*Codeforces

Here are some of websites/blogs which can be useful for you too.
*Smilitude
* UVA FORUM

Problems solving with C:
A sample code for submitting a problem is given below.
[code]
#include<stdio.h>
int main()
{
int testcase,input,I,j,k;
while(scanf(“%d”,&input)==1)
{
//your solution exists here
If(input ==2)
printf(“two”);
else
printf(“Not two”);
}
return 0;
}
[/code]

Now practice problem solving. You must attend free online contests to judge yourself.

For any kind of help or suggestions
I am here