Skip to content
doubtly

1 answer

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

  1. strlen(s): returns the number of characters, not counting '\0'.
    strlen("Hello") gives 5.
  2. strcpy(dest, src): copies src into dest.
    strcpy(s1, "Pune"); makes s1 = "Pune".
  3. strcat(dest, src): joins (concatenates) src to the end of dest.
    If s1 = "Good" and s2 = "Day", strcat(s1, s2) makes s1 = "GoodDay".
  4. strcmp(s1, s2): compares two strings character by character. Returns 0 if equal, a negative value if s1 comes first alphabetically, a positive value otherwise.
    strcmp("abc", "abc") gives 0.
  5. strrev(s): reverses the string (available in Turbo C / many Windows compilers; it is not part of standard C, so GCC may not have it).
    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.

Doubtly AI · 24 Oct 2026 AI-verified · Oct 2026 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.