C Strings

C strings are essentially arrays of characters terminated by a special null character ('\0').

</>
Copy
char greeting[] = "Hello World!";

The null terminator '\0' indicates where the string ends.

Handle Strings with Care in C

Unlike higher-level string types found in other languages, C strings do not automatically manage memory or track their length, so developers must carefully allocate enough space and ensure proper termination to prevent buffer overflows or undefined behavior.

Using Strings

In practice, C strings are used to represent text, store user input, and manipulate data in many applications.

Functions such as strcpy()strcat()strlen(), and strcmp() provide basic operations to copy, concatenate, measure, and compare these null-terminated arrays.

Because C strings are low-level constructs, they offer great performance and control but also require diligent memory management and error checking by the programmer.


Basic Operations on Strings

1. Declaring and Printing a String

This example demonstrates how to declare a C string and print it using printf(). The string is stored in a character array and automatically terminated with a null character.

</>
Copy
#include <stdio.h>

int main() {
    char greeting[] = "Hello World!";
    printf("%s\n", greeting);
    return 0;
}

Program Output:

Hello World!

Explanation:

  1. A character array greeting is declared and initialized with the string literal “Hello, World!”.
  2. The compiler automatically appends a null character '\0' at the end of the string.
  3. The printf() function prints the string until it encounters the null terminator.

2. Finding the Length of a String

This example shows how to calculate the length of a string using the strlen() function from the <string.h> library.

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

int main() {
    char str[] = "Hello World!";
    size_t len = strlen(str);
    printf("Length of the string: %zu\n", len);
    return 0;
}

Program Output:

Length of the string: 12

Explanation:

  1. The string "Hello, world!" is stored in the character array str.
  2. The function strlen() computes the number of characters before the null terminator.
  3. The computed length is stored in the variable len and then printed.

3. Concatenating Two Strings

This example demonstrates how to concatenate two C strings using the strcat() function.

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

int main() {
    char first[50] = "Hello ";
    char second[] = "World!";
    strcat(first, second);
    printf("Concatenated string: %s\n", first);
    return 0;
}

Program Output:

Concatenated string: Hello World!

Explanation:

  1. The character array first is declared with enough space to hold both strings.
  2. The string second is concatenated to the end of first using strcat().
  3. The resulting string is printed, showing the combination of the two original strings.

4. Copying a String

This example shows how to copy one string into another using the strcpy() function from the <string.h> library.

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

int main() {
    char src[] = "Hello World!";
    char dest[50];
    strcpy(dest, src);
    printf("Copied string: %s\n", dest);
    return 0;
}

Program Output:

Copied string: Hello World!

Explanation:

  1. The source string "Hello World!" is stored in the array src.
  2. The destination array dest is declared with sufficient space to hold the copied string.
  3. The strcpy() function copies the contents of src into dest, including the null terminator.
  4. The copied string is then printed using printf().

C String Tutorials

In the above examples, we have covered only a few cases about strings. The following series of tutorials cover an extensive range of use-cases and scenarios related to Strings in C language.

1 Basic String Handling

  1. How to Declare and Initialize a String in C
  2. How to Read a String from Console in C
  3. How to Read a Multi-Word String from Console in C
  4. How to Print a String in C
  5. How to Differentiate Between Character Arrays and Pointers in Strings in C
  6. How to Dynamically Allocate Memory for Strings in C

2 String Manipulation

  1. How to Find the Length of a String in C
  2. How to Copy One String to Another in C
  3. How to Concatenate Two Strings in C
  4. How to Compare Two Strings in C
  5. How to Check if Two Strings are Equal Ignoring Case in C
  6. How to Get a Character at a Specific Index in a String in C
  7. How to Find a Substring in a String in C
  8. How to Split a String in C
  9. How to Split a String by Space in C
  10. How to Split a String by Comma in C
  11. How to Convert a String to Uppercase in C
  12. How to Convert a String to Lowercase in C
  13. How to Reverse a String in C
  14. How to Count Occurrences of a Character in a String in C
  15. How to Remove a Specific Character from a String in C
  16. How to Remove Leading and Trailing Spaces from a String in C
  17. How to Replace a Substring Within a String in C
  18. How to Convert a String to an Integer in C
  19. How to Convert a String to a Float in C
  20. How to Convert an Integer to a String in C

3 Basic String Validations

  1. How to Check if a String is Empty in C
  2. How to Check if Two Strings Are Equal in C
  3. How to Check if a String Contains Only Digits in C
  4. How to Check if a String Contains Only Alphabets in C
  5. How to Check if a String Contains Only Alphanumeric Characters in C
  6. How to Check if a String Contains Only Whitespace Characters in C
  7. How to Check if a String Starts with a Specific Prefix
  8. How to Check if a String Ends with a Specific Suffix
  9. How to Check if a String Contains a Specific Substring
  10. How to Check if a String Contains a Specific Character in C

4 Case and Palindrome Checks

  1. How to Check if a String is Uppercase in C
  2. How to Check if a String is Lowercase in C
  3. How to Check if a String is a Palindrome in C
  4. How to Check if a String follows Camel Case in C
  5. How to Check if a String follows Snake Case in C

5 Pattern Matching & Formatting Checks

  1. How to Check if a String is a valid Email Address in C
  2. How to Check if a String is a valid Phone Number in C
  3. How to Check if a String is a valid URL in C
  4. How to Check if a String is a valid IPv4 Address in C
  5. How to Check if a String is a valid IPv6 Address in C
  6. How to Check if a String is a valid Hexadecimal Number in C
  7. How to Check if a String is a valid Binary Number in C
  8. How to Check if a String is a valid Floating-Point Number in C

6 Special Character Checks

  1. How to Check if a String Contains Special Characters in C
  2. How to Check if a String Contains Only Punctuation Marks in C
  3. How to Check if a String Contains Non-ASCII Characters in C
  4. How to Check if a String has Duplicate Characters in C
  5. How to Check if a String has Balanced Parentheses in C

7 Substring & Repetition Checks

  1. How to Check if a String is a Rotation of Another String in C
  2. How to Check if a String has Repeating Characters in C
  3. How to Check if a String contains Repeated Substrings in C
  4. HHow to Check if a String has All Unique Characters in C

8 Advanced String Handling

  1. How to Sort an Array of Strings in Alphabetical Order in C
  2. How to Find the Most Frequently Occurring Character in a String in C