Friday 4 September 2015

Useful String Operations used in real time C language


The Following list of String operations are being used in real time C applications.

String Operations:
==================

Header File Required : #include<string.h>

------------------------
strcat(), strncat()
------------------------

Concatenate two strings into a single string.


Prototypes
------------
#include <string.h>
int strcat(const char *dest, const char *src);
int strncat(const char *dest, const char *src, size_t n);

Concatenate", for those not in the know, means to "stick together".
These functions take two strings, and stick them together, storing the result in the first string.


Return Value
-------------
Both functions return a pointer to the destination string, like most of the string-oriented functions.


Example
========

char dest[20] = "Hello";
char *src = ", World!";
char numbers[] = "12345678";

printf("dest before strcat: \"%s\"\n", dest); // "Hello"

strcat(dest, src);
printf("dest after strcat:  \"%s\"\n", dest); // "Hello, world!"

strncat(dest, numbers, 3); // strcat first 3 chars of numbers
printf("dest after strncat: \"%s\"\n", dest); // "Hello, world!123"


--------------------------------------------------------------

strchr(), strrchr()  : Find a character in a string.
---------------------------------------------------------------



strchr() : Find the first occurance of a letter in a string
----------

strrchr(): Find the last occurance of a letter in a string
---------          
(The extra "r" in strrchr() stands for "reverse"-- it looks starting at the end of the string and working backward.)

-----------
Example 1:
----------

The above function returns a pointer to the char or NULL if the letter isn't found in the string.

// "Hello, world!"
//       ^  ^  
//       A  B

char *str = "Hello, world!";
char *p;

p = strchr(str, ','); // p now points at position A
p = strrchr(str, 'o'); // p now points at position B

-----------
Example 2:
----------

// repeatedly find all occurances of the letter 'B'
char *str = "A BIG BROWN BAT BIT BEEJ";
char *p;

for(p = strchr(str, 'B'); p != NULL; p = strchr(p + 1, 'B')) {
    printf("Found a 'B' here: %s\n", p);
}

// output is:
//
// Found a 'B' here: BIG BROWN BAT BIT BEEJ
// Found a 'B' here: BROWN BAT BIT BEEJ
// Found a 'B' here: BAT BIT BEEJ
// Found a 'B' here: BIT BEEJ
// Found a 'B' here: BEEJ


-------------------------------------------------------
strstr()  : Find a string in another string.
-------------------------------------------------------

#include <string.h>
char *strstr(const char *str, const char *substr);


Return Value:
------------
You get back a pointer to the occurance of the substr inside the str, or NULL if the substring can't be found.

----------------------------------------------------------------
Example :
----------------------------------------------------------------

char *str = "The quick brown fox jumped over the lazy dogs.";
char *p;

p = strstr(str, "lazy");
printf("%s\n", p); // "lazy dogs."

// p is NULL after this, since the string "wombat" isn't in str:
p = strstr(str, "wombat");


---------------------------------------------
strtok()
---------------------------------------------

Tokenize a string.

#include <string.h>
char *strtok(char *str, const char *delim);

If you have a string that has a bunch of separators in it, and you want to break that string up into individual pieces,
this function can do it for you.

Note that it does this by actually putting a NUL terminator after the token, and then returning a pointer to the start of the token.
So the original string you pass in is destroyed, as it were.
If you need to preserve the string, be sure to pass a copy of it to strtok() so the original isn't destroyed.

Return Value:
------------
A pointer to the next token. If you're out of tokens, NULL is returned.

-------
Example
-------

// break up the string into a series of space or
// punctuation-separated words
char *str = "Where is my bacon, dude?";
char *token;

// Note that the following if-do-while construct is very very
// very very very common to see when using strtok().

// grab the first token (making sure there is a first token!)
if ((token = strtok(str, ".,?! ")) != NULL) {
    do {
        printf("Word: \"%s\"\n", token);

        // now, the while continuation condition grabs the
        // next token (by passing NULL as the first param)
        // and continues if the token's not NULL:
    } while ((token = strtok(NULL, ".,?! ")) != NULL);
}

// output is:
//
// Word: "Where"
// Word: "is"
// Word: "my"
// Word: "bacon"
// Word: "dude"
//



---------------------------------------------
strspn(), strcspn()
---------------------------------------------



#include <string.h>
size_t strspn(char *str, const char *accept);
size_t strcspn(char *str, const char *reject);

strspn() will tell you the length of a string consisting entirely of the set of characters in accept.
That is, it starts walking down str until it finds a character that is not in the set
(that is, a character that is not to be accepted), and returns the length of the string so far.

strcspn() works much the same way, except that it walks down str until it finds a character
in the reject set (that is, a character that is to be rejected.) It then returns the length of the string so far.


Return Value

The lenght of the string consisting of all characters in accept (for strspn()),
or the length of the string consisting of all characters except reject (for strcspn()


Example
========

char str1[] = "a banana";
char str2[] = "the bolivian navy on manuvers in the south pacific";

// how many letters in str1 until we reach something that's not a vowel?
n = strspn(str1, "aeiou");  // n == 1, just "a"

// how many letters in str1 until we reach something that's not a, b,
// or space?
n = strspn(str1, "ab "); // n == 4, "a ba"

// how many letters in str2 before we get a "y"?
n = strcspn(str2, "y"); // n = 16, "the bolivian nav"


-------------------

strcpy(), strncpy()   : Copy a string

---------------------


Prototypes

#include <string.h>
char *strcpy(char *dest, char *src);
char *strncpy(char *dest, char *src, size_t n);

Description

These functions copy a string from one address to another, stopping at the NUL terminator on the srcstring.

strncpy() is just like strcpy(), except only the first n characters are actually copied.
Beware that if you hit the limit, n before you get a NUL terminator on the src string,
your dest string won't be NUL-terminated. Beware! BEWARE!

(If the src string has fewer than n characters, it works just like strcpy().)

You can terminate the string yourself by sticking the '\0' in there yourself:


Example
=======

char s[10];
char foo = "My hovercraft is full of eels."; // more than 10 chars

strncpy(s, foo, 9); // only copy 9 chars into positions 0-8
s[9] = '\0';        // position 9 gets the terminator