Strings in C (Character Arrays)

In C programming, strings are essentially arrays of characters. A string is a sequence of characters that is terminated by a special character called the null character ('\0'). C does not have a built-in string data type like some other languages, so strings are typically represented as arrays of characters.

Let’s explore how strings (character arrays) work in C.

String Handling Functions in C
strlen(), strcpy() , strncpy(), strcat(),strncat(), strcmp(), strncmp(), strchr(), strrchr(),strstr(),strtok(), strdup(), strrev(), memset(), memcpy(), memmove(), memcmp()


What is a String?

A string in C is a sequence of characters stored in a character array, and it is always terminated by a null character ('\0'). The null character marks the end of the string, allowing C to determine where the string ends in memory.

Example:

In this example:

  • The string "Hello" is stored in the array name.
  • The size of the array is 6, including the 5 characters of "Hello" and the null terminator '\0'.

Null Character:

The null character ('\0') is essential for marking the end of the string in C. It helps functions like printf and strlen determine where the string stops.


Declaring and Initializing Strings

You can declare and initialize strings in C in various ways.

1. Using Character Array with Explicit Size:

You can declare a character array with a fixed size and manually add a null character to terminate the string.

2. Using String Literals:

C allows you to use string literals (a sequence of characters enclosed in double quotes) to initialize character arrays. When using a string literal, the compiler automatically adds the null character.

char greeting[] = "Hello";
  • The array greeting has enough space for all characters in "Hello" plus the null terminator.
  • This is equivalent to the previous example but more convenient.

Accessing Characters in a String

Since strings are arrays, you can access individual characters using array indexing. The first character is at index 0, and the last character (before the null terminator) is at index n-1 (where n is the length of the string).

Example:

Output:

You can also modify individual characters of a string since it is an array.

Example:

Output:


String Handling Functions in C

  1. strlen() – Find the length of a string.
  2. strcpy() – Copy a string.
  3. strncpy() – Copy a specified number of characters from one string to another.
  4. strcat() – Concatenate two strings.
  5. strncat() – Concatenate a specified number of characters from one string to another.
  6. strcmp() – Compare two strings.
  7. strncmp() – Compare a specified number of characters from two strings.
  8. strchr() – Locate the first occurrence of a character in a string.
  9. strrchr() – Locate the last occurrence of a character in a string.
  10. strstr() – Locate a substring in a string.
  11. strtok() – Split a string into tokens.
  12. strdup() – Duplicate a string.
  13. strrev() – Reverse a string (non-standard function, supported in some C libraries).
  14. memset() – Fill a block of memory with a particular value.
  15. memcpy() – Copy a block of memory.
  16. memmove() – Move a block of memory.
  17. memcmp() – Compare two blocks of memory.

The C Standard Library provides several functions to handle and manipulate strings. These functions are declared in the <string.h> header file. Here is a list of commonly used string handling functions:


Explanation of String Handling Functions with Syntax and Examples


1. strlen() – Find the Length of a String

The strlen() function is used to calculate the length of a string, excluding the null character \0.

  • Syntax:
  • Example:
  • Output:

2. strcpy() – Copy a String

The strcpy() function copies a string from one location to another, including the null terminator \0.

  • Syntax:
  • Example:
  • Output:

3. strncpy() – Copy Specified Number of Characters

The strncpy() function copies a specified number of characters from the source string to the destination string.

  • Syntax:
  • Example:
  • Output:

4. strcat() – Concatenate Two Strings

The strcat() function appends the source string to the destination string.

  • Syntax:
  • Example:
  • Output:

5. strncat() – Concatenate Specified Number of Characters

The strncat() function concatenates a specified number of characters from the source string to the destination string.

  • Syntax:
  • Example:
  • Output:

6. strcmp() – Compare Two Strings

The strcmp() function compares two strings lexicographically (character by character).

  • Syntax:
  • Example:
  • Output:

7. strncmp() – Compare Specified Number of Characters

The strncmp() function compares a specified number of characters from two strings.

  • Syntax:
  • Example:
  • Output:

8. strchr() – Locate First Occurrence of Character

The strchr() function locates the first occurrence of a character in a string.

  • Syntax:
  • Example:
  • Output:

9. strrchr() – Locate Last Occurrence of Character

The strrchr() function locates the last occurrence of a character in a string.

  • Syntax:
  • Example:
  • Output:

10. strstr() – Locate Substring in a String

The strstr() function locates the first occurrence of a substring in a string.

  • Syntax:
  • Example:
  • Output:

11. strtok() – Split a String into Tokens

The strtok() function splits a string into tokens based on a delimiter.

  • Syntax:
  • Example:
  • Output:


12. strdup() – Duplicate a String

The strdup() function allocates memory and duplicates a string. It returns a pointer to the newly allocated copy of the string. This function is not part of the C standard but is available in many C libraries.

  • Syntax:
  • Example:
  • Output:

13. strrev() – Reverse a String (Non-standard)

The strrev() function reverses the characters of a string in place. This function is non-standard and may not be available in all C libraries.

  • Syntax:
  • Example:
  • Output:

14. memset() – Fill a Block of Memory with a Particular Value

The memset() function sets all bytes in a block of memory to a particular value.

  • Syntax:
  • Example:
  • Output:

15. memcpy() – Copy a Block of Memory

The memcpy() function copies a specified number of bytes from the source memory block to the destination memory block. It’s often used for fast copying of large amounts of data.

  • Syntax:
  • Example:
  • Output:

16. memmove() – Move a Block of Memory

The memmove() function is similar to memcpy(), but it is safer when the source and destination memory blocks overlap. It ensures that the source block is copied to a temporary location before moving to the destination, preventing data corruption.

  • Syntax:
  • Example:
  • Output:

17. memcmp() – Compare Two Blocks of Memory

The memcmp() function compares the first num bytes of two memory blocks and returns an integer indicating their relationship. It is typically used to compare binary data or strings stored in memory.

  • Syntax:
  • Example:
  • Output:

Summary of String and Memory Handling Functions in C

Here’s a complete summary of all the string and memory handling functions discussed:

NoFunctionDescriptionExample Output
1strlen()Returns the length of a string (excluding the null character).Length of the string is: 13
2strcpy()Copies one string to another, including the null terminator.Copied string: Hello, World!
3strncpy()Copies a specified number of characters from one string to another.Copied string: Hello
4strcat()Concatenates two strings (appends source to destination).Concatenated string: Hello, World!
5strncat()Concatenates a specified number of characters from one string to another.Concatenated string: Hello, Wo
6strcmp()Compares two strings lexicographically.Strings are not equal
7strncmp()Compares a specified number of characters from two strings.First 3 characters are equal
8strchr()Finds the first occurrence of a character in a string.First occurrence of ‘o’ found at position: 4
9strrchr()Finds the last occurrence of a character in a string.Last occurrence of ‘o’ found at position: 8
10strstr()Finds the first occurrence of a substring in a string.Substring ‘World’ found at position: 7
11strtok()Splits a string into tokens based on a delimiter.Token: Hello \n Token: World
12strdup()Duplicates a string and returns a pointer to the copy.Original: Hello, World! \n Duplicate: Hello, World!
13strrev()Reverses a string in place (non-standard).Original: Hello, World! \n Reversed: !dlroW ,olleH
14memset()Fills a block of memory with a specified value.Modified string: —–, World!
15memcpy()Copies a block of memory from one location to another.Copied string: Hello, World!
16memmove()Moves a block of memory (safe for overlapping regions).After memmove: Hello, Hello!
17memcmp()Compares two blocks of memory byte by byte.str1 is less than str2

Explanation of Key Functionality:

  1. String Manipulation:
  • Basic Operations: Functions like strlen(), strcpy(), and strcat() allow you to measure, copy, and concatenate strings.
  • Comparison: Functions like strcmp() and strncmp() let you compare strings lexicographically.
  • Searching: Functions like strchr(), strrchr(), and strstr() help locate characters or substrings within a string.
  1. Memory Handling:
  • Filling Memory: The memset() function is used to set all bytes in a block of memory to a specified value.
  • Copying Memory: memcpy() is a fast way to copy memory from one location to another, while memmove() ensures safe copying even when the source and destination overlap.
  • Comparing Memory: The memcmp() function compares two blocks of memory to determine whether they are identical or different.
  1. Special Operations:
  • Tokenization: strtok() is useful for splitting a string into tokens based on delimiters.
  • Reversing and Duplicating Strings: strrev() reverses a string (though non-standard), and strdup() creates a duplicate of a string, dynamically allocating memory for the new copy.

These functions form the backbone of string and memory manipulation in C programming, providing essential tools for managing data efficiently. By mastering these functions, you can handle strings, copy memory, compare data, and perform various other operations that are critical in systems-level programming.

error: Content is protected !!
Open chat
1
Hi,how Can We Help You ?