C Library String Functions

Advertisements
Advertisements

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

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.

From the above table, strlen(), strcpy(), strcmp() and strcat() are the most frequently used functions.

C Library String Functions Example

You will write a program that implement these 4 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

Length of First String = 5
Length of Second String = 5
String Mismatch
string cat = ChileIndia
string copy = India
Press any key to continue . . . 

Bestseller

C Programming Language, 2nd Edition 2nd Edition

The authors present the complete guide to ANSI standard C language programming. Written by the developers of C, this new version helps readers keep up with the finalized ANSI standard for C while showing how to take advantage of C’s rich set of operators, economy of expression, improved control flow, and data structures. The 2/E has been completely rewritten with additional examples and problem sets to clarify the implementation of difficult language constructs. For years, C programmers have let K&R guide them to building well-structured and efficient programs. Now this same help is available to those working with ANSI compilers. Includes detailed coverage of the C language plus the official C language reference manual for at-a-glance help with syntax notation, declarations, ANSI changes, scope rules, and the list goes on and on.

Advertisements
Advertisements
Advertisements