×

Strings In C Programming


In this chapter, we will understand about strings.


String in C

Strings in C Programming Language :-

String:- A one-dimensional array of characters with last element is a null character or '\0', i.e. {'E','a','r','t','h','C','o','d','i','n','g','\0'}. That is a String in C programming resembles. Declaring a String is much like working with a one-dimension Array in C. How about we move farther with examples to explore more on String in C.

Syntax of Declaring a String

The Syntax of Declaring a String or a Single-Dimension Character Array or One-Dimension Array of Character in C Programming is as follows:

 char string_variable_name[size];

How Declare String in C?

Declaring a String is same as you declare a one-dimension character array, the main thing that different is you set the last element of that array, a null character or '/0'. There exist other ways also, to Initialize a String. We will study each of these by Examples.

This is how a string can be Declared and Initialized in a C program:-

 char ec[] = {'E','a','r','t','h','C','o','d','i','n','g','\0'};
 char ec[] = "EarthCoding";
 char ec[20] = {'E','a','r','t','h','C','o','d','i','n','g','\0'};
 char ec[20] = "EarthCoding";

Examples of String in C

Simple C program to print "Welcome to EarthCoding" on the console using string. Here we will use EarthCoding as String.

File name - string_eg.c
#include<stdio.h>
void main()
{
    //char welcome_string[] = {'E','a','r','t','h','C','o','d','i','n','g','\0'};
    char welcome_string[20] = {'E','a','r','t','h','C','o','d','i','n','g','\0'};
    printf("Welcome to %s\n", welcome_string);
}

Or the same program can be written as:

File name - string_eg.c
#include<stdio.h>
void main()
{
    //char welcome_string[] = "EarthCoding";
    char welcome_string[20] = "EarthCoding";
    printf("Welcome to %s\n", welcome_string);
}

Both the above programs will print the following output

output
Welcome to EarthCoding

In the above example we have used format specifier %s in the printf() statement. In C %s is used to print string variables.

Built-in String Functions in C

C gives a variety of built-in functions which are very helpful when working with Strings. For instance, comparing two strings, determining the length of a string, and so forth.

Below are few string functions for practicing

strcmp() in C - Compare two Strings

  1. strcmp Function in C Compares two Strings Character by Character, means, the first character of strings are compared, then second and so on until a Null character('\0') reaches or a mismatch is detected.

  2. It Returns Zero(0) if both the string are identical, For example

  3.  strcmp("Earth","Earth") will return 0
  4. strcmp(string1,string2) , Returns a Positive(+ve) number If the ASCII value of the first non-matching character in string1 is greater than the ASCII value of the character in string2. For Example:

  5. string_compare.c
    #include<stdio.h>
    #include<string.h>
    void main()
    {
        char string1[15] = "Earth";
        char string2[15] = "Coding";
        /*First non-matching characters are E and C
         *ASCII value of E is 69
         *ASCII value of C is 67
         */
        int result;
        result = strcmp(string1,string2);
        printf("strcmp(string1,string2) is %d\n", result);
    }
    
    output
    strcmp(string1,string2) is 2
    
  6. strcmp(string1,string2) Returns Negative(-ve) number the first non-matching character's ASCII value in string1 is less than the ASCII value of the character in string2. For Example:

  7. string_compare.c
    #include<stdio.h>
    #include<string.h>
    void main()
    {
        char string1[15] = "Coding";
        char string2[15] = "Earth";
        /*First non-matching characters are C and E
         *ASCII value of C is 67
         *ASCII value of E is 69
         */
        int result;
        result = strcmp(string1,string2);
        printf("strcmp(string1,string2) is %d\n", result);
    }
    
    output
    strcmp(string1,string2) is -2
    
  8. Fact: strcmp gives the difference between the ASCII value of First Non-Matching Characters in two string.

strcpy() in C - Copy one string to another

  1. strcpy function in C is used to copy one string value to another string. For Example

  2. strcpy_function.c
    #include<stdio.h>
    #include<string.h>
    void main()
    {
        char string1[15] = "Coding";
        char string2[15] = "Earth";
        strcpy(string1,string2);
        printf("string1 is %s\n", string1);
    }
    
    output
    string1 is Earth
    

Example with more functions:

example_string_function.c
#include <stdio.h>
#include <string.h>
void main()
{
    //create some strings with length 15, you can give any length but it should be enough to hold the value
    char string1[15] = "earth";
    char string2[15] = "coding";
    char string3[15];
    int length;

    /* Lets concat string1 and string2 and print welcome to earthcoding*/
    strcat(string1, string2);
    printf("welcome to %s\n", string1); // string1 is now earthcoding

    /* Lets find the length of string1 after concatination */
    length = strlen(string1);
    printf("Length of string1 is %d\n",length);

    /* Let's copy content of string1 into string3 */
    strcpy(string3,string1);
    printf("string3 = %s\n",string3); //string1 is copied into string3
}

output
welcome to earthcoding
Length of string1 is 11
string3 = earthcoding

In the next chapter, we will learn about Pointers.

Please share this tutorial if you like this