Definition of String in C

  • A string in C is typically a single-dimensional character array ending with a null character.
  • String in C are groups of characters that include letters, digits, and symbols enclosed in quotation marks.
  • The string is an array/sequence of characters that is terminated by a null character ‘\0’

Syntax String in C

  • Declaration of Strings :
    • To declare a string, the data type is specified first as char and the number of characters in the array is placed in square brackets after the string name.
    • Syntax : char StringName[size];
    • Example : char str[30];
  • Initialization of Strings :
    • The string can be initialized simply as follows:
      • char str1[ 8] = {‘W’, ‘E’, ‘L’, ‘C’, ‘O’, ‘M’, ‘E’, ‘\0’};
      • char str1[ 8] = “WELCOME”;
      • char str1[ ] = “WELCOME”;
    • If we don’t specify the size of the character array, but do specify a string literal, then C will set the character array to the size of the string literal, including the null terminator automatically.
    • If the string is too small for the string literal, then the string literal will be truncated. If the string literal (including its null terminator) is smaller than the character array, then the final characters in the array will be undefined.
    • We can assign a string constant to a char array – either with no size specified, or we can specify a size plus one for a null character.

Feature/Characteristics String in C

  • The end of the string is marked with a special character, called Null Character( ‘\0’ ), which is a backslash symbol with zero numeric value. A null character is a character that has all its bits set to 0. A null character has a special meaning when interpreted as text. In C programming language, null character is used to mark the end of a character string.
  • The C compiler inserts the NULL (‘\0’) character automatically at the end of the string. So initialization of the NULL character is not essential.
  • There is a difference between a single character in character form stored in memory and a single character in string form stored in a memory. The single character requires only one byte of memory space whereas the single character in string form requires two bytes (one byte for the character and the other byte for the delimiter).
  • Each character of string occupies 1 byte of memory (on a 16-bit computer machine). The size of the character is machine-dependent and varies from 16-bit to 64-bit computers.
  • The characters of strings are stored in the contiguous (adjacent) memory locations.
  • The %s is used in the scanf() and printf() functions for input and output as a format specification for strings.With the scanf() function of string, the ‘&’ symbol is not necessary to attach during inputting the string data(such as scanf(“%s”,str1)).
  • Also, the string function gets() and puts() are used as string input and output (such as gets(str1) or puts(str1)).
  • The %c is used in the scanf() and printf() functions for inputting/displaying the string data, characters-wise one by one using a looping statement, similar to an array.
  • ARRAY OF STRINGS :
    • An array of strings is multiple strings, stored in the form of a table.
    • Declaring an array of strings is the same as strings, except it will have an additional dimension to store the number of strings.
    • Syntax : char StringName[size][size];
    • Example : char Str[3][4];
    • char Str[2][5] = {“Ram”,”Gita”,”Sohan” };
R a m \0
G i t a \0
S o h a n \0

Advantage String in C

  • An array of characters helps in the manipulation of  String contents.

Disadvantage String in C

  • C language does not provide the intrinsic string types.

Use/Applications String in C

String Functions in C

  • Most of the C compilers include string library functions that allow string comparison, string copy, concatenation of strings, etc.
  • The string functions operate on null-terminated arrays of characters and require the header <string.h>.
  • The header file <string.h> contains some string manipulation functions which are used as in-built string functions. Some important are –
    • Strlen() Function :
      • The strlen() function gives the length of a string.
      • It takes the string name as an argument.
      • Syntax : n = strlen (str);    where ‘str’ is the name of the string and ‘n’ is the length of the string in integer form, returned by strlen() function.
    • Strcpy() Function :
      • The strcpy() function is used to copy one string to another.
      • Syntax : strcpy(str1, str2);  where str1, str2 are two strings. Here, the content of string ‘str2’ is copied onto string ‘str1’.
    • Strncpy() function :
      • The strncpy function same as strcpy. It copies characters of one string to another string up to the given length(n).
      • Syntax : strncpy(str1, str2, 5);  where str1 and str2 are two strings. Here, only 5 characters of string str2 are copied onto string str1.
    • Strcmp() Function :
      • The Strcmp() function compares two strings, character by character.
      • Syntax :
      • It stops the comparison of two strings when there is a difference in the ASCII value or the end of any one string and returns the ASCII difference of the characters that is integer. If the return value zero means the two strings are equal, a negative value means that the first is less than the second, and a positive value means the first is greater than the second.
      • Syntax : n = strcmp(str1, str2);
        where ‘str1’ and ‘str2’ are two strings to be compared and ‘n’ is the returned value of different characters in integer form.
    • Stricmp() function :
      • The stricmp() function compares two strings ignoring the letter case (lower and upper case) of words.
      • Syntax : n = stricmp(str1, str2);
    • Strncmp() function :
      • The strncmp() function compares two strings up to a specified length.
      • Syntax : n = strncmp(str1, str2, 5);
      • where 5 characters of str1 and str2 are compared and n is returned value of differed characters. if n=0, characters are matched.
    • Strcat() Function :
      • The strcat() function is used to join one string to another.
      • It takes two strings as arguments; the characters of the second string will be appended to the first string.
      • Syntax : strcat(str1, str2);
        where str1 and str2 are two string arguments, string str2 is appended to string str1.
    • Strncat() function :
      • The strncat() function appends up to a specified length.
      • Syntax : strncat(str1, str2, 5);
        here first 5 characters of the str2 string are added into the str1 string.
    • Strlwr() Function :
      • The strlwr() function converts capitals/upper case characters of string to lower case characters.
      • Syntax : strlwr(str1);
        where str1 is the string to be converted into lowercase characters.
    • Strupr() Function :
      • The strupr() function converts small/lower case characters of the string to upper case characters.
      • Syntax : strupr(str1);
        where str1 is the string to be converted into upper-case characters.
    • Strrev() Function :
      • The strrev () function reverses the given string.
      • Syntax : strrev(str1);
        where string str1 is used for reversing the given text.
    • Strspn() Function :
      • The strspn() function gives the position of the string, from where the first string mismatches with second string.
      • Syntax : n = strspn (str1, str2);
        where str1 and str2 are two strings to be compared, n is the (integer) number of characters from where the first string does not match with the second string.
    • Strchr() function :
      • The strchr() function takes two arguments and returns the address of the first occurrence of the character in the given string.
      • Syntax : cp = strchr (str1, c);
        where str1 is the string c is the character and cp is the character pointer.
    • Strset() function :
      • The strset function replaces the string with the given character.
      • Syntax : strset (str1, ch);
        where string ‘str1’ will be replaced by character ‘ch’.
    • Strstr() function :
      • The strstr() function returns the address from where the second string starts in the first string.
      • Syntax : cp = strstr (str1, str2);
        where str1 and str2 are two strings, cp is a character pointer.
    • Sprintf() function :
      • The sprintf() stands for “string print.”
      • The sprintf() function does not print the string output on the console screen. It transfers the data to the buffer. It returns the total number of characters present in the string.

    Loading

    Categories: C

    0 Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.