Strings in C Language:
Strings in C
Strings in C are arrays of characters terminated by a null character '\0'. This null character indicates the end of the string.
Declaring Strings
Strings can be declared in two ways:
#include
int main() {
// Declaration using character array
char str1[20] = "Hello, World!";
// Declaration using pointer
char *str2 = "Hello, World!";
printf("%s\n", str1);
printf("%s\n", str2);
return 0;
}
String Handling Functions
C provides several standard library functions to manipulate strings. Here are some commonly used string functions:
1. strlen()
Returns the length of the string (excluding the null character).
#include
#include
int main() {
char str[] = "Hello, World!";
printf("Length of string: %lu\n", strlen(str));
return 0;
}
2. strcpy()
Copies the source string to the destination string.
#include
#include
int main() {
char src[] = "Hello, World!";
char dest[20];
strcpy(dest, src);
printf("Destination string: %s\n", dest);
return 0;
}
3. strcat()
Appends the source string to the destination string.
#include
#include
int main() {
char dest[20] = "Hello, ";
char src[] = "World!";
strcat(dest, src);
printf("Concatenated string: %s\n", dest);
return 0;
}
4. strcmp()
Compares two strings lexicographically.
#include
#include
int main() {
char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal\n");
} else if (result < 0) {
printf("str1 is less than str2\n");
} else {
printf("str1 is greater than str2\n");
}
return 0;
}
5. strncpy()
Copies a specified number of characters from the source string to the destination string.
#include
#include
int main() {
char src[] = "Hello, World!";
char dest[20];
strncpy(dest, src, 5);
dest[5] = '\0'; // Adding null character to terminate the string
printf("Destination string: %s\n", dest);
return 0;
}
6. strncat()
Appends a specified number of characters from the source string to the destination string.
#include
#include
int main() {
char dest[20] = "Hello, ";
char src[] = "World!";
strncat(dest, src, 3);
printf("Concatenated string: %s\n", dest);
return 0;
}
7. strncmp()
Compares a specified number of characters from two strings lexicographically.
#include
#include
int main() {
char str1[] = "Hello";
char str2[] = "Helium";
int result = strncmp(str1, str2, 3);
if (result == 0) {
printf("First 3 characters are equal\n");
} else if (result < 0) {
printf("str1 is less than str2\n");
} else {
printf("str1 is greater than str2\n");
}
return 0;
}
8. strchr()
Finds the first occurrence of a character in a string.
#include
#include
int main() {
char str[] = "Hello, World!";
char ch = 'o';
char *result = strchr(str, ch);
if (result) {
printf("Character found at position: %ld\n", result - str);
} else {
printf("Character not found\n");
}
return 0;
}
9. strstr()
Finds the first occurrence of a substring in a string.
#include
#include
int main() {
char str[] = "Hello, World!";
char substr[] = "World";
char *result = strstr(str, substr);
if (result) {
printf("Substring found at position: %ld\n", result - str);
} else {
printf("Substring not found\n");
}
return 0;
}
10. sprintf()
Writes formatted data to a string.
#include
int main() {
char str[50];
int age = 25;
sprintf(str, "I am %d years old.", age);
printf("%s\n", str);
return 0;
}
11. sscanf()
Reads formatted data from a string.
#include
int main() {
char str[] = "25";
int age;
sscanf(str, "%d", &age);
printf("Age: %d\n", age);
return 0;
}
Common Use Cases of Strings
Strings are commonly used in various scenarios in programming, such as:
- Displaying messages and prompts to the user
- Handling user input and storing text data
- Processing and manipulating text data
- Implementing various algorithms like string searching and matching
Examples
Here are some additional examples demonstrating the usage of strings in C:
Example: Reversing a String
#include
#include
int main() {
char str[] = "Hello, World!";
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;
}
printf("Reversed string: %s\n", str);
return 0;
}
Example: Converting a String to Uppercase
#include
#include
int main() {
char str[] = "Hello, World!";
for (int i = 0; str[i] != '\0'; i++) {
str[i] = toupper(str[i]);
}
printf("Uppercase string: %s\n", str);
return 0;
}
Example: Tokenizing a String
#include
#include
int main() {
char str[] = "Hello, World! How are you?";
char *token = strtok(str, " ");
while (token != NULL) {
printf("%s\n", token);
token = strtok(NULL, " ");
}
return 0;
}