🟡 Intermediate · Lesson 23
String Functions (string.h)
String Functions
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] = "codekafunda"; 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 = CODEKAFUNDA
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.
String Functions Overview
C में string functions के लिए <string.h> header file use की जाती है। इससे length निकालना, copy करना, join करना, compare करना और substring search करना आसान हो जाता है।
Important Standard C Note
strrev(), strupr() और strlwr() standard C functions नहीं हैं। ये कुछ compilers में चलते हैं, लेकिन GCC/online compilers में error दे सकते हैं। Portable code के लिए manual loop और <ctype.h> use करें।
Standard String Functions
| Function | काम |
|---|---|
strlen(s) | String की length देता है |
strcpy(d,s) | s को d में copy करता है |
strcat(d,s) | s को d के end में जोड़ता है |
strcmp(s1,s2) | दो strings compare करता है |
strchr(s,ch) | character search करता है |
strstr(s,sub) | substring search करता है |
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] = "Namaste"; char s2[80] = "Duniya"; char rev[80] = "Code"; char up[80] = "codekafunda"; printf("Length = %d\n", (int)strlen(s1)); strcat(s1, " "); strcat(s1, s2); printf("Joined = %s\n", s1); printf("Compare = %d\n", strcmp("abc", "abc")); reverseString(rev); printf("Reverse = %s\n", rev); toUpperCase(up); printf("Uppercase = %s\n", up); return 0; }
Length = 7
Joined = Namaste Duniya
Compare = 0
Reverse = edoC
Uppercase = CODEKAFUNDA
Practice
- String में vowels और consonants count करें।
strcmp()के बिना दो strings compare करें।- Manual loop से string reverse करें।
- Sentence में words count करें।