String manipulation refers to the process of modifying, analyzing, or handling strings (sequences of characters) in programming. This includes a variety of operations that allow you to work with text data effectively. Common string manipulation tasks include:
- Concatenation: Joining two or more strings together.
- Substring Extraction: Retrieving a portion of a string.
- Length Calculation: Determining the number of characters in a string.
- Character Replacement: Changing specific characters within a string.
- Searching: Finding the position of a substring within a string.
- Trimming: Removing whitespace or specific characters from the beginning and end of a string.
- Case Conversion: Changing the case of characters (e.g., from lowercase to uppercase).
Example in C:
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[20] = "World";
// Concatenation
strcat(str1, " "); // Add a space
strcat(str1, str2); // str1 is now "Hello World"
// Length
printf("Length: %zu\n", strlen(str1)); // Output: Length: 11
// Substring (using pointer arithmetic)
char *sub = str1 + 6; // Points to "World"
printf("Substring: %s\n", sub); // Output: Substring: World
return 0;
}
In this example, various string manipulation operations are demonstrated. If you have more questions about specific string operations or need examples in another programming language, feel free to ask!
