MCS-201 · Unit 4

Arrays and Strings

Section 9 of 9

Block 1 · Unit 4Arrays and Strings8 min · Section 9 of 9

4.9Non-Standard String Functions and Writing Your Own

Covers IGNOU MCS-201 §4.11.5, 4.11.6, 4.11.8

The study material lists several string functions that are not part of standard C. They existed in Turbo C and still appear in exam questions, so you should know what they do. Because they are missing on most modern systems, you should also be able to write them yourself, which is a favourite examination task.

Which functions are non-standard

FunctionWhat it doesStandard C?Portable replacement
strlwr(s)Converts s to lower caseNoLoop with tolower
strupr(s)Converts s to upper caseNoLoop with toupper
strrev(s)Reverses s in placeNoSwap ends in a loop
strset(s, c)Sets every character of s to cNoLoop assigning c
stricmp(a, b)Compares ignoring caseNoPOSIX strcasecmp, or a loop
strdup(s)Returns a newly allocated copyPOSIX; added to ISO C in C23malloc and strcpy

Character functions from ctype.h

<ctype.h> is standard and works on single characters:

FunctionReturns
toupper(c)The upper-case form of c if it is a lower-case letter; otherwise c unchanged
tolower(c)The lower-case form of c if it is an upper-case letter; otherwise c
isalpha(c), isdigit(c), isspace(c)Non-zero if c is a letter, a digit, or whitespace

Checking for a palindrome

A palindrome reads the same forwards and backwards, such as “madam”. The check uses the same two-index walk as reversal: compare s[i] with s[j] from both ends; if any pair differs, it is not a palindrome.

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

int main(void)
{
    char word[] = "madam";
    int i = 0, j = (int)strlen(word) - 1;
    int is_pal = 1;

    while (i < j) {
        if (word[i] != word[j]) {
            is_pal = 0;
            break;
        }
        i++;
        j--;
    }
    printf("%s is %sa palindrome\n", word, is_pal ? "" : "not ");
    return 0;
}
Output
madam is a palindrome

Key takeaways

  • strlwr, strupr, strrev, strset and stricmp are Turbo C / Microsoft extensions, not ISO C.
  • <ctype.h> provides standard toupper, tolower, isalpha, isdigit.
  • Length, copy, concatenation, case conversion and reversal are all loops that stop at '\0'.
  • Always store the final '\0' when building a string.

Practice questions

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

Question 1 · multiple choice

Which of these functions is part of standard C?

Show answer

Answer: C. strchr

strchr is defined by ISO C in <string.h>. The other three are Turbo C / Microsoft extensions.

Question 2 · multiple choice

Which header declares toupper and tolower?

Show answer

Answer: D. <ctype.h>

Character classification and conversion functions live in <ctype.h>; <string.h> works on whole strings.

Question 3 · multiple choice

What does toupper('7') return?

Show answer

Answer: A. ‘7’

toupper changes only lower-case letters; any other character is returned unchanged.

Question 4 · multiple choice

In the reversal function, why does the loop run while i < j?

Show answer

Answer: B. Each swap fixes two characters, so the indices need only meet in the middle

Swapping from both ends finishes when the indices meet; continuing past the middle would swap the characters back to their original places.

Question 5 · multiple choice

What does this program print?

C · count_chars.c
#include <stdio.h>

int main(void)
{
    char s[] = "hello";
    int j = 0;
    while (s[j] != '\0')
        j++;
    printf("%d\n", j);
    return 0;
}
Show answer

Answer: C. 5

The loop counts characters until it reaches '\0', which is not counted, so the result is 5, the same as strlen.

Question 6 · short answer

Write a function my_strcpy(char dest[], const char src[]) without using library functions, and trace it for the source “Hi”.

Show worked answer

void my_strcpy(char dest[], const char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }. Trace for “Hi”: i = 0, ‘H’ is not '\0', so dest[0] = ‘H’, i = 1; ‘i’ is not '\0', so dest[1] = ‘i’, i = 2; src[2] is '\0', so the loop ends and dest[2] = ‘\0’ is stored. dest now holds ‘H’, ‘i’, ‘\0’, a valid copy of the string.

↑ ↓ move↵ openTab switch type