In data structures and programming, strings are often manipulated through a set of built-in library functions. These functions make it easy to perform common string operations without writing complex code. Here’s a breakdown of popular string library functions, particularly in languages like C, C++, Java, and Python.
1. String Length Functions
- Purpose: To determine the length of a string (i.e., the number of characters in it).
- Examples:
- C: strlen() – Returns the length of the string (excluding the null character).
- C++/Java/Python: Use .length() or len() to get the length of a string.
// C Example
#include <stdio.h>
#include <string.h>
int main() {
char str[] = “Hello”;
printf(“Length of string: %zu\n”, strlen(str)); // Output: 5
}
# Python Example
str = “Hello”
print(len(str)) # Output: 5
2. String Copy Functions
- Purpose: To copy the contents of one string into another.
- Examples:
- C: strcpy() and strncpy() – Copies a string (with strncpy() allowing you to specify the number of characters to copy).
- C++/Java/Python: Assignment operation = is often used for copying strings.
// C Example
#include <stdio.h>
#include <string.h>
int main() {
char src[] = “Hello”;
char dest[20];
strcpy(dest, src); // Copies “Hello” into dest
printf(“Copied String: %s\n”, dest); // Output: Hello
}
3. String Concatenation Functions
- Purpose: To join two strings together.
- Examples:
- C: strcat() and strncat() – Concatenate two strings (with strncat() allowing you to specify the number of characters to concatenate).
- C++/Java/Python: Use the + operator for concatenation.
// C Example
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = “Hello “;
char str2[] = “World!”;
strcat(str1, str2);
printf(“Concatenated String: %s\n”, str1); // Output: Hello World!
}
# Python Example
str1 = “Hello “
str2 = “World!”
print(str1 + str2) # Output: Hello World!
4. String Comparison Functions
- Purpose: To compare two strings lexicographically.
- Examples:
- C: strcmp() and strncmp() – Compares strings (with strncmp() allowing you to specify the number of characters to compare).
- C++/Java/Python: Use comparison operators (==, <, >) or .equals() in Java.
// C Example
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = “apple”;
char str2[] = “banana”;
if (strcmp(str1, str2) < 0)
printf(“str1 is less than str2\n”);
else if (strcmp(str1, str2) > 0)
printf(“str1 is greater than str2\n”);
else
printf(“str1 is equal to str2\n”);
}
5. String Search Functions
- Purpose: To search for a specific character or substring within a string.
- Examples:
- C: strchr() and strstr() – strchr() searches for a character, and strstr() searches for a substring.
- C++/Java/Python: .find() method can be used to locate a substring.
// C Example
#include <stdio.h>
#include <string.h>
int main() {
char str[] = “Hello World”;
char *pos = strchr(str, ‘o’); // Finds the first occurrence of ‘o’
printf(“First occurrence of ‘o’: %s\n”, pos); // Output: o World
}
# Python Example
str = “Hello World”
print(str.find(“World”)) # Output: 6 (index of substring)
6. String Tokenization Functions
- Purpose: To split a string into tokens based on a delimiter.
- Examples:
- C: strtok() – Splits a string by a specified delimiter.
- C++/Java/Python: .split() is commonly used for tokenizing strings.
// C Example
#include <stdio.h>
#include <string.h>
int main() {
char str[] = “Hello,World,Programming”;
char *token = strtok(str, “,”);
while (token != NULL) {
printf(“%s\n”, token);
token = strtok(NULL, “,”);
}
// Output: Hello
// World
// Programming
}
# Python Example
str = “Hello,World,Programming”
tokens = str.split(‘,’)
print(tokens) # Output: [‘Hello’, ‘World’, ‘Programming’]
7. String Uppercase/Lowercase Conversion
- Purpose: To convert a string to all uppercase or lowercase characters.
- Examples:
- C: Functions like toupper() and tolower() are used for individual characters.
- C++/Java/Python: .toUpperCase()/.toLowerCase() or .upper()/.lower().
// C Example
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = “Hello”;
for (int i = 0; str[i]; i++)
str[i] = toupper(str[i]);
printf(“Uppercase String: %s\n”, str); // Output: HELLO
}
# Python Example
str = “Hello World”
print(str.upper()) # Output: HELLO WORLD
8. String Reverse Functions
- Purpose: To reverse the characters in a string.
- Examples:
- C: Custom code is typically written to reverse a string.
- C++/Java/Python: .reverse() in some libraries or slicing in Python.
// C Example (Custom function)
#include <stdio.h>
#include <string.h>
void reverse(char *str) {
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len – i – 1];
str[len – i – 1] = temp;
}
}
int main() {
char str[] = “Hello”;
reverse(str);
printf(“Reversed String: %s\n”, str); // Output: olleH
}
# Python Example
str = “Hello”
print(str[::-1]) # Output: olleH
9. String Formatting
- Purpose: To create formatted strings with placeholders.
- Examples:
- C: sprintf() is used for formatted strings.
- Python: f-strings or .format() method.
// C Example
#include <stdio.h>
int main() {
char str[50];
int age = 25;
sprintf(str, “I am %d years old.”, age);
printf(“%s\n”, str); // Output: I am 25 years old.
}
# Python Example
age = 25
print(f”I am {age} years old.”) # Output: I am 25 years old.
Summary
These string library functions and methods simplify string manipulation tasks and make programming more efficient.