strlen()
The strlen() function returns the number of characters in a string
#include <stdio.h> #include <string.h> int main() { char name[] = "Alice"; printf("Length of string: %d\n", strlen(name)); return 0; }
Output:
Length of string: 5
strcpy() String Copy
#include <stdio.h> #include <string.h> int main() { char src[] = "Hello"; char dest[20]; strcpy(dest, src); printf("Copied string: %s\n", dest); return 0; }
Output:
Copied string: Hello
strcat() String Concatenation
strcat() joins two strings.
#include <stdio.h> #include <string.h> int main() { char str1[20] = "Hello, "; char str2[] = "World!"; strcat(str1, str2); printf("Concatenated string: %s\n", str1); return 0; }
Concatenated string: Hello, World!
strcmp() String Compare
The strcmp() function compares two strings lexicographically (dictionary order).
#include <stdio.h> #include <string.h> int main() { char str1[] = "apple"; char str2[] = "banana"; int result = strcmp(str1, str2); if (result == 0) printf("Strings are equal\n"); else if (result > 0) printf("First string is greater\n"); else printf("Second string is greater\n"); return 0; }
Second string is greater