> ~ Online tutorial


String Manipulation
The following functions perform useful functions for string handling, See Strings.
strcat()
This function "concatenates" two strings: that is, it joins them together into one string. The effect of:
 char *new,*this, onto[255];

 new = strcat(onto,this);

is to join the string this onto the string onto. new is a pointer to the complete string; it is identical to onto. Memory is assumed to have been allocated for the starting strings. The string which is to be copied to must be large enough to accept the new string, tagged onto the end. If it is not then unpredictable effects will result. (In some programs the user might get away without declaring enough space for the "onto" string, but in general the results will be garbage, or even a crashed machine.) To join two static strings together, the following code is required:
 char *s1 = "string one";
 char *s2 = "string two";

 main ()

 { char buffer[255];

 strcat(buffer,s1);
 strcat(buffer,s2);
 }

buffer would then contain "string onestring two".
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.
There are also variations on the theme of the functions above which begin with strn instead of str. These enable the programmer to perform the same actions with the first n characters of a string:
strncat()
This function concatenates two strings by copying the first n characters of this to the end of the onto string.
 char *onto,*new,*this;

 new = strncat(onto,this,n);


strncpy()
This function copies the first n characters of a string from one place to another
 char *to,*from;
 int n;

 to = strncpy (to,from,n);


strncmp()
This function compares the first n characters of two strings
 int value;
 char *s1,*s2;

 value = strcmp(s1,s2,n);

The following functions perform conversions between strings and floating point/integer types, without needing to use sscanf(). They take a pre-initialized string and work out the value represented by that string.

atof()
ASCII to floating point conversion.
 double x;
 char *stringptr;

 x = atof(stringptr);


atoi()
ASCII to integer conversion.
 int i;
 char *stringptr;

 i = atoi(stringptr);


atol()
ASCII to long integer conversion.
 long i;
 char *stringptr;

 i = atol(stringptr);


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: