Strings in C programming



String Definition

A string is a series of characters treated as a single unit. A string may include letters, digits and various special characters such as +, -, *, / and $. String literals, or string constants, in C are written in double quotation marks.

A string in C is an array of characters ending in the null character ('\0'). A string is accessed via a pointer to the first character in the string.


    char color[] = "blue";
    const char *colorPtr = "blue";

The first definition creates a 5-element array color containing the characters 'b', 'l', 'u', 'e' and '\0'.

The second definition creates pointer variable colorPtr that points to the string "blue" somewhere in memory.

Function puts & getchar

Function getchar reads a character from the standard input and returns the character as an integer.

Function puts takes a string as an argument and displays the string followed by a newline character.

   
    // Using function puts & getchar
    #include <stdio.h>
    #define SIZE 80
    int main( void ) { 
    int c; // variable to hold character input by user
    char sentence[ SIZE ]; // create char array
    int i = 0; // initialize counter i
    // prompt user to enter line of text
    puts( "Enter a line of text:" ); 
    // use getchar to read each character                
    while ( i < SIZE - 1 && ( c = getchar() ) != '\n' ) {
    sentence[ i++ ] = c;                              
    } // end while                                       
    sentence[ i ] = '\0'; // terminate string
   
    // use puts to display sentence
    puts( "\nThe line entered was:" ); 
    puts( sentence );
    } // end main

    Output:
    Enter a line of text: Infocodify tutorials
    The line entered was: Infocodify tutorials

String functions of the string-handling library

The table following lists many of the String functions of the string-handling library.

Function prototype Function description
char *strcpy( char *s1, const char *s2 ) Copies string s2 into array s1. The value of s1 is returned.
char *strncpy( char *s1, const char *s2, size_t n ) Copies at most n characters of string s2 into array s1. The value of s1 is returned.
char *strcat( char *s1, const char *s2 ) Appends string s2 to array s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned.
char *strncat( char *s1, const char *s2, size_t n ) Appends at most n characters of string s2 to array s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned.
int strcmp( const char *s1, const char *s2 ); Compares the string s1 with the string s2. The function returns 0, less than 0 or greater than 0 if s1 is equal to, less than or greater
int strncmp( const char *s1, const char *s2, size_t n ); Compares up to n characters of the string s1 with the string s2. The function returns 0, less than 0 or greater than 0 if s1 is equal to, less than or greater than s2, respectively.

PROMOTIONS