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
Function
What it does
Standard C?
Portable replacement
strlwr(s)
Converts s to lower case
No
Loop with tolower
strupr(s)
Converts s to upper case
No
Loop with toupper
strrev(s)
Reverses s in place
No
Swap ends in a loop
strset(s, c)
Sets every character of s to c
No
Loop assigning c
stricmp(a, b)
Compares ignoring case
No
POSIX strcasecmp, or a loop
strdup(s)
Returns a newly allocated copy
POSIX; added to ISO C in C23
malloc and strcpy
Character functions from ctype.h
<ctype.h> is standard and works on single characters:
Function
Returns
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 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.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/arrays-and-strings/non-standard-string-functions-and-writing-your-own/ to read this page.