MCS-201 · Unit 4

Arrays and Strings

Section 8 of 9

Block 1 · Unit 4Arrays and Strings7 min · Section 8 of 9

4.8Standard String Functions

Covers IGNOU MCS-201 §4.11, 4.11.1, 4.11.2, 4.11.3, 4.11.4, 4.11.7, 4.11.8

Because strings are arrays, C cannot copy or compare them with = and ==. The standard library header <string.h> supplies functions for these jobs. This section covers the functions that belong to standard C; the next covers the non-standard ones mentioned in the study material.

The four core functions

FunctionPurposeReturns
strlen(s)Number of characters before '\0'A size_t (print with %zu)
strcpy(dest, src)Copies src, including '\0', into destdest
strcat(dest, src)Appends src to the end of destdest
strcmp(s1, s2)Compares two strings character by characterNegative, zero or positive

strlen does not count the '\0': strlen("TYRAN") is 5, although the string occupies 6 bytes. For strcpy and strcat, the destination array must be large enough for the result plus '\0'; the functions do not check.

How strcmp compares

Comparison of this kind is called lexicographic order. Because it uses character codes, all capital letters come before all small letters in ASCII (‘Z’ is 90, ‘a’ is 97), so strcmp("Zebra", "apple") is negative.

strspn and the other standard functions

strspn(s1, s2) returns the length of the initial part of s1 that consists only of characters found anywhere in s2. s2 is treated as a set of allowed characters, not as a string to match position by position.

FunctionPurposeExampleResult
strncpy(d, s, n)Copies at most n charactersstrncpy(d, "HELLO", 3)d gets H, E, L (no '\0' added)
strncat(d, s, n)Appends at most n characters, then '\0'strncat(d, "WORLD", 2)Appends “WO”
strncmp(a, b, n)Compares at most n charactersstrncmp("apple", "apply", 4)0
strchr(s, c)Address of the first c in s, or NULLstrchr("programming", 'g')Points to “gramming”
strstr(s, t)Address of the first t in s, or NULLstrstr("programming", "ram")Points to “ramming”

Key takeaways

  • Include <string.h> for the string functions.
  • strlen excludes '\0'; strcpy copies; strcat appends without adding a space.
  • strcmp returns 0 for equal, negative if the first is smaller, positive if greater.
  • strspn counts leading characters of the first string that belong to the set in the second.
  • Read input with fgets, never gets.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What does strlen("C Language") return?

Show answer

Answer: C. 10

The nine letters plus the space make 10 characters; the terminating '\0' is not counted.

Question 2 · multiple choice

What is the sign of strcmp("APPLE", "APPLY")?

Show answer

Answer: A. Negative

The strings first differ at the fifth character, where ‘E’ (69) is less than ‘Y’ (89), so the first string is smaller and the result is negative.

Question 3 · multiple choice

After char a[20] = "Data"; strcat(a, "Base");, what does a contain?

Show answer

Answer: D. DataBase

strcat appends directly after the last character, with no space added.

Question 4 · multiple choice

What does strspn("12345abc", "0123456789") return?

Show answer

Answer: C. 5

The first five characters are digits and belong to the set; ‘a’ is the first character not in the set, so the length is 5.

Question 5 · multiple choice

What does this program print?

C · cmp_case.c
#include <stdio.h>
#include <string.h>

int main(void)
{
    if (strcmp("delhi", "Delhi") == 0)
        printf("same\n");
    else
        printf("different\n");
    return 0;
}
Show answer

Answer: B. different

strcmp is case-sensitive: ‘d’ (100) and ‘D’ (68) have different codes, so the result is not zero.

Question 6 · short answer

Write a fragment that reads a password with fgets and prints “Access granted” only if it equals “mca2026”. Explain the condition used.

Show worked answer

char pw[30]; fgets(pw, sizeof pw, stdin); pw[strcspn(pw, "\n")] = '\0'; if (strcmp(pw, "mca2026") == 0) printf("Access granted\n"); else printf("Access denied\n");. fgets reads the line safely, and the newline is removed so that it does not take part in the comparison. strcmp returns 0 only when every character matches and both strings end together, so == 0 is the test for equality; using pw == "mca2026" would compare addresses, not contents.

↑ ↓ move↵ openTab switch type