String Functions in C Programming
String Functions in C Programming
Introduction to String Functions in C Programming
String functions in C programming are used to perform operations on strings such as length calculation, copying, concatenation, and comparison.
These functions are available in the standard library and help simplify string manipulation.
String Library in C
To use string functions, include the string library:
Common String Functions in C Programming
strlen() Function
The strlen() function is used to find the length of a string.
#include <string.h>
int main() {
char str[] = “Hello”;
printf(“Length = %lu”, strlen(str));
return 0;
}
strcpy() Function
The strcpy() function is used to copy one string into another.
#include <string.h>
int main() {
char src[] = “C Programming”;
char dest[20];
strcpy(dest, src);
printf(“Copied string: %s”, dest);
return 0;
}
strcat() Function
The strcat() function is used to join two strings.
#include <string.h>
int main() {
char str1[20] = “Hello “;
char str2[] = “World”;
strcat(str1, str2);
printf(“Concatenated: %s”, str1);
return 0;
}
strcmp() Function
The strcmp() function is used to compare two strings.
#include <string.h>
int main() {
char str1[] = “Apple”;
char str2[] = “Banana”;
int result = strcmp(str1, str2);
printf(“Result = %d”, result);
return 0;
}
strrev() Function (Optional)
The strrev() function is used to reverse a string (not standard in all compilers).
#include <string.h>
int main() {
char str[] = “Hello”;
strrev(str);
printf(“Reversed: %s”, str);
return 0;
}
Example: Combine Multiple String Functions
#include <string.h>
int main() {
char str1[50] = “Hello”;
char str2[] = ” C”;
strcat(str1, str2);
printf(“New String: %s\n”, str1);
printf(“Length: %lu\n”, strlen(str1));
return 0;
}
Why String Functions are Important
Key Benefits
- Simplify string operations
- Save time and code
- Improve readability
- Useful in real-world applications
Common Mistakes
Avoid These Errors
- Not allocating enough memory
- Using unsafe functions like gets
- Forgetting null character
Start Learning C Programming
Practice string functions to improve your text processing skills in C programming.
Summary
String functions in C programming are used to manipulate strings easily. Common functions include strlen, strcpy, strcat, and strcmp.
FAQs
What are string functions in C programming?
They are functions used to perform operations on strings.
Which header file is used for string functions?
<string.h>
What does strlen do?
It returns the length of a string.
What does strcmp return?
It compares two strings and returns a value.



