Strings in C

Author - Shlok Devi


str image

A String in C programming is a sequence of characters terminated with a null character ‘\0’. The C String is stored as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character ‘\0’. 'A String in C programming is a sequence of characters terminated with a null character ‘\0’. The C String is stored as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character ‘\0’.

Declaration:

char string_name[size];

Initialization:-

  • Assigning a String Literal without Size

    char str[] = "Revolution Software Development Club";
  • Assigning a String Literal with a Predefined Size.
    Strings can be assigned with a predefined size. If the required size is n then the size of string should be n+1 because the we have to account for a extra space for the null character.

    char str [25] = "Vishwakarma University";
  • Assigning Character by Character with Size.
    We can also assign a string character by character. But the end character should be the null character.

    char str [8] = { 'S','T','R','I','N','G','S','\0'};

String Functions:-

  • STRLEN: To find the length of the string.
    cpp image
  • STRCPY: To copy the string from the source to destination.
    cpp image
  • STRCAT: To concatenate the string from source to destination.
    cpp image
  • STRCMP:
    1. Compares two strings.
    2. Returns zero if two strings are equal, positive value if str1 is greater and negative value if str2 is greater.
    cpp image