Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/default-filters.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/default-filters.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/class-wp-theme.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/class-wp-theme.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/class-wp-styles.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/class-wp-styles.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-request.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-request.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/block-supports/duotone.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/block-supports/duotone.php on line 1

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768
{"id":153,"date":"2012-01-15T21:07:50","date_gmt":"2012-01-15T15:07:50","guid":{"rendered":"http:\/\/ujjalruet.wordpress.com\/?p=153"},"modified":"2015-02-14T10:55:26","modified_gmt":"2015-02-14T10:55:26","slug":"including-string-h-in-cc","status":"publish","type":"post","link":"https:\/\/blog.ujjal.net\/?p=153","title":{"rendered":"Frequently used header files ( string.h , algorithm ) in C\/C++"},"content":{"rendered":"

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.<\/p>\n

Let’s start from #include<\/strong>
\nstrlen(): int strlen(consts char *array)<\/em> <\/strong>
\nA frequently used function and quite familiar to everyone is strlen() which returns the length of a character array passed to the function.<\/p>\n

Example:<\/p>\n

\r\nchar arr[]=\u201dThis is a strlen function test. \u201d;\r\nPrintf(\u201c%d\u201d,strlen(arr));\r\nOutput:  30\r\n<\/pre>\n


\nstrcmp(): int strcmp(const char *a,const char *b)<\/em><\/strong>
\nThis one is used for comparing two strings. It can be used on Big number comparison also.
\nExapmle:<\/p>\n

\r\n\r\n\tchar  a[]=\u201dABC\u201d;\r\n\tchar  b[]=\u201dABC\u201d;\r\n\tchar  c[]=\u201dAAA\u201d;\r\n\t\/\/ now \r\n\tint  x= strcmp(a,b)  ; \/\/ x= 0  as a==b\r\n\tx= strcmp(a,c);\t\/\/ x>0  as a>c;  1st  parameter>  2nd parameter\r\n\tx= strcmp(c,a)        \/\/ x<0  as c<a;   1st  parameter<  2nd parameter\r\n<\/pre>\n

Now what is the basis of comparison of this function and what does this comparison actually mean? This comparison actually means lexicographically comparison.
\nEach of the character has a ASCII value . and ASCII value thus \u2018A\u2019 is less than \u2018a\u2019
\nSo if we compare them \u2018a\u2019 >\u2019A\u2019<\/p>\n

Now let’s have a practice about this type of comparison .
\n1. strcmp( \u201cabc\u201d,\u201dAbc\u201d ) Output : >0
\n2. strcmp(\u201ccow\u201d,\u201dwoc\u201d) Output: 0
\n4. strcmp(\u201ccow\u201d,\u201dcowa\u201d) Output: <0
\nNow have a look closely
\nA[]= \u201ccow\u201d
\nB[]= \u201dcowa\u201d
\nIf we see the memory we would see like this :<\/p>\n

A[] :\u2018c\u2019 \u2018o\u2019 \u2018w\u2019 0 0
\nB[] :\u2018c\u2019 \u2018o\u2019 \u2018w\u2019 \u2018a\u2019 0<\/p><\/blockquote>\n

Now see place 0 : A[0]==B[0] \/\/ \u2018c\u2019
\n \t A[1]==B[1]\t\/\/ \u2018o\u2019
\n \t A[2]==B[2]\t\/\/ \u2018c\u2019
\n\t\t A[3]<B[3]\t\/\/ 0<\u2019a\u2019 by ascii value ( here 0 ,not \u20180\u2019)
\nSo,now strcmp return a value less than 0.
\nThus strcmp(A,B)\u2019y\u2019 so it returns >0 value.
\nIt will return 0 if and only if two strings are equal.<\/p>\n

strcpy(): char *strcpy(char *destination,const char *source)<\/em><\/strong>
\nThis function is used to copy 2nd string to 1st string.
\nExample:<\/p>\n

\r\nchar destination[]=\u201dDEPT\u201d;\r\nchar source[]=\u201dCSE\u201d;\r\nstrcpy(destination,source);\r\n<\/pre>\n

destination: \u201cCSE\u201d
\nsource : \u201dCSE\u201d
\nOne thing is very important and should be remembered that Array size of destination must be greater than array of source, Else program may crash.
\nNow the question is why?
\nLook closely at the memory.
\nSay,
\nchar dest=\u201dcse\u201d; \/\/ size is 4 considering the NULL
\nchar source=\u201ddepartment\u201d; \/\/ size is 11 considering the NULL
\nstrcpy(dest,source);
\n Is it possible ?<\/p>\n

dest: memory size 4 (considering the NULL)
\n\u2018c\u2019 \u2018s\u2019 \u2018e\u2019 0<\/p>\n

Source : memory size 11 (considering the NULL)
\n\u2018d\u2019 \u2018e\u2019 \u2018p\u2019 \u2018a\u2019 \u2018r\u2019 \u2018t\u2019 \u2018m\u2019 \u2018e\u2019 \u2018n\u2019 \u2018t\u2019 0 <\/p>\n

Now think is it possible to keep this 11 elements including NULL (0) in 4 sized array of \u201cdest\u201d ? so you have to ensure that the destination will be long enough so that no overflow occurs . <\/p>\n

strcat(): char * strcat(char *a,const char *n)<\/em><\/strong>
\nThis function is generally used for concatenation. If two strings are str1,str2 then,<\/p>\n

\r\nchar str1[100],str2[100];\r\ngets(str1);  \/\/ say input is \u201ccse\u201d \r\ngets(str2);   \/\/ say input is \u201c rocks.\u201d<\/pre>\n
\r\nstrcat(str1,str2);\r\nputs(str1);    \/\/ output : cse rocks.\r\nputs(str2);    \/\/ output : rocks\r\n<\/pre>\n

1st string is changed and no change to 2nd one .
\nBut 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.<\/p>\n

memset() : void * memset ( void * ptr, int value, size_t num )<\/em><\/strong>
\nThis 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.<\/p>\n

Lets see<\/p>\n

\r\n  int a[100];  \/\/ or  char a[100]\r\n    for(int i=0;i<50;i++) \r\n \ta[i]= \u2018f\u2019;\r\n<\/pre>\n

this may be done by also this:<\/p>\n

\r\nmemset(a,\u2019f\u2019,50*sizeof(a[0]) );<\/pre>\n

if any one want to insert whole array a single value than <\/p>\n

memset(a,\u2019f\u2019,sizeof(a));<\/pre>\n

A complete c code is also provided here for clarification. \uf04a<\/p>\n

\r\n#include \r\n#include \r\n\r\nint main ()\r\n{\r\n  char str[] = \"almost every programmer should know memset!\";\r\n  memset (str,'-',6);\r\n  puts (str);\r\n  return 0;\r\n}\r\n<\/pre>\n

strtok: char * strtok ( char * str, const char * delimiters )<\/em><\/strong>
\nstrtok<\/strong> is a superb function indeed. According to me this function is more valuable than any other cstring\u2019s functions. Now let\u2019s see ,what is it ? <\/p>\n

char str[]=\u201dI am a cse student. Oh! Shit, I forgot to say my high CGPA.\u201d
\nstrtok= string tokenizer (:P) <\/p>\n

If you want to tokenize a string by space ,\u2019!\u2019 ,\u2019.\u2019, \u2018,\u2019 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 .
\nYou can use strtok in this case.
\nchar * p =strtok(str,\u201d .,!.\u201d);<\/strong>
\nnow we are ready to get tokens :
\n1. I
\n2. am
\n3. a
\n4. cse
\n5. student
\n6. Oh
\n7. shit
\n8. I
\n9. forgot
\n10. to
\n11. say
\n12. my
\n13. High
\n14. CGPA
\nWe have 14 tokens as we has given delimiters \u201d .,!.\u201d
\nHere a c code is also provided for more clarification.<\/p>\n

\r\n#include \r\n#include \r\n\r\nint main ()\r\n{\r\n  char str[] =\"- This, a sample string.\";\r\n  char * pch;\r\n  printf (\"Splitting string %s into tokens\\n\",str);\r\n  pch = strtok (str,\" ,.-\");\r\n  while (pch != NULL)\r\n  {\r\n    printf (\"%s\\n\",pch);\r\n    pch = strtok (NULL, \" ,.-\");\r\n  }\r\n  return 0;\r\n}\r\n\r\nOutput:<\/strong>\r\nSplitting string \"- This, a sample string.\" into tokens:\r\nThis\r\na\r\nsample\r\nstring\r\n<\/pre>\n

This were frequently used string manupulating functions.<\/p>\n

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

\r\n#include \r\n#include \r\nusing namespace std;\r\n\r\nint main()\r\n{\r\n    int a=10,b=15;\r\n\r\n\/\/a=10 b=15\r\n    swap(a,b);\r\n\/\/a=15 b=10\r\n\r\n    cout<< max(a,b)<\n

There are many of these types of usuful functions in this header file.For further information see books.<\/p>\n

There is another thing called STL which means Standard Template Library<\/strong>,another useful library of functions which makes programmer's job easier. This function provides with decrease of line of codes.<\/p>\n

Frequently used container classes of STL :
\n1. vector
\n2. queue
\n3. string
\n4. stack
\n5. map
\n6. list
\n7. set
\n8. pair<\/p>\n

Fore more about STL click here<\/a><\/p>\n

Thanking for reading this,
\nUjjal Suttra Dhar
\nsssujjal@gmail.com<\/p>\n","protected":false},"excerpt":{"rendered":"

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 … <\/p>\n