Strings in C are arrays of characters ending with a special null character (\0
). Although C doesn’t have a dedicated string data type, it provides the <string.h>
library with built-in functions to simplify tasks like finding the length of a string, copying, concatenation, comparison, and more.
Below, you’ll find practical examples of commonly used string functions with simple explanations.
Examples of String Functions in C
1. strlen()
– Find String Length
This function calculates the number of characters in a string, excluding the null character (\0
).
#include <stdio.h>
#include <string.h>
int main() {
char text[] = "Master Coding";
printf("Length of the string: %lu\n", strlen(text));
return 0;
}
Output:
Length of the string: 13
2. strcpy()
– Copy Strings
The strcpy
function copies one string to another.
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello, World!";
char destination[20];
strcpy(destination, source);
printf("Copied string: %s\n", destination);
return 0;
}
Output:
Copied string: Hello, World!
3. strcat()
– Concatenate Strings
This function joins two strings by appending the second string to the first.
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello, ";
char str2[] = "World!";
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
Output:
Concatenated string: Hello, World!
4. strcmp()
– Compare Two Strings
Compares two strings and returns:
0
if both strings are equal.- A positive or negative value based on the lexicographical difference.
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Master";
char str2[] = "Coding";
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal.\n");
} else {
printf("Strings are not equal.\n");
}
return 0;
}
Output:
Strings are not equal.
5. strchr()
– Find Character in String
Searches for the first occurrence of a character in a string.
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Master Coding";
char *result = strchr(str, 'C');
if (result != NULL) {
printf("Character found at position: %ld\n", result - str + 1);
} else {
printf("Character not found.\n");
}
return 0;
}
Output:
Character found at position: 8
6. strncpy()
– Copy Limited Characters
Copies a specific number of characters from one string to another.
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Master Coding";
char destination[10];
strncpy(destination, source, 6);
destination[6] = '\0'; // Add null terminator
printf("Copied string: %s\n", destination);
return 0;
}
Output:
Copied string: Master
Real-Life Application
Here’s a real-world example using multiple string functions to simulate a login system.
#include <stdio.h>
#include <string.h>
int main() {
char stored_username[] = "admin";
char stored_password[] = "1234";
char username[20], password[20];
printf("Enter username: ");
scanf("%s", username);
printf("Enter password: ");
scanf("%s", password);
if (strcmp(username, stored_username) == 0 && strcmp(password, stored_password) == 0) {
printf("Login Successful!\n");
} else {
printf("Invalid Credentials.\n");
}
return 0;
}
Conclusion
C string functions simplify text manipulation, enabling you to handle complex operations like copying, concatenation, and comparison effortlessly. Mastering these functions helps you create robust and efficient programs.
For more insights and practical examples, visit Master Coding Science at mastercodingscience.com.