Loading . . .

C Programming in VS Code

Printing a message in C

Beginner's Guide to Keywords, Identifiers, Constants, Variables, Datatypes in C

Operators & Expressions in C

Input & Output Operations in C

Control Statements in C

Loops in C

Array in C

Character Array & Strings in C

User Defined Functions in C

Structure and Union in C

Pointers in C


Disclaimer: Educational Purposes Only

The content on this website, including videos and code examples, is for educational purposes only. All demonstrations and designs are fictional and created to illustrate coding techniques. Any resemblance to existing websites or brands is purely coincidental.

The creators and administrators of this website do not claim ownership or affiliation with any existing websites or companies. Users are encouraged to use the information responsibly for learning purposes. Liability for any misuse of the content provided is not accepted.

Download Notes

Character Array & Strings in C

Character Array: It is a sequence of characters that are considered as a single data item.

Common operations performed on character strings includes:

1) Reading and writing strings.

2) Combining strings.

3) Copying one string to another.

4) Extracting a portion of a string.

strcat function: strcat() is a C function that concatenates (joins) two strings together. It takes two parameters: the destination string and the source string, and appends the source string to the end of the destination string.

strcmp function: strcmp() is a C function that compares two strings lexicographically, and returns 0 if they are equal, <0 or >0 otherwise.

strcpy function: strcpy() is a C function that copies the contents of a string (including the null terminator) from a source to a destination. It takes two parameters: the destination string and the source string.

strlen function: strlen() is a C function that returns the length of a string, not including the null terminator. It takes a single parameter, a string, and returns an integer value.

strstr function: strstr() is a C function that searches for the first occurrence of a substring within a string, and returns a pointer to the substring. It takes two parameters: the string being searched and the substring being searched for. It returns a pointer to the first occurrence of the substring, or NULL if the substring is not found.

Character Array & Strings in C

Reading string:

reading_string.c


#include <stdio.h>

void main() {
    char string[10];
    printf("Enter a string(Press Enter at the end): ");
    scanf("%s", string);
    printf("\nYou entered: %s", string);
}

Reading line:

reading_line.c


#include <stdio.h>

void main() {
    char line[100], letter;
    int i = 0;
    printf("Enter a line of text (Press Enter at the end): ");
    do {
        letter = getchar();
        line[i] = letter;
        i++;
    } while(letter != '\n');
    line[i] = '\0';
    printf("\nYou entered:\t\t%s", line);
}

Copying one string to another:

copying_one_string_to_another.c


#include <stdio.h>

void main() {
    char string1[10], string2[10];
    int i = 10;
    printf("Enter a string (Press Enter at the end): ");
    scanf("%s",string1);
    for (i = 0; string1[i] != '\0'; i++)
        string2[i] = string1[i];
    string2[i] = '\0';
    printf("\nYou entered: %s\nTotal characters: %d", string2, i);
}

Writing string to screen:

writing_string_to_screen.c


#include <stdio.h>

void main() {
    char string[20] = "Like and Share";
    printf("\nDo %s this video along with playlist.\n", string);
}

Putting strings together:

putting_strings_together.c


#include <stdio.h>

void main() {
    char string1[20] = "Customized", string2[5] = "Dev", string3[25];
    int i, j;
    for(i = 0; string1[i] != '\0'; i++)
        string3[i] = string1[i];
    string3[i] = ' ';
    for(j = 0; string2[j] != '\0'; j++)
        string3[i + j + 1] = string2[j];
    string3[i + j + 1] = '\0';
    printf("\nString1: %s\tString2: %s\tCombined String: %s", string1, string2, string3);
}

Comparing two strings:

compairing_two_strings.c


#include <stdio.h>

void main() {
    char string1[10] = "String1", string2[10] = "String2";
    int i =0;
    while(string1[i] == string2[i] && string1[i] != '\0' && string2[i] != '\0')
        i++;
    if(string1[i] == '\0' && string2[i] == '\0')
        printf("\nBoth String are Equal.");
    else
        printf("\nBoth String are Not Equal.");
}

Concating two strings:

concating_two_strings.c


#include <stdio.h>
#include <string.h>

void main() {
    char string1[15] = "Press Like", string2[10] = " & Share";
    printf("\n%s this video.\n", strcat(string1, string2));
}

Compairing two strings using strcmp:

compairing_two_strings_using_strcmp.c


#include <stdio.h>
#include <string.h>

void main() {
    char string1[10] = "String", string2[10] = "String";
    int compare_value;
    compare_value = strcmp(string1, string2);
    if(compare_value == 0)
        printf("\nBoth string are equal.\n");
    else
        printf("\nBoth string are not equal.\n");
}

Copying one string to another using strcpy:

copying_one_string_to_another_using_strcpy.c


#include <stdio.h>
#include <string.h>

void main() {
    char string1[100] = "This is original text but soon it will be copied.", string2[100];
    strcpy(string2, string1);
    printf("\nOriginal Text: %s\nCopied Text: %s\n", string1, string2);
}

Finding the length of string:

finding_string_length.c


#include <stdio.h>
#include <string.h>

void main() {
    char string[20] = "Customized Dev";
    printf("\nString: %s\nTotal characters: %lu\n", string, strlen(string));
}

Finding the substring:

finding_substring.c


#include <stdio.h>
#include <string.h>

void main() {
    char string1[10] = "customize", string2[10] = "custom";
    if(strstr(string1, string2) == NULL)
        printf("\nSubstring not found.\n");
    else
        printf("\nString2 is substring of String1.\n");
}