> ~ Online tutorial


Handling strings
The C Standard Library commonly provides a number of very useful functions which handle strings. Here is a short list of some common ones which are immediately relevant (more are listed in the following chapter). Chances are, a good compiler will support a lot more than those listed below, but, again, it really depends upon the compiler.
strlen()
This function returns a type int value, which gives the length or number of characters in a string, not including the NULL byte end marker. An example is:
  int len;
  char *string;
  len = strlen (string);


strcpy()
This function copies a string from one place to another. Use this function in preference to custom routines: it is set up to handle any peculiarities in the way data are stored. An example is
  char *to,*from;

  to = strcpy (to,from);

Where to is a pointer to the place to which the string is to be copied and from is the place where the string is to be copied from.
strcmp()
This function compares two strings and returns a value which indicates how they compared. An example:
  int value;
  char *s1,*s2;

  value = strcmp(s1,s2);

The value returned is 0 if the two strings were identical. If the strings were not the same, this function indicates the (ASCII) alphabetical order of the two. s1 > s2, alphabetically, then the value is > 0. If s1 < s2 then the value is < 0. Note that numbers come before letters in the ASCII code sequence and also that upper case comes before lower case.
strstr()
Tests whether a substring is present in a larger string
  int n;
  char *s1,*s2;

  if (n = strstr(s1,s2))
     {
     printf("s2 is a substring of s1, starting at %d",n);
     }


strncpy()
This function is like strcpy, but limits the copy to no more than n characters.
strncmp()
This function is like strcmp, but limits the comparison to no more than n characters.

Please Give Us Your 1 Minute In Sharing This Post!
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Powered By: BloggerYard.Com

0 comments: