Strings in C with Examples: String Functions
What is a String in C?
A string in C is an array of characters terminated by a null character '\0'.
Declaring Strings
char name[20] = "Hello";
char name2[] = {'H','i','\0'};
Common String Functions (string.h)
strlen(str)— returns the length of a string.strcpy(dest, src)— copies one string into another.strcat(dest, src)— appends one string to another.strcmp(str1, str2)— compares two strings, returns 0 if equal.strrev(str)— reverses a string (non-standard, compiler-dependent).
char a[20] = "Hello";
printf("%d", strlen(a)); // 5
PreviousMultidimensional Arrays in C: 2D and 3D Arrays
Next Call by Value and Call by Reference in C
Ready to master real-world C Programming development?
Learn C Programming hands-on with mentor-led, live sessions.
.png)