🟡 Intermediate · Lesson 23
String Functions (string.h)
String Functions Overview
C provides many useful string functions in the <string.h> header file. These functions help you find length, copy, join, compare and search strings.
Important Standard C Note
strrev(), strupr() and strlwr() are not standard C functions. Some compilers support them, but GCC and many online compilers may not. Use manual loops or <ctype.h> for portable code.
Standard String Functions
| Function | Syntax | Purpose |
|---|---|---|
strlen() | strlen(s) | Returns length excluding null character '\0' |
strcpy() | strcpy(dest, src) | Copies source string into destination |
strncpy() | strncpy(dest, src, n) | Copies at most n characters |
strcat() | strcat(dest, src) | Appends source string to destination |
strncat() | strncat(dest, src, n) | Appends at most n characters |
strcmp() | strcmp(s1, s2) | Compares two strings |
strncmp() | strncmp(s1, s2, n) | Compares first n characters |
strchr() | strchr(s, ch) | Finds first occurrence of a character |
strstr() | strstr(s, sub) | Finds first occurrence of a substring |
Complete Demo Program
C Language – Standard String Functions
#include <stdio.h> #include <string.h> #include <ctype.h> void reverseString(char s[]) { int i = 0, j = strlen(s) - 1; while (i < j) { char temp = s[i]; s[i] = s[j]; s[j] = temp; i++; j--; } } void toUpperCase(char s[]) { for (int i = 0; s[i] != '\0'; i++) { s[i] = toupper((unsigned char)s[i]); } } int main() { char s1[80] = "Hello"; char s2[80] = "World"; char copy[80]; char rev[80] = "Gagan"; char up[80] = "codingeasily"; printf("Length = %d\n", (int)strlen(s1)); strcpy(copy, s1); printf("Copy = %s\n", copy); strcat(s1, " "); strcat(s1, s2); printf("Joined = %s\n", s1); printf("Compare = %d\n", strcmp("abc", "abc")); printf("Substring = %s\n", strstr("Hello World", "World")); reverseString(rev); printf("Reverse = %s\n", rev); toUpperCase(up); printf("Uppercase = %s\n", up); return 0; }
Length = 5
Copy = Hello
Joined = Hello World
Compare = 0
Substring = World
Reverse = nagaG
Uppercase = CODINGEASILY
Practice Questions
- Write a program to count vowels and consonants in a string.
- Write a program to check whether two strings are equal without using
strcmp(). - Write a program to reverse a string manually.
- Write a program to count words in a sentence.
Frequently Asked Questions
What are the main string functions in C?
The most used string functions live in
<string.h>: strlen for length, strcpy to copy, strcat to join, strcmp to compare, and strchr/strstr to search. They handle the common tasks of measuring, copying, combining and comparing text.What header file is needed for string functions in C?
You include
<string.h> to use the standard string functions. Without it the compiler will not recognise names like strlen or strcpy, so this header is required whenever you work with these functions.What is the difference between strcpy and strcat?
strcpy copies one string over another, replacing its contents, while strcat appends one string to the end of another, keeping the first part. Both need the destination to be large enough to hold the result plus the null terminator.How does strcmp work in C?
strcmp compares two strings character by character and returns 0 if they are equal, a negative value if the first is smaller, and a positive value if it is larger. It is commonly used in an if to check whether two strings match.Why must destination arrays be large enough for string functions?
Functions like
strcpy and strcat do not check the destination size, so if it is too small they write past the end of the array, corrupting memory. You must size the destination to hold the full result including the terminating '\0'.💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.