C <string.h> String
In C, the <string.h>
library provides a variety of functions for manipulating strings. These functions enable operations such as copying, concatenating, comparing, and searching within strings. Below is an overview of some commonly used string functions available in <string.h>
, along with their syntax, purpose, and examples.
Common String Functions in <string.h>
strlen()
:- Purpose: Returns the length of a string (number of characters before the null terminator).
- Syntax:
size_t strlen(const char *str);
- Example:
char str[] = "Hello"; printf("Length: %zu\n", strlen(str)); // Output: Length: 5
strcpy()
:- Purpose: Copies the source string (including the null terminator) to the destination string.
- Syntax:
char *strcpy(char *dest, const char *src);
- Example:
char src[] = "Hello"; char dest[20]; strcpy(dest, src); printf("Copied String: %s\n", dest); // Output: Copied String: Hello
strcat()
:- Purpose: Concatenates (appends) the source string to the end of the destination string.
- Syntax:
char *strcat(char *dest, const char *src);
- Example:
char str1[20] = "Hello"; char str2[] = " World"; strcat(str1, str2); printf("Concatenated String: %s\n", str1); // Output: Concatenated String: Hello World
strcmp()
:- Purpose: Compares two strings lexicographically. Returns 0 if they are equal, a negative value if the first is less than the second, and a positive value if greater.
- Syntax:
int strcmp(const char *str1, const char *str2);
- Example:
char str1[] = "Hello"; char str2[] = "World"; int result = strcmp(str1, str2); printf("Comparison Result: %d\n", result); // Output: Comparison Result: -1
strchr()
:- Purpose: Searches for the first occurrence of a character in a string and returns a pointer to that character.
- Syntax:
char *strchr(const char *str, int character);
- Example:
char str[] = "Hello"; char *ptr = strchr(str, 'e'); if (ptr != NULL) { printf("Character found at position: %ld\n", ptr - str); // Output: Character found at position: 1 }
strstr()
:- Purpose: Finds the first occurrence of a substring in a string and returns a pointer to it.
- Syntax:
char *strstr(const char *haystack, const char *needle);
- Example:
char str[] = "Hello, World!"; char *ptr = strstr(str, "World"); if (ptr != NULL) { printf("Substring found at: %s\n", ptr); // Output: Substring found at: World! }
strncpy()
:- Purpose: Copies up to
n
characters from the source string to the destination string. If the source string is shorter thann
, the destination is padded with null bytes. - Syntax:
char *strncpy(char *dest, const char *src, size_t n);
- Example:
char src[] = "Hello"; char dest[10]; strncpy(dest, src, 3); dest[3] = '\0'; // Manually add null terminator printf("Copied String: %s\n", dest); // Output: Copied String: Hel
- Purpose: Copies up to
strncat()
:- Purpose: Appends up to
n
characters from the source string to the destination string. - Syntax:
char *strncat(char *dest, const char *src, size_t n);
- Example:
char str1[20] = "Hello"; char str2[] = " World!"; strncat(str1, str2, 5); printf("Concatenated String: %s\n", str1); // Output: Concatenated String: Hello World
- Purpose: Appends up to
sprintf()
:- Purpose: Formats and stores a string in a buffer (similar to
printf
, but writes to a string). - Syntax:
int sprintf(char *str, const char *format, ...);
- Example:
char buffer[50]; sprintf(buffer, "The answer is: %d", 42); printf("%s\n", buffer); // Output: The answer is: 42
- Purpose: Formats and stores a string in a buffer (similar to
Example of Using Multiple String Functions
Here's an example that combines several of the string functions listed above:
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello";
char str2[50] = "World!";
char buffer[100];
// Length of str1
printf("Length of str1: %zu\n", strlen(str1)); // Output: Length of str1: 5
// Copying str2 to buffer
strcpy(buffer, str2);
printf("Buffer after strcpy: %s\n", buffer); // Output: Buffer after strcpy: World!
// Concatenating str1 and str2
strcat(str1, " ");
strcat(str1, str2);
printf("Concatenated String: %s\n", str1); // Output: Concatenated String: Hello World!
// Comparing two strings
int cmpResult = strcmp(str1, buffer);
printf("Comparison result: %d\n", cmpResult); // Output: Comparison result: -1 (if str1 is less than buffer)
// Finding a character
char *foundChar = strchr(str1, 'W');
if (foundChar != NULL) {
printf("Character 'W' found at position: %ld\n", foundChar - str1); // Output: Position of 'W'
}
// Finding a substring
char *foundSubstr = strstr(str1, "Hello");
if (foundSubstr != NULL) {
printf("Substring 'Hello' found: %s\n", foundSubstr); // Output: Substring found: Hello World!
}
return 0;
}
Explanation of the Example
- String Length: The
strlen()
function is used to find the length ofstr1
. - Copying Strings: The
strcpy()
function copiesstr2
intobuffer
. - Concatenation: The
strcat()
function concatenatesstr1
andstr2
with a space in between. - Comparison: The
strcmp()
function comparesstr1
withbuffer
. - Character Search: The
strchr()
function locates the character'W'
instr1
. - Substring Search: The
strstr()
function finds the substring"Hello"
instr1
.
Important Points About String Functions
- Null Terminator: Most string functions assume that the strings are null-terminated. Ensure that your strings are properly null-terminated to avoid undefined behavior.
- Memory Management: Be cautious of buffer sizes when using functions that modify strings. Always ensure the destination buffer is large enough to hold the resulting string.
- Return Values: Many string functions return pointers to strings or memory locations, while others return integers (e.g., comparison results). Be mindful of these return types when using the functions.
- Standard Library: To use these functions, you need to include the
<string.h>
header file in your program.