String Manipulation

String Manipulation

Now we all know that strings data definitions are actually just pointers to an array, we are able to get into some nice ways to manipulate these strings. All C compilers come with some handy functions that give you pre-written ways to do commonly required things to strings, like make them all lower case, or add one string to another, or compare them together and stuff like that. The nice thing about them being included by the compiler writers is that a) They will always all conform to the same standards, which means that using a string function on one compiler will work exactly the same as on another – you can rely on the function returning the same results no matter what – and b) since the compiler builders wrote the function, almost always it’s been heavily optimized for the processor that your compiler is writing code for. Many times compiler builders will give you two versions of included string libraries, one that’s written in high level C, so the compiler can build it itself should the need arise (and so you can actually examine the source code yourself to see how it does it) and another ready built version that’s built in assembly language, and is extremely fast.

In order to get access to all this nice and handy dandy functions, you need to include the include file “string.h”

I’m going to give you a quick list of some of the more commonly used functions, with the calling prototypes, so you know how to call them and what to expect back. By this point you should be picking up the idea of how C works, and are almost at the stage of being able to go code stuff for yourself. I would recommend buying yourself a good reference book that contains a list of all the functions in all the included libraries that C compilers are supposed to have.
Remember, this isn’t all the functions there are, just the most commonly used ones.

errno
One thing to bear in mind before we go too much further is that often, when a function fails for some reason, it wants to tell you what happened and why it failed. Quite often the return variable from a function is already being used for pointers or data, so C has a different way to return this error info to you. By including the “error.h” include file you get access to a global variable called errno. It’s a int by defintion. It’s initial value is zero, but depending on what function you call, it may get set to various numbers that tell you why a function failed. The ranges of what the numbers errno can be set to are also defined in the “error.h” file. Have a good look there, and you’ll see the #defines that can be returned.
It’s up to you to include this file, and actually test what errno is under any given circumstance. Most of the time you won’t care, but every now and then, it’s useful.

memchr / strchr
void *memchr(const void *str, int c, size_t n);
char *strchr(const char *str, int c);

In the case of memchar, it searches for the first occurrence of the character c (an unsigned char) in the first n bytes of the string pointed to by the argument str.
For strchr, it does the same thing, except when a zero is encountered in the string passed in, the search is terminated.
Returns a pointer pointing to the first matching character, or null if no match was found.

Memcmp / strcmp / strncmp
int memcmp(const void *str1, const void *str2, size_t n);
int strcmp(const char *str1, const char *str2);
int strncmp(const char *str1, const char *str2, size_t n);

All functions compare the string pointed to by str1 to the string pointed to by str2.
With memcp, it compares n chars no matter what. With strcmp, it compares until a string terminator (a zero) is found, then it returns. For strncmp it does as strcmp, but you can specify the number of chars within the string it compares. So if the string could be 100 chars long, but you only want to compare the first 30, then you’d use this function.
Returns zero if str1 and str2 are equal. Returns less than zero or greater than zero if str1 is less than or greater than str2 respectively.

memcpy / strcpy / strncpy
void *memcpy(void *str1, const void *str2, size_t n);
char *strcpy(char *str1, const char *str2);
char *strncpy(char *str1, const char *str2, size_t n);

In the case of memcpy, it copies n characters from str2 to str1. For strncpy, the behavior is the same, except if the string is null terminated before the n chars are hit then more null chars are copied into str1 until n chars have been copied. For strcpy, it copied from str2 to str1 until a null terminator (a zero) is hit in str2.
If str1 and str2 overlap the behavior is undefined.
Returns the argument str1.

memset
void *memset(void *str, int c, size_t n);

Copies the character c (an unsigned char) to the first n characters of the string pointed to by the argument str.
The argument str is returned.

memmove
void *memmove(void *str1, const void *str2, size_t n);

Copies n characters from str2 to str1. If str1 and str2 overlap the information is first completely read from str1 and then written to str2 so that the characters are copied correctly.
Returns the argument str1.

strlen
size_t strlen(const char *str);

Computes the length of the string str up to but not including the terminating null character.
Returns the number of characters in the string.

strrchr
char *strrchr(const char *str, int c);

Searches for the last occurrence of the character c (an unsigned char) in the string pointed to by the argument str. The terminating null character is considered to be part of the string.
Returns a pointer pointing to the last matching character, or null if no match was found.

strchr
char *strchr(const char *str, int c);

Searches for the first occurrence of the character c (an unsigned char) in the string pointed to by the argument str. The terminating null character is considered to be part of the string.
Returns a pointer pointing to the first matching character, or null if no match was found.

strstr
char *strstr(const char *str1, const char *str2);

Finds the first occurrence of the entire string str2 (not including the terminating null character) which appears in the string str1.
Returns a pointer to the first occurrence of str2 in str1. If no match was found, then a null pointer is returned. If str2 points to a string of zero length, then the argument str1 is returned.

strcat / strncat
char *strcat(char *str1, const char *str2);
char *strncat(char *str1, const char *str2, size_t n);

Appends the string pointed to by str2 to the end of the string pointed to by str1. The terminating null character of str1 is overwritten. Copying stops once the terminating null character of str2 is copied, or, in the case of strncat, when n chars have been copied. If overlapping occurs, the result is undefined.
The argument str1 is returned.

Other String Stuff.

Now there are a couple more string type functions that are useful, but to get to these, you need to include “stdlib.h”

atoi
int atoi(const char *str);

The string pointed to by the argument str is converted to an integer (type int). Any initial whitespace characters are skipped (space, tab, carriage return, new line, vertical tab, or formfeed). The number may consist of an optional sign and a string of digits. Conversion stops when the first unrecognized character is reached.
On success the converted number is returned. If the number cannot be converted, then 0 is returned.

atof
double atof(const char *str);

The string pointed to by the argument str is converted to a floating-point number (type double). Any initial whitespace characters are skipped (space, tab, carriage return, new line, vertical tab, or formfeed). The number may consist of an optional sign, a string of digits with an optional decimal character, and an optional e or E followed by a optionally signed exponent. Conversion stops when the first unrecognized character is reached.
On success the converted number is returned. If no conversion can be made, zero is returned. If the value is out of range of the type double, then HUGE_VAL is returned with the appropriate sign and ERANGE is stored in the variable errno. If the value is too small to be returned in the type double, then zero is returned and ERANGE is stored in the variable errno.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>