String Functions in

The strcat() function joins two strings together by overwriting the null character in the first string parameter with the second string, which retains its null terminating character. One conclusion we can draw from this operation is that we must be sure that the first string has a large enough space in memory to contain both strings, as there is no bound checking.

#include <stdio.h>
#include <string.h>

int main(void){
    
    char string1[200] = "Making assumptions is part of any investigation.";
    char *string2 = " If we're wrong, all it'll cost is an apology.";
    
    //strcat returns the first string
    printf("%s", strcat(string1, string2));
            
    //first string remains changed
    printf("\n%s\n", string1);
    
    return 0;
    
}

Our next program will append the second string read from stdin to the first, and then output the string to stdout.

#include <stdio.h>
#include <string.h>

#define BUFFER_SIZE 200

int main(void){
    
    char string1[BUFFER_SIZE];
    char string2[BUFFER_SIZE];
    
    printf("Please enter a string:\t");
    
    //standard input (stdin)
    //is treated as a file
    fgets(string1, BUFFER_SIZE, stdin);
    
    printf("Please enter another string:\t");
    
    fgets(string2, BUFFER_SIZE, stdin);

    //note how fgets includes the trailing newline
    //in the strings
    printf("\n%s\n", strcat(string1, string2));
    
    
    return 0;
    
}

The strchr() function returns a pointer to the first occurrence of a byte in a string provided to it. If no match is found, a null pointer is returned.

#include <stdio.h>
#include <string.h>

int main(void){
    
    char *string1 = "The morning sun has vanquished the horrible night";
    char string2[100] = "What a terrible night for a curse.";
    
    char *string3 = strchr(string1, 'v');
    char *string4 = strchr(string2, ' ');
    
    printf("\n%s\n", string3);
    printf("\n%s\n", string4);
    
    return 0;
    
}

The strcmp() function compares two strings and returns an integer based on the outcome. A value less than zero is returned if the first string parameter is less than the second string parameter. A value of zero is returned if the two strings are equal to each other. Finally, a value of greater than zero is returned if the first string is greater than the second string.

#include <stdio.h>
#include <string.h>

#define BUFFER_SIZE 200

int main(void){
    
    char string1[BUFFER_SIZE] = "Three pounds of flax";
    char string2[BUFFER_SIZE] = "Three pounds of flax";
    char string3[BUFFER_SIZE] = "three pounds of flax";
    
    if(strcmp(string1, string3)){
        printf("<%s> and <%s> are not the same\n", string1, string3);
    }
    
    if((strcmp(string1, string2))==0){
        printf("<%s> and <%s> are the same\n", string1, string2);
    }
    
    //result might seem counterintuitive
    if((strcmp(string1, string3))<0){
        printf("<%s> is less than <%s>\n", string1, string3);
    } else if((strcmp(string1, string3))>0){
        printf("<%s> is greater than <%s>\n", string1, string3);
    } else {
        printf("<%s> and <%s> are equal\n", string1, string3);
    }
    
    return 0;
    
}

The strcpy() function copies the contents of the second string parameter into the first.

It bears mentioning here than in certain situations that call for a pointer, an array name devolves to a pointer to its first element. Remember, a string is a char array with a standardized sentinel, in this case the null character, defining its boundary.

#include <stdio.h>
#include <string.h>

#define BUFFER_SIZE 200

int main(void){
    
    char string1[BUFFER_SIZE];
    char string2[BUFFER_SIZE] = "Han shot first.";
    char string3[BUFFER_SIZE] = "No one knows what he can do till he tries.";
    
    strcpy(string1, string2);
    
    printf("\n%s\n", string1);
    
    printf("\n%s\n", strcpy(string2, string3));
    
    char *string4 = strcpy(string3, string1);
    
    printf("\n%s\n", string4);
    
    return 0;
    
}

Don’t forget that there are no standard operators for assigning and comparing strings. To assign a string value to another string, we must use strcpy(). To compare a string, we must use strcmp().

The strlen() function returns the length as an unsigned long integer of the null-terminated string pointed to by the function’s sole parameter. Note that the null terminator itself is not counted.

#include <stdio.h>
#include <string.h>

#define BUFFER_SIZE 200

int main(void){
    
    char *str1 = "Don't Panic";
    
    printf("The length of the string: %lu\n", strlen("You rush a Miracle Man, you get rotten miracles"));
    
    printf("The length of %s is %lu", str1, strlen(str1));
    
    return 0;
}

The strncat() function joins a two strings together, with the second string parameter being limited in length by a certain number of characters specified in the third and final function parameter, which is an integer number.

#include <stdio.h>
#include <string.h>

#define BUFFER_SIZE 30
#define LARGER_BUFFER_SIZE 60

int main(void){
    
    char string1[BUFFER_SIZE]="";
    char string2[LARGER_BUFFER_SIZE] = "As Thucydides has said, ";
    char *string3 = "The secret of happiness is freedom. The secret of freedom is courage.";
    
    strncat(string1, string3, BUFFER_SIZE);
    
    printf("\n%s\n", string1);
    
    //remember, strncat and strcat append second string
    //to the end of the first
    strncat(string2, string3, LARGER_BUFFER_SIZE - strlen(string2));
    
    printf("\n%s\n", string2);
    
    return 0;
}

If you’re interested in learning more about the C language, please buy a copy of my book at http://www.amazon.com/Big-Als-C-Standard-ebook/dp/B00A4JGE0M

 

 

Advertisements