Friday, July 31, 2009

How to use string function in C?

There is no string function you have to use character arrays.





RJ

How to use string function in C?
This depends on what you are trying to do.


You must include strng.h at the top of your file e.g.


#include %26lt;string.h%26gt;





A string is a null terminated char array e.g.


char mystr[80] = "Hello World";


/* mystr is = "Hello World\0" */


mystr[5] = '\0'; /* Null terminated so the string is now Hello*/





Look here http://www.opengroup.org/onlinepubs/0079... for explanations of the functions but basicall you use them like:-





#include %26lt;string.h%26gt;





void main(void) {


char str1[80] = "A String";


char str2[80] = "Another String";





if (strcmp(str1, str2) == 0)


printf ("They are the same\n");


else


printf ("They are different\n");





if (strncmp(str1, str2, 1) == 0)


printf ("The first character is the same\n");


else


printf ("The first character is different\n");





}
Reply:There is no string datatype in C, but there are several functions that work with "strings" (arrays of characters with a null terminator). For example, strcmp, strncmp, strcpy, etc.





Are you referring to these?


No comments:

Post a Comment