What is String? Explain any five string functions with example which is available in String.h.
What is String? Explain any five string functions with example which is available in String.h.
Answer: a string in C is an array of characters ending with the null character '\0'. The header string.h provides functions such as strlen, strcpy, strcat, strcmp and strrev to work with strings.
Example: char name[10] = "Ravi"; is stored as 'R' 'a' 'v' 'i' '\0'. The '\0' marks where the string ends.
Five functions from string.h
'\0'.strlen("Hello") gives 5.src into dest.strcpy(s1, "Pune"); makes s1 = "Pune".src to the end of dest.s1 = "Good" and s2 = "Day", strcat(s1, s2) makes s1 = "GoodDay".s1 comes first alphabetically, a positive value otherwise.strcmp("abc", "abc") gives 0.strrev(s) with s = "abc" gives "cba".#include <stdio.h>
#include <string.h>
int main()
{
char s1[20] = "Good", s2[] = "Day", s3[20];
printf("%d\n", (int)strlen(s1)); /* 4 */
strcpy(s3, s1); /* s3 = "Good" */
strcat(s1, s2); /* s1 = "GoodDay" */
printf("%s\n", s1);
printf("%d\n", strcmp(s3, "Good")); /* 0 */
return 0;
}
Other useful ones: strncpy, strncat, strupr/strlwr (non-standard), strchr, strstr. Make sure the destination array is large enough for strcpy and strcat.
Log in or create a free account to answer this question.