MCS-011 Problem Solving and Programming

Admin | First year, Semester1

Chapters

Newsletter

Built-in Functions

The header file <string.h> in C includes various string manipulation functions. Some common functions are strlen, strcpy, strcmp, strcat, strlwr, and strrev.


strlen Function

The strlen function returns the length of a string. It takes the string name as an argument. The syntax for using strlen is:

n = strlen(str);

Here, str is the name of the string, and n is the length of the string returned by the strlen function.


strcpy Function

In C, you cannot simply assign one character array to another; you have to copy element by element. The string library <string.h> contains a function called strcpy for this purpose. The strcpy function is used to copy one string to another. The syntax is:

strcpy(str1, str2);

Here, str1 and str2 are two strings. The content of string str2 is copied onto string str1


strcmp Function

The strcmp function compares two strings character by character and stops comparison when there is a difference in the ASCII value or the end of any one string. It returns the ASCII difference of the characters as an integer. A return value of zero means the two strings are equal, a negative value means the first string is less than the second, and a positive value means the first string is greater than the second. The syntax is:

n = strcmp(str1, str2);
Here, str1 and str2 are the two strings to be compared, and n is the returned value of the differed characters.


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 are appended to the first string. The syntax is:

strcat(str1, str2);

Here, str1 and str2 are the two string arguments, and string str2 is appended to string str1.


strlwr Function

The strlwr function converts upper case characters of a string to lower case characters. The syntax is:

strlwr(str1);

Here, str1 is the string to be converted into lower case characters.


strrev Function

The strrev function reverses the given string. The syntax is:

strrev(str);

Here, the string str will be reversed.

Declaration of Strings

In C, strings are groups of characters enclosed in quotation marks, declared as character arrays, and terminated with a '\0' (Null character). A character uses one byte in memory, whereas a single character string requires two bytes. Strings are declared with the char data type followed by the array size in square brackets, e.g., char name[20];

Strings in C can be initialized using a character array, such as char name[8] = {'P', 'R', 'O', 'G', 'R', 'A', 'M', '\0'};. Each character occupies 1 byte of memory, though the size can vary with different computer architectures. Characters in a string are stored in contiguous memory locations. The C compiler automatically inserts a null character (\0) at the end of the string, making manual initialization of the null character optional.

You can set the initial value of a character array using a string literal. If the array is too small, the literal will be truncated; if the literal is smaller, the remaining characters are undefined. If no size is specified, the array size is set to the literal's length, including the null terminator.

Examples:

  • char str[4] = {'u', 'n', 'i', 'x'}; is valid but problematic because it's not null-terminated.
  • char str[5] = {'u', 'n', 'i', 'x', '\0'}; is correct and null-terminated.
  • char str[4] = "unix"; is problematic as it lacks space for the null-terminator.
  • char str[] = "UNIX"; is correct as the compiler sets the appropriate size and adds the null-terminator

  • String constants

    String constants (enclosed in double quotes) can be assigned to char pointers or copied to char arrays.

    Example:

    char *s = "hello";

    char s[100];

    strcpy(s, "hello");


    Differences in Behavior

    1. In char *s = "hello";, s points to the string constant in the string constant table, making it read-only.
    2. In char s[100]; strcpy(s, "hello");, the string is copied into the array s, making it modifiable.
    3. Direct assignment s = "hello"; is not allowed for arrays and will not compile.


    /* Fragment 1 */

    {

    char *s;
    s = "hello";
    printf("%s\n", s);
    // Output: hello
    }

    /* Fragment 2 */
    {
        
    char s[100];
        strcpy(s, "hello");
        
    printf("%s\n", s);
      
    // Output: hello

    }


    Display of strings using different formatting

    The printf function with the %s format is used to display strings. For example, printf("%s", name); displays the entire string stored in name.

    To specify the accuracy and width, such as displaying the first 5 characters within a field width of 15, use:

    printf("%15.5s", name);

    Including a minus sign in the format (e.g., %-10.5s) left-justifies the string:

    printf("%-10.5s", name);

    Array of Strings

    An array of strings in C is a table of multiple strings. Declaring it involves adding an additional dimension to specify the number of strings. The syntax is:

    char array-name[rows][cols];

    Example:

    char names[5][10];

    Here, names is the array, 5 indicates the number of strings, and 10 specifies the maximum length of each string.


    Another Example:

    An array of strings can be declared and initialized as follows:

    char names[3][10] = {"martin", "phil", "collins"};

    This creates a two-dimensional array with 3 strings, each with a maximum length of 10 characters. The representation in memory is:


    0123456789
    martin\0


    phil\0




    collins\0


    Built-in Functions

    The header file <string.h> in C includes various string manipulation functions. Some common functions are strlen, strcpy, strcmp, strcat, strlwr, and strrev.


    strlen Function

    The strlen function returns the length of a string. It takes the string name as an argument. The syntax for using strlen is:

    n = strlen(str);

    Here, str is the name of the string, and n is the length of the string returned by the strlen function.


    strcpy Function

    In C, you cannot simply assign one character array to another; you have to copy element by element. The string library <string.h> contains a function called strcpy for this purpose. The strcpy function is used to copy one string to another. The syntax is:

    strcpy(str1, str2);

    Here, str1 and str2 are two strings. The content of string str2 is copied onto string str1


    strcmp Function

    The strcmp function compares two strings character by character and stops comparison when there is a difference in the ASCII value or the end of any one string. It returns the ASCII difference of the characters as an integer. A return value of zero means the two strings are equal, a negative value means the first string is less than the second, and a positive value means the first string is greater than the second. The syntax is:

    n = strcmp(str1, str2);
    Here, str1 and str2 are the two strings to be compared, and n is the returned value of the differed characters.


    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 are appended to the first string. The syntax is:

    strcat(str1, str2);

    Here, str1 and str2 are the two string arguments, and string str2 is appended to string str1.


    strlwr Function

    The strlwr function converts upper case characters of a string to lower case characters. The syntax is:

    strlwr(str1);

    Here, str1 is the string to be converted into lower case characters.


    strrev Function

    The strrev function reverses the given string. The syntax is:

    strrev(str);

    Here, the string str will be reversed.

    Other String functions

    Some other string functions are:

    1. strncpy function: Copies characters from one string to another up to a specified length.
      Syntax: strncpy(str1, str2, 10);
    2. stricmp function: Compares two strings ignoring case differences.
      Syntax: n = stricmp(str1, str2);
    3. strncmp function: Compares two strings up to a specified length.
      Syntax: n = strncmp(str1, str2, 10);
    4. strchr function: Returns the address of the first occurrence of a specified character in a string.
      Syntax: cp = strchr(str, c);
    5. strset function: Replaces all characters in a string with a specified character.
      Syntax: strset(first, ch);
    6. strncat function: Appends up to a specified length of one string to another.
      Syntax: strncat(str1, str2, 10);
    7. strupr function: Converts lower case characters of a string to upper case.
      Syntax: strupr(str1);
    8. strstr function: Returns the address from where the second string starts in the first string.
      Syntax: cp = strstr(first, second);

    About John Doe

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

    Report an issue

    Related Posts

    3 Comments

    John Doe

    5 min ago

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

    Reply

    John Doe

    5 min ago

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

    Reply