Skip to content

Working with string

Rafiul Islam edited this page Oct 31, 2018 · 4 revisions

C language support string operation functionality defined at string.h

Define a string

There are two way to define a string in c. One is as a pointer another is character array

    char a[] = "Hello World";
    char *p = "Hello World";
String length

String header supports an API called strlen(char *s) which return the length of the passed string.

    char a[]  = "Hello World";
    char *p = "Hello World";
    int l1 = strlen(a);
    int l2 = strlen(p);

For array length there is another way to find out the length of the array. Formula is total memory/one element memory. sizeof function help to find out the memory size

    char arr[] = "Hello World";
    int len = sizeof(arr)/sizeof(arr[0]);

String operations

It is better to use character array to define a string in c. Pointer is good but for general string operation array is more easy to understand and operate.

String copy

using character array to copy from another character array string

    char s1[] = "Hello World"; // define a string
    char s2[sizeof(s1)/sizeof(s1[0])]; // create a new character array to hold the copy
    strcpy(s2, s1); // copy s1 data into s2

using a pointer to copy from another character array string

    char s1[] = "Hello World"; // define a string
    char *s2 = strdup(s1); // duplicate s1 and store the copy into s2

Copy a string from pointer to pointer

    char *a = "Hello There"; // define a string
    char *b; // create a new pointer
    b = a; // assign the pointer address

Copy defined length characters

    char a[] = "Hello World"; // define a string
    char b[10]; // character array of 10 length
    strncpy(b, a, 5); // copy 0-4(first 5 characters) index characters from a to b
String concatenation
    char s1[] = "Hello"; // define string s1
    char s2[] = "World"; // define string s2 
    strcat(s1, s2); // s1 = s1 + s2;

string concatenation with defined length.

    char s1[] = "Hello"; // define string s1
    char s2[] = "World"; // define string s2
    // here 2 is length of string s2 that will be add with s1
    strncat(s1, s2, 2); // HelloWo
String comparison

strings are compared in c using the ASCII value of the characters. 'a' > 'A' as for their ASCII value.

Return Condition
0 If every character are equal of string-1 and string-2
1 If first unmatched index character of string-1 > string-2
-1 If first unmatched index character of string-1 > string-2
    char s1[] = "a";
    char s2[] = "A";
    char s3[] = "AA";
    char s4[] = "aavv";
    char s5[] = "z";
    char s6[] = "AAVV";

    strcmp(s1,s2); // return 1
    strcmp(s1,s3); // return 1
    strcmp(s1,s6); // return 1
    strcmp(s1,s5); // return -1
    strcmp(s5,s4); // return 1
String reverse
    char s[] = "Hello"; // define a string 
    strrev(s); // reverse the string
String case transform
    char s[] = "Hello World"; // define a string
    strupr(s); // convert all characters to upper case
    strlwr(s); // convert all characters to lower case
String tokenization
    char data[] = "Hello, Wow! What???? Hey.... 'good', i say; no:"; // define the source string
    const char delim[] = " ,.;:?!'"; // define the tokens delimiter

    char *token = strtok(data, delim); // starting iteration with pointer

    while(token != NULL)
    {
        puts(token);

        // to walk through all the available tokens in data string
        token = strtok(NULL, delim); // iterate on previous data source
    }
    /*
        Hello
        Wow
        What
        Hey
        good
        i
        say
        no
    */