{"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":"<p>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<p>Let&#8217;s start from <strong>#include<\/strong><br \/>\n<strong>strlen():  <em>int  strlen(consts char *array)<\/em> <\/strong><br \/>\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<p>Example:<\/p>\n<pre class=\"lang:c++ decode:true \" >\r\nchar arr[]=\u201dThis is a strlen function test. \u201d;\r\nPrintf(\u201c%d\u201d,strlen(arr));\r\nOutput:  30\r\n<\/pre>\n<p><strong><br \/>\nstrcmp():  <em>int strcmp(const char *a,const char *b)<\/em><\/strong><br \/>\nThis one is used for comparing two strings. It can be used on Big number comparison also.<br \/>\nExapmle:<\/p>\n<pre class=\"lang:c++ decode:true \" >\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&gt;0  as a&gt;c;  1st  parameter&gt;  2nd parameter\r\n\tx= strcmp(c,a)        \/\/ x&lt;0  as c&lt;a;   1st  parameter&lt;  2nd parameter\r\n<\/pre>\n<p>Now what is the basis of comparison of this function and what does this comparison actually mean? This comparison actually means lexicographically comparison.<br \/>\nEach of the character has a ASCII value . and ASCII value thus \u2018A\u2019 is less than \u2018a\u2019<br \/>\nSo if we compare them  \u2018a\u2019 &gt;\u2019A\u2019<\/p>\n<p>Now let&#8217;s have a practice about  this type of comparison .<br \/>\n1. strcmp( \u201cabc\u201d,\u201dAbc\u201d )  Output : &gt;0<br \/>\n2. strcmp(\u201ccow\u201d,\u201dwoc\u201d)    Output:  0<br \/>\n4. strcmp(\u201ccow\u201d,\u201dcowa\u201d)   Output:  &lt;0<br \/>\nNow have a look closely<br \/>\nA[]= \u201ccow\u201d<br \/>\nB[]= \u201dcowa\u201d<br \/>\nIf we see the memory we would see  like this :<\/p>\n<blockquote><p>A[] :\u2018c\u2019 \u2018o\u2019 \u2018w\u2019  0  0<br \/>\nB[] :\u2018c\u2019 \u2018o\u2019 \u2018w\u2019 \u2018a\u2019 0<\/p><\/blockquote>\n<p>Now see  place 0 : A[0]==B[0]   \/\/ \u2018c\u2019<br \/>\n      \t           A[1]==B[1]\t\/\/ \u2018o\u2019<br \/>\n   \t           A[2]==B[2]\t\/\/ \u2018c\u2019<br \/>\n\t\t   A[3]&lt;B[3]\t\/\/   0&lt;\u2019a\u2019  by ascii value ( here 0 ,not \u20180\u2019)<br \/>\nSo,now strcmp return a value less than 0.<br \/>\nThus strcmp(A,B)\u2019y\u2019 so it returns &gt;0 value.<br \/>\nIt will return 0 if and only if two strings are equal.<\/p>\n<p><strong>strcpy():   <em>char *strcpy(char *destination,const char *source)<\/em><\/strong><br \/>\nThis function is used to copy 2nd string to 1st string.<br \/>\nExample:<\/p>\n<pre class=\"lang:c++ decode:true \" >\r\nchar destination[]=\u201dDEPT\u201d;\r\nchar source[]=\u201dCSE\u201d;\r\nstrcpy(destination,source);\r\n<\/pre>\n<p>destination:  \u201cCSE\u201d<br \/>\nsource     :  \u201dCSE\u201d<br \/>\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.<br \/>\nNow the question is why?<br \/>\nLook closely at the memory.<br \/>\nSay,<br \/>\nchar dest=\u201dcse\u201d;    \/\/ size is 4  considering the NULL<br \/>\nchar source=\u201ddepartment\u201d;        \/\/ size is 11  considering the NULL<br \/>\nstrcpy(dest,source);<br \/>\n  Is it possible ?<\/p>\n<p>dest: memory size 4 (considering the NULL)<br \/>\n\u2018c\u2019 \u2018s\u2019 \u2018e\u2019 0<\/p>\n<p>Source :  memory size 11 (considering the NULL)<br \/>\n\u2018d\u2019 \u2018e\u2019 \u2018p\u2019 \u2018a\u2019 \u2018r\u2019 \u2018t\u2019 \u2018m\u2019 \u2018e\u2019 \u2018n\u2019 \u2018t\u2019 0 <\/p>\n<p>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<p><strong>strcat():    <em> char * strcat(char *a,const char *n)<\/em><\/strong><br \/>\nThis function is generally used for concatenation. If two strings are str1,str2 then,<\/p>\n<pre class=\"lang:c++ decode:true \" >\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<pre class=\"lang:c++ decode:true \" >\r\nstrcat(str1,str2);\r\nputs(str1);    \/\/ output : cse rocks.\r\nputs(str2);    \/\/ output : rocks\r\n<\/pre>\n<p>1st  string is changed and no change to 2nd one .<br \/>\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<p><strong>memset() :   <em>void * memset ( void * ptr, int value, size_t num )<\/em><\/strong><br \/>\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<p>Lets see<\/p>\n<pre class=\"lang:c++ decode:true \" >\r\n  int a[100];  \/\/ or  char a[100]\r\n    for(int i=0;i&lt;50;i++) \r\n \ta[i]= \u2018f\u2019;\r\n<\/pre>\n<p>this may be done by also this:<\/p>\n<pre class=\"lang:c++ decode:true \" >\r\nmemset(a,\u2019f\u2019,50*sizeof(a[0]) );<\/pre>\n<p>if any one want to insert whole array a single value than <\/p>\n<pre class=\"lang:c++ decode:true \" >memset(a,\u2019f\u2019,sizeof(a));<\/pre>\n<p>A complete c code is also provided here for clarification. \uf04a<\/p>\n<pre class=\"lang:c++ decode:true \" >\r\n#include <stdio.h>\r\n#include <string.h>\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<p><strong>strtok:  <em>char * strtok ( char * str, const char * delimiters )<\/em><\/strong><br \/>\n<strong>strtok<\/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<p>char str[]=\u201dI am a cse student. Oh! Shit, I forgot to say my high CGPA.\u201d<br \/>\nstrtok= string tokenizer (:P)  <\/p>\n<p>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 .<br \/>\nYou can use strtok in this case.<br \/>\n<strong>char * p =strtok(str,\u201d .,!.\u201d);<\/strong><br \/>\nnow we are ready to get tokens :<br \/>\n1. I<br \/>\n2. am<br \/>\n3. a<br \/>\n4. cse<br \/>\n5. student<br \/>\n6. Oh<br \/>\n7. shit<br \/>\n8. I<br \/>\n9. forgot<br \/>\n10. to<br \/>\n11. say<br \/>\n12. my<br \/>\n13. High<br \/>\n14. CGPA<br \/>\nWe have 14 tokens as we has given delimiters   \u201d .,!.\u201d<br \/>\nHere a c code is also provided for more clarification.<\/p>\n<pre class=\"lang:c++ decode:true \" >\r\n#include <stdio.h>\r\n#include <string.h>\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\n<strong>Output:<\/strong>\r\nSplitting string \"- This, a sample string.\" into tokens:\r\nThis\r\na\r\nsample\r\nstring\r\n<\/pre>\n<p>This were frequently used string manupulating functions.<\/p>\n<p>In c++ there is anothet header file <strong>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<pre class=\"lang:c++ decode:true \" >\r\n#include <iostream>\r\n#include <algorithm>\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)<<endl; \/\/ display maximum of a and b\r\n\r\n    cout<< min(a,b)<<endl;  \/\/ display minimum of a and b\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n<p>There are many of these types of usuful functions in this header file.For further information see books.<\/p>\n<p>There is another thing called STL which means <strong>Standard Template Library<\/strong><em>,another useful library of functions which makes programmer's job easier. This function provides with decrease of line of codes.<\/p>\n<p>Frequently used container classes of STL :<br \/>\n1. vector<br \/>\n2. queue<br \/>\n3. string<br \/>\n4. stack<br \/>\n5. map<br \/>\n6. list<br \/>\n7. set<br \/>\n8. pair<\/p>\n<p><a href=\"http:\/\/ujjalruet.wordpress.com\/2011\/04\/05\/stl-2\/\">Fore more about STL click here<\/a><\/p>\n<p>Thanking for reading this,<br \/>\nUjjal Suttra Dhar<br \/>\nsssujjal@gmail.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s start from #include strlen(): int strlen(consts char *array) A frequently used function and quite familiar to everyone is strlen() which returns &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.ujjal.net\/?p=153\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Frequently used header files ( string.h , algorithm ) in C\/C++&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-153","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/posts\/153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=153"}],"version-history":[{"count":2,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions"}],"predecessor-version":[{"id":351,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions\/351"}],"wp:attachment":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}