In C programming, a string is a sequence of characters that is terminated by a null character (‘\0’). A string can be represented as a one-dimensional character array.
Here’s a table of commonly used string functions in C, along with their brief descriptions and examples:
Function | Discription | Example |
---|---|---|
Strlen() | returns the length of a string | char str[] = "Hello, world!"; int len = strlen(str); printf("Length is %d", len); |
Strcpy() | copies one string to another | char str1[] = "Hello"; char str2[10]; strcpy(str2, str1); printf("%s", str2); |
Strcat() | concatenates two strings | char str1[] = "Hello"; char str2[] = ", world!"; strcat(str1, str2); printf("%s", str1); |
Strcmp() | compares two strings | char str1[] = "Hello"; char str2[] = "World"; int result = strcmp(str1, str2); if (result == 0) {...} |
Strchr() | searches for a character in a string | char str[] = "Hello, world!"; char *ptr = strchr(str, 'w'); printf("%s", ptr); |
Strstr() | searches for a substring in a string | char str[] = "Hello, world!"; char *ptr = strstr(str, "world"); printf("%s", ptr); |
Strncpy() | copies a specified number of characters from one string to another | char str1[] = "Hello"; char str2[10]; strncpy(str2, str1, 3); printf("%s", str2); |
Strncat() | concatenates a specified number of characters from one string to another | char str1[] = "Hello"; char str2[] = ", world!"; strncat(str1, str2, 5); printf("%s", str1); |
Strncmp() | compares a specified number of characters from two strings | char str1[] = "Hello"; char str2[] = "Hell"; int result = strncmp(str1, str2, 4); if (result == 0) {...} |
Strtok() | breaks a string into tokens using a specified delimiter | char str[] = "Hello, world!"; char *ptr = strtok(str, ", "); while (ptr != NULL) {...} |
Sprint() | writes formatted data to a string | char str[20]; int num = 42; sprintf(str, "%d", num); printf("%s", str); |
Snprintf() | writes formatted data to a string with a specified maximum length | char str[20]; int num = 42; snprintf(str, 20, "%d", num); printf("%s", str); |
Strrev() | reverses a string | char str[20]; printf("Enter string: "); gets(str);//reads string from console. printf("String is: %s",str); printf("\nReverse String is: %s",strrev(str)); |
#include <stdio.h>
#include <string.h>
int main()
{
// defining string
char str[] = “Use String Length”;
// getting length of str using strlen()
int length = strlen(str);
printf(“String Length is : %d”, length);
return 0;
}
Output :- String Length is : 17
#include <stdio.h>
#include <string.h>
int main()
{
// defining string
char str[] = “Use String Length”;
// getting length of str using strlen()
int length = strlen(str);
printf(“String Length is : %d”, length);
return 0;
}
Output :- String Length is : 17
#include <stdio.h>
#include <string.h>
int main()
{
// defining string
char str[] = “Use String Length”;
// getting length of str using strlen()
int length = strlen(str);
printf(“String Length is : %d”, length);
return 0;
}
Output :- String Length is : 17
#include <stdio.h>
#include <string.h>
int main()
{
// defining string
char str[] = “Use String Length”;
// getting length of str using strlen()
int length = strlen(str);
printf(“String Length is : %d”, length);
return 0;
}
Output :- String Length is : 17
#include <stdio.h>
#include <string.h>
int main()
{
// defining string
char str[] = “Use String Length”;
// getting length of str using strlen()
int length = strlen(str);
printf(“String Length is : %d”, length);
return 0;
}
Output :- String Length is : 17
#include <stdio.h>
#include <string.h>
int main()
{
// defining string
char str[] = “Use String Length”;
// getting length of str using strlen()
int length = strlen(str);
printf(“String Length is : %d”, length);
return 0;
}
Output :- String Length is : 17
#include <stdio.h>
#include <string.h>
int main()
{
// defining string
char str[] = “Use String Length”;
// getting length of str using strlen()
int length = strlen(str);
printf(“String Length is : %d”, length);
return 0;
}
Output :- String Length is : 17
#include <stdio.h>
#include <string.h>
int main()
{
// defining string
char str[] = “Use String Length”;
// getting length of str using strlen()
int length = strlen(str);
printf(“String Length is : %d”, length);
return 0;
}
Output :- String Length is : 17
These functions are all part of the standard C library, and can be included by adding #include <string.h> to your source code.