Category: 11. Strings

https://cdn3d.iconscout.com/3d/premium/thumb/string-talk-3d-icon-png-download-13557359.png

  • Special Characters

    The C language identifies a character set that comprises English alphabets upper and lowercase (A to Z as well as “a” to “z”), digits 0 to 9, and certain other symbols called “special characters” with a certain meaning attached to them.

    While many of the characters in the special symbol category are defined as operators, certain combinations of characters also have a special meaning attached to them. For example, “\n” is known as the newline character. Such combinations are called escape sequences.

    In C language, quotation marks too have a special meaning. Double quotes are used for strings, while characters are enclosed inside single quotes. Read this chapter to learn more about the other special characters used in C programs.

    Parentheses ()

    Parentheses are especially used to group one or more operands in an expression and control the order of operations in a statement.

    A part of the expression embedded inside parentheses has a higher order of precedence.

    Example

    int a =2, b =3, c =4;int d =(a + b)* c;

    Braces { }

    Braces are especially used to define blocks of code, such as function bodies and loops. They are also used to initialize arrays and struct variables.

    Examples

    Braces in function definition −

    intadd(int a,int b){int sum = a + b;return sum;}

    Braces in array initialization −

    int arr[5]={1,2,3,4,5};

    Braces in struct variable −

    structbook{char title[10];double price;int pages;};structbook b1;structbook b1 ={"Learn C",675.50,325};

    Square Brackets [ ]

    Square brackets are used to declare arrays and access elements of an array with the subscript index.

    Example

    For example, to define an array of integers and access its third element, you would use square brackets −

    int arr[5]={1,2,3,4,5};int third = arr[2];

    Asterisk (*)

    Apart from its use as a multiplication operator, the asterisk symbol (*) is also used to declare a pointer variable and dereference it to obtain the value of the target variable.

    Example

    For example, to define a pointer to an integer and access the value it points to, you would use an asterisk −

    int num =10;int*ptr =#printf("*d",*ptr);

    Ampersand (&)

    The ampersand (&) symbol is used as the address-of operator. It returns the address of a variable.

    Example

    For example, to get the address of an integer variable, you would use an ampersand −

    int num =10;int*ptr =#

    Comma (,)

    The comma is used as a separator between a statement or a function call.

    Example

    int a =1, b =2, c =3;

    Semicolon (;)

    As a primary syntax rule in C, the semicolon indicates the end of a statement in a C program.

    Example

    printf("Hello, world!");

    Dot (.)

    The dot symbol (.) is used to access the members of a structure or a union.

    Example

    structbook b1 ={"Learn C",675.50,325};printf("Title: %s\n", b1.title);printf("Price: %lf\n", b1.price);printf("No of Pages: %d\n", b1.pages);

    Arrow ()

    The arrow symbol () is used to access the members of a structure or a union through a pointer.

    Example

    structbook b1 ={"Learn C",675.50,325};structbook*strptr;
    strptr =&b1;printf("Title: %s\n", strptr->title);printf("Price: %lf\n", strptr->price);printf("No of Pages: %d\n", strptr->pages);

  • Character Array

    In C, characters and strings are stored differently. A character array stores a sequence of characters, like text. We use single quotes (‘ ‘) to define a single character (e.g., ‘A’) and double quotes (” “) to define a string or a character array (e.g., “A”, “Hello”).

    The main difference is that a single character is stored as its ASCII value, while a string is stored as a sequence of characters ending with a null terminator (\0). In this chapter, we will see single and double quoted character arrays.

    Understanding Chracter Arrays in C

    character array is a collection of characters stored in consecutive memory locations, which means each character occupies the next memory slot one after the another. In C, we use character arrays to represent strings, which are enclosed in double quotes and a null terminator (\0) is added at the end of each string to mark its end.

    Example of a Character Array

    In this example, we declare a character array str[] with 6 elements and print it using %s. Each character is stored one after the other in memory, so the program prints them one by one until it reaches the null terminator \0.

    #include <stdio.h>intmain(){char str[]="Hello";// Creating a character array of 6 chractersprintf("%s", str);// Printing the stringreturn0;}

    Below you can see the output of the above program, which displays the string stored in the character array.

    Hello
    

    Single Quotes in C

    Single quotes (‘ ‘) in C are used to represent single character. We can also use them to create a character array by listing characters one by one inside braces {}. Each character is stored internally as its ASCII value.

    For example‘A’ represents the character A, but in memory it is stored as the number 65, which is the ASCII value of A.

    Note that we cannot use single quotes to store multiple characters together like ‘Hi’ because that will cause an error.

    Example of Single Quoted Character Array

    Below is an example showing a character array using single quotes. Here, we declare a character array arr to store the characters ‘H’‘i’, and ‘!’. We then print each character one by one in a loop.

    #include <stdio.h>intmain(){char arr[]={'H','i','!'};// Creating a single-quoted character arrayint i;for(i =0; i <3; i++){printf("%c ", arr[i]);// Prints each character}return0;}

    Below is the output of the above program, which displays each character of the array one by one.

    H i !
    

    Double Quotes in C

    Double quotes (” “) in C are used to represent string literals, which are stored as character arrays. A string in C always ends with a null character (\0) so that the program knows where the string ends.

    For example“A” may look like a single character, but in memory, it is stored as two characters: ‘A’ followed by ‘\0’.

    Note: Unlike single quotes, double quotes can store multiple characters as a string.

    Example of Double Quoted Character Array

    Below is an example showing the use of double quotes. Here, we define a string str with the value “Hi”. So, “Hi” is stored as three characters: ‘H’‘i’, and the null terminator ‘\0’. That is why the size of the array becomes 3.

    #include <stdio.h>intmain(){// storing the string "Hi" in a character array (includes '\0' automatically)char str[]="Hi";printf("String: %s\n", str);printf("Size of str: %lu\n",sizeof(str));return0;}

    Following is the output of the above program, which displays the given string and also shows the size of the array (including the null terminator) −

    String: Hi
    Size of str: 3
    

    Common Mistakes

    When working with characters and strings in C, it’s easy to get confused when to use single quotes (‘ ‘) and double quotes (” “). Here are two common mistakes to avoid −

    • Using double quotes for a single character −
    char c ="A";// Wrong: "A" is a string (character array), not a single characterchar c ='A';// Correct: 'A' is a single character

    Always use single quotes when you want to store just one character.

    • Using single quotes for strings −
    char str[]='Hello';// Wrong: single quotes can only hold one characterchar str[]="Hello";// Correct: double quotes are used for strings

    Always use double quotes when you want to store multiple characters (a string).

    Difference between Single and Double Quotes in C

    The following table shows the key differences between single quotes and double quotes in C −

    FeatureSingle Quotes (‘ ‘)Double Quotes (” “)
    RepresentsA single characterA sequence of characters (string)
    Example‘A’“A”
    Data Typechar (or integer constant)char[] (character array)
    Memory Size1 byte (for a single character)Number of characters + 1 (for \0)
    Null TerminatorNot addedAutomatically added at the end
    UsageIndividual letters, digits, or symbolsWords, sentences, or multiple characters
    Stored AsASCII value of the characterArray of ASCII values with \0 at the end

    Conclusion

    In this chapter, we learned that single quotes are used to store a single character as its ASCII value, while double quotes store multiple characters as a string ending with a null terminator. We also looked at their differences in character arrays.

  • Array of Strings

    In C programming language, a string is an array of character sequences terminated by NULL, it is a one-dimensional array of characters. And, the array of strings is an array of strings (character array).

    What is an Array of Strings in C?

    Thus, an array of strings can be defined as –

    An array of strings is a two-dimensional array of character-type arrays where each character array (string) is null-terminated.

    To declare a string, we use the statement −

    char string[]={'H','e','l','l','o','\0'};
    Or
    char string ="Hello";

    Declare and Initialize an Array of Strings

    To declare an array of strings, you need to declare a two-dimensional array of character types, where the first subscript is the total number of strings and the second subscript is the maximum size of each string.

    To initialize an array of strings, you need to provide the multiple strings inside the double quotes separated by the commas.

    Syntax

    To construct an array of strings, the following syntax is used −

    char strings [no_of_strings][max_size_of_each_string];

    Example

    Let us declare and initialize an array of strings to store the names of 10 computer languages, each with the maximum length of 15 characters.

    char langs [10][15]={"PYTHON","JAVASCRIPT","PHP","NODE JS","HTML","KOTLIN","C++","REACT JS","RUST","VBSCRIPT"};

    Printing An Array of Strings

    A string can be printed using the printf() function with %s format specifier. To print each string of an array of strings, you can use the for loop till the number of strings.

    Example

    In the following example, we are declaring, initializing, and printing an array of string −

    #include <stdio.h>intmain(){char langs [10][15]={"PYTHON","JAVASCRIPT","PHP","NODE JS","HTML","KOTLIN","C++","REACT JS","RUST","VBSCRIPT"};for(int i =0; i <10; i++){printf("%s\n", langs[i]);}return0;}

    Output

    When you run this code, it will produce the following output −

    PYTHON
    JAVASCRIPT
    PHP
    NODE JS
    HTML
    KOTLIN
    C++
    REACT JS
    RUST
    VBSCRIPT
    

    Note: The size of each string is not equal to the row size in the declaration of the array. The “\0” symbol signals the termination of the string and the remaining cells in the row are empty. Thus, a substantial part of the memory allocated to the array is unused and thus wasted.

    How an Array of Strings is Stored in Memory?

    We know that each char type occupies 1 byte in the memory. Hence, this array will be allocated a block of 150 bytes. Although this block is contagious memory locations, each group of 15 bytes constitutes a row.

    Assuming that the array is located at the memory address 1000, the logical layout of this array can be shown as in the following figure −

    Situated Memory Address

    An Array of Strings with Pointers

    To use the memory more efficiently, we can use the pointers. Instead of a 2D char array, we declare a 1D array of “char *” type.

    char*langs[10]={"PYTHON","JAVASCRIPT","PHP","NODE JS","HTML","KOTLIN","C++","REACT JS","RUST","VBSCRIPT"};

    In the 2D array of characters, the strings occupied 150 bytes. As against this, in an array of pointers, the strings occupy far less number of bytes, as each string is randomly allocated memory as shown below −

    Randomly Allocated Memory

    Note: Here, lang[ ] is an array of pointers of individual strings.

    Array of Pointers

    Example

    We can use a for loop as follows to print the array of strings −

    #include <stdio.h>intmain(){char*langs[10]={"PYTHON","JAVASCRIPT","PHP","NODE JS","HTML","KOTLIN","C++","REACT JS","RUST","VBSCRIPT"};for(int i =0; i <10; i++)printf("%s\n", langs[i]);return0;}

    Output

    When you run this code, it will produce the following output −

    PYTHON
    JAVASCRIPT
    PHP
    NODE JS
    HTML
    KOTLIN
    C++
    REACT JS
    RUST
    VBSCRIPT
    

    Here, langs is a pointer to an array of 10 strings. Therefore, if langs[0] points to the address 5000, then “langs + 1” will point to the address 5004 which stores the pointer to the second string.

    Hence, we can also use the following variation of the loop to print the array of strings −

    for(int i =0; i <10; i++){printf("%s\n",*(langs + i));}

    When strings are stored in array, there are a lot use cases. Let study some of the use cases.

    Find the String with the Largest Length

    In the following example, we store the length of first string and its position (which is “0”) in the variables “l” and “p” respectively. Inside the for loop, we update these variables whenever a string of larger length is found.

    Example

    Take a look at the following example −

    #include <stdio.h>#include <string.h>intmain(){char langs [10][15]={"PYTHON","JAVASCRIPT","PHP","NODE JS","HTML","KOTLIN","C++","REACT JS","RUST","VBSCRIPT"};int l =strlen(langs[0]);int p =0;for(int i =0; i <10; i++){if(strlen(langs[i])>= l){
    
         l =strlen(langs&#91;i]);
         p = i;}}printf("Language with the longest name: %s Length: %d", langs&#91;p], l);return0;}</code></pre>

    Output

    When you run this code, it will produce the following output −

    Language with longest name: JAVASCRIPT Length:10

    Sort a String Array in Ascending Order

    We need to use the strcmp() function to compare two strings. If the value of comparison of strings is greater than 0, it means the first argument string appears later than the second in alphabetical order. We then swap these two strings using the strcmp() function.

    Example

    Take a look at the following example −

    #include <stdio.h>#include <string.h>intmain(){char langs [10][15]={"PYTHON","JAVASCRIPT","PHP","NODE JS","HTML","KOTLIN","C++","REACT JS","RUST","VBSCRIPT"};int i, j;char temp[15];for(i =0; i <9; i++){for(j = i +1; j <10; j++){if(strcmp(langs[i], langs[j])>0){strcpy(temp, langs[i]);strcpy(langs[i], langs[j]);strcpy(langs[j], temp);}}}for(i =0; i <10; i++){printf("%s\n", langs[i]);}return0;}

    Output

    When you run this code, it will produce the following output −

    C++
    HTML
    JAVASCRIPT
    KOTLIN
    NODE JS
    PHP
    PYTHON
    REACT JS
    RUST
    VBSCRIPT
    

    In this chapter, we explained how you can declare an array of strings and how you can manipulate it with the help of string functions.

  • Strings

    Strings in C

    string in C is a one-dimensional array of char type, with the last character in the array being a “null character” represented by ‘\0’. Thus, a string in C can be defined as a null-terminated sequence of char type values.

    Creating a String in C

    Let us create a string “Hello”. It comprises five char values. In C, the literal representation of a char type uses single quote symbols such as ‘H’. These five alphabets put inside single quotes, followed by a null character represented by ‘\0’ are assigned to an array of char types. The size of the array is five characters plus the null character six.

    Example

    char greeting[6]={'H','e','l','l','o','\0'};

    Initializing String Without Specifying Size

    C lets you initialize an array without declaring the size, in which case the compiler automatically determines the array size.

    Example

    char greeting[]={'H','e','l','l','o','\0'};

    The array created in the memory can be schematically shown as follows −

    String Representation

    If the string is not terminated by “\0”, it results in unpredictable behavior.

    Note: The length of the string doesnt include the null character. The library function strlen() returns the length of this string as 5.

    Loop Through a String

    You can loop through a string (character array) to access and manipulate each character of the string using the for loop or any other loop statements.

    Example

    In the following example, we are printing the characters of the string.

    #include <stdio.h>#include <string.h>intmain(){char greeting[]={'H','e','l','l','o','\0'};for(int i =0; i <5; i++){printf("%c", greeting[i]);}return0;}

    Output

    It will produce the following output −

    Hello
    

    Printing a String (Using %s Format Specifier)

    C provides a format specifier “%s” which is used to print a string when you’re using functions like printf() or fprintf() functions.

    Example

    The “%s” specifier tells the function to iterate through the array, until it encounters the null terminator (\0) and printing each character. This effectively prints the entire string represented by the character array without having to use a loop.

    #include <stdio.h>intmain(){char greeting[]={'H','e','l','l','o','\0'};printf("Greeting message: %s\n", greeting );return0;}

    Output

    It will produce the following output −

    Greeting message: Hello
    

    You can declare an oversized array and assign less number of characters, to which the C compiler has no issues. However, if the size is less than the characters in the initialization, you may get garbage values in the output.

    char greeting[3]={'H','e','l','l','o','\0'};printf("%s", greeting);

    Constructing a String using Double Quotes

    Instead of constructing a char array of individual char values in single quotation marks, and using “\0” as the last element, C lets you construct a string by enclosing the characters within double quotation marks. This method of initializing a string is more convenient, as the compiler automatically adds “\0” as the last character.

    Example

    #include <stdio.h>intmain(){// Creating stringchar greeting[]="Hello World";// Printing stringprintf("%s\n", greeting);return0;}

    Output

    It will produce the following output −

    Hello World
    

    String Input Using scanf()

    Declaring a null-terminated string causes difficulty if you want to ask the user to input a string. You can accept one character at a time to store in each subscript of an array, with the help of a for loop −

    Syntax

    for(i =0; i <6; i++){scanf("%c",&greeting[i]);}
    greeting[i]='\0';

    Example

    In the following example, you can input a string using scanf() function, after inputting the specific characters (5 in the following example), we are assigning null (‘\0’) to terminate the string.

    printf("Starting typing... ");for(i =0; i <5; i++){scanf("%c",&greeting[i]);}// Assign NULL manually
    greeting[i]='\0';// Printing the stringprintf("Value of greeting: %s\n", greeting);

    Output

    Run the code and check its output −

    Starting typing... Hello
    Value of greeting: Hello
    

    Example

    It is not possible to input “\0” (the null string) because it is a non-printable character. To overcome this, the “%s” format specifier is used in the scanf() statement −

    #include <stdio.h>#include <string.h>intmain(){char greeting[10];printf("Enter a string:\n");scanf("%s", greeting);printf("You entered: \n");printf("%s", greeting);return0;}

    Output

    Run the code and check its output −

    Enter a string:
    Hello
    You entered:
    Hello
    

    Note: If the size of the array is less than the length of the input string, then it may result in situations such as garbage, data corruption, etc.

    String Input with Whitespace

    scanf(“%s”) reads characters until it encounters a whitespace (space, tab, newline, etc.) or EOF. So, if you try to input a string with multiple words (separated by whitespaces), then the C program would accept characters before the first whitespace as the input to the string.

    Example

    Take a look at the following example −

    #include <stdio.h>#include <string.h>intmain(){char greeting[20];printf("Enter a string:\n");scanf("%s", greeting);printf("You entered: \n");printf("%s", greeting);return0;}

    Output

    Run the code and check its output −

    Enter a string:
    Hello World!
    
    You entered:
    Hello
    

    String Input Using gets() and fgets() Functions

    To accept a string input with whitespaces in between, we should use the gets() function. It is called an unformatted console input function, defined in the “stdio.h” header file.

    Example: String Input Using gets() Function

    Take a look at the following example −

    #include <stdio.h>#include <string.h>intmain(){char name[20];printf("Enter a name:\n");gets(name);printf("You entered: \n");printf("%s", name);return0;}

    Output

    Run the code and check its output −

    Enter a name:
    Sachin Tendulkar
    
    You entered: 
    Sachin Tendulkar
    

    In newer versions of C, gets() has been deprecated. It is potentially a dangerous function because it doesnt perform bound checks and may result in buffer overflow.

    Instead, it is advised to use the fgets() function.

    fgets(char arr[], size, stream);

    The fgets() function can be used to accept input from any input stream, such as stdin (keyboard) or FILE (file stream).

    Example: String Input Using fgets() Function

    The following program uses fgets() and accepts multiword input from the user.

    #include <stdio.h>#include <string.h>intmain(){char name[20];printf("Enter a name:\n");fgets(name,sizeof(name),stdin);printf("You entered: \n");printf("%s", name);return0;}

    Output

    Run the code and check its output −

    Enter a name:
    Virat Kohli
    
    You entered: 
    Virat Kohli
    

    Example: String Input Using scanf(“%[^\n]s”)

    You may also use scanf(“%[^\n]s”) as an alternative. It reads the characters until a newline character (“\n”) is encountered.

    #include <stdio.h>#include <string.h>intmain(){char name[20];printf("Enter a name: \n");scanf("%[^\n]s", name);printf("You entered \n");printf("%s", name);return0;}

    Output

    Run the code and check its output −

    Enter a name: 
    Zaheer Khan
    
    You entered 
    Zaheer Khan
    

    Printing String Using puts() and fputs() Functions

    We have been using printf() function with %s specifier to print a string. We can also use puts() function (deprecated in C11 and C17 versions) or fputs() function as an alternative.

    Example

    Take a look at the following example −

    #include <stdio.h>#include <string.h>intmain(){char name[20]="Rakesh Sharma";printf("With puts(): \n");puts(name);printf("With fputs(): \n");fputs(name,stdout);return0;}

    Output

    Run the code and check its output −

    With puts(): 
    Harbhajan Singh
    
    With fputs(): 
    Harbhajan Singh