String Manipulation Functions
C provides a set of built-in library functions in <string.h> to
perform operations on strings.
strcpy()
Purpose:Copy one string into another.
Syntax: strcpy(destination, source);
Example:
char str1[20], str2[20];
strcpy(str1, "Hello");
strcpy(str2, str1); // str2 now contains "Hello"
strcat()
Purpose: Concatenate (join) two strings.
Syntax: strcat(destination, source);
Example:
char str1[20] = "Hello ", str2[] = "World";
strcat(str1, str2); // str1 now contains "Hello World"
strlen()
Purpose: Find the length of a string.
Syntax: int len = strlen(string);
Example:
char str[] = "Hello";
int len = strlen(str); // len = 5
strcmp()
Purpose: Compare two strings.
Syntax: strcmp(str1, str2);
Return Value:
0 if strings are equal
Negative if str1 < str2
Positive if str1 > str2
Example:
strcmp("abc", "abd"); // returns negative value
strrev() (Non-standard, may need custom function)
Purpose: Reverse a string.
Example:
char str[] = "Hello";
// After reverse: "olleH"
strchr()
Purpose: Find the first occurrence of a character in a string.
Syntax: char ptr = strchr(str, 'a');
strstr()
Purpose: Find the first occurrence of a substring in a string.
Syntax: char ptr = strstr(str, "sub");
String Library Functions
| Function | Purpose | Example |
| -------- | ----------------------------- | ------------------------- |
| strcpy | Copy string | strcpy(str2, str1); |
| strcat | Concatenate strings | strcat(str1, str2); |
| strlen | Find length of string | len = strlen(str); |
| strcmp | Compare strings | strcmp(str1, str2); |
| strchr | Find character in string | ptr = strchr(str, 'a'); |
| strstr | Find substring in string | ptr = strstr(str, "sub"); |
| strrev | Reverse string (non-standard) | strrev(str); |