Definition

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

Syntax

  • 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 null character.

Feature/Characteristics

  • The end of the string is marked with a special character, called Null Character( ‘\0’ ), which is backslash symbol with zero numeric value. 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 memory space whereas the single character  in string form requires two bytes (one byte for the character and other byte for the delimiter).
  • Each character of string occupies 1 byte of memory (on 16 bit computer machine). The size of character is machine dependent, and varies from 16 bit – 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, ‘&’ 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 used in the scanf() and printf() functions for inputting/displaying the string data, character wise one by one using looping statement, similar as an array.
  • ARRAY OF STRINGS :
    • Array of strings are multiple strings, stored in the form of table.
    • Declaring array of strings is same as strings, except it will have 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

  • Array of character helps in the manipulation of  String contents.

Disadvantage

  • C language does not provide the intrinsic string types.

Use/Applications

String Functions

  • 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 argument.
      • Syntax : n = strlen (str);    where ‘str’ is 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 on to 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 comparison of two string when there is a difference in the ASCII value or the end of any one string and returns ASCII difference of the characters that is integer.If the return value zero means the two strings are equal, a negative value means that first is less than second, and a positive value means first is greater than second.
      • Syntax : n = strcmp(str1, str2);
        where ‘str1’ and ‘str2’ are two strings to be compared and ‘n’ is returned value of differed 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 specified length.
      • Syntax : strncat(str1, str2, 5);
        here first 5 character of the str2 string is added into str1 string.
    • Strlwr() Function :
      • The strlwr() function converts capitals/upper case characters of string to lower case characters.
      • Syntax : strlwr(str1);
        where str1 is string to be converted into lower case characters.
    • Strupr() Function :
      • The strupr() function converts small/lower case characters of string to upper case characters.
      • Syntax : strupr(str1);
        where str1 is string to be converted into upper case characters.
    • Strrev() Function :
      • The strrev () funtion 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 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 character from where first string does not match with second string.
    • Strchr() function :
      • The strchr() function takes two arguments and returns the address of first occurrence of the character in the given string.
      • Syntax : cp = strchr (str1, c);
        where str1 is string and c is character and cp is character pointer.
    • Strset() function :
      • The strset funtion 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 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.

     600 total views,  2 views today

    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.