C Library String Functions

You can access the builtin C library functions through header files. The string related functions are located in String.h file.

Advertisements

Each function listed below has a specific purpose.

FunctionDescription
strlen()To find the length of a string.
strlwr()Converts a string to lowercase.
strupr()Converts a string to uppercase.
strcat()Add one string to another to make a bigger string.
strncat()Add n character of a string to another string
strcpy()Copies a string to another string replaces any existing.
strcmp()Compare two strings.
strncpy()Copies first n characters of two strings.
strncmp()Compares only first n characters with another string.
stricmp()Compare two strings, disregards case.
strcmpi()Same as stricmp()
strncmp()Compares first n characters of two strings.
strnicmp()Compares first n characters of two strings, disregards case
strdup()Duplicate a string.
strchr()Finds first occurrence of a given character in a string.
strrchr()Finds last occurrence of a given character in a string.
strstr()Finds first occurrence of a given string in another string.
strset()Changes all characters of a string to given character.
strnset()Changes first n char of a string to given character.
strrev()Reverse a given string.
Advertisements

From the above table, , , and are the most frequently used functions.

C Library String Functions Example

You will write a program that implement these built-in functions.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char str[] = "Chile";
    char str2[] = "India";
    int len1;
    int len2;
    int boo;
  
/* Implement strlen() function */
    len1 = strlen(str);
    len2 = strlen(str2);
    printf("Length of First String = %d\n",len1);
    printf("Length of Second String = %d\n",len2);
    boo = strcmp(str2,str);
  
    if(boo > 0)
    {
        printf("String Mismatch\n");
    }
    else
    {
        printf("String Matched\n");
    }
  
    strcat(str,str2);
    printf("string cat = %s\n",str);
    strcpy(str,str2);
    printf("string copy = %s\n",str);
  
system("PAUSE"); 
return 0;
}

Output

Your site doesn’t include support for the CodeMirror Blocks block. You can try installing the block, convert it to a Custom HTML block, or remove it entirely.

Install CodeMirror Blocks
Keep as HTML
Length of First String = 5
Length of Second String = 5
String Mismatch
string cat = ChileIndia
string copy = India
Press any key to continue . . . 
Advertisements

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Exit mobile version