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> Example:<\/p>\n Now what is the basis of comparison of this function and what does this comparison actually mean? This comparison actually means lexicographically comparison. Now let’s have a practice about this type of comparison . A[] :\u2018c\u2019 \u2018o\u2019 \u2018w\u2019 0 0 Now see place 0 : A[0]==B[0] \/\/ \u2018c\u2019 strcpy(): char *strcpy(char *destination,const char *source)<\/em><\/strong> destination: \u201cCSE\u201d dest: memory size 4 (considering the NULL) Source : memory size 11 (considering the NULL) 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> 1st string is changed and no change to 2nd one . memset() : void * memset ( void * ptr, int value, size_t num )<\/em><\/strong> Lets see<\/p>\n this may be done by also this:<\/p>\n if any one want to insert whole array a single value than <\/p>\n A complete c code is also provided here for clarification. \uf04a<\/p>\n strtok: char * strtok ( char * str, const char * delimiters )<\/em><\/strong> char str[]=\u201dI am a cse student. Oh! Shit, I forgot to say my high CGPA.\u201d 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 . 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 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 :
\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\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
\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
\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
\nB[] :\u2018c\u2019 \u2018o\u2019 \u2018w\u2019 \u2018a\u2019 0<\/p><\/blockquote>\n
\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
\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
\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
\n\u2018c\u2019 \u2018s\u2019 \u2018e\u2019 0<\/p>\n
\n\u2018d\u2019 \u2018e\u2019 \u2018p\u2019 \u2018a\u2019 \u2018r\u2019 \u2018t\u2019 \u2018m\u2019 \u2018e\u2019 \u2018n\u2019 \u2018t\u2019 0 <\/p>\n
\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
\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
\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\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
\r\nmemset(a,\u2019f\u2019,50*sizeof(a[0]) );<\/pre>\n
memset(a,\u2019f\u2019,sizeof(a));<\/pre>\n
\r\n#include
\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
\nstrtok= string tokenizer (:P) <\/p>\n
\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
\n1. vector
\n2. queue
\n3. string
\n4. stack
\n5. map
\n6. list
\n7. set
\n8. pair<\/p>\n