Programming Language & Its Applications — Introduction to C Programming, NEC licence examination syllabus (Nepal Engineering Council).
C has no string type — only char arrays ending in '\0', and that single design choice explains every string bug in C.
In C a "string" is a char array whose last meaningful byte is '\0' (the null character, value 0). Every library function finds the end by scanning for that byte. So the length is not stored — it is computed — and if the terminator is missing, functions run off the end of your buffer. Understanding this makes both <string.h> and its dangers obvious.
strlen is O(n) — so for(i=0;i<strlen(s);i++) is accidentally O(n²), a real performance bug found in production code constantly. And because every function trusts the terminator, a missing '\0' reads off the end of your buffer: that is the Heartbleed class of vulnerability. Search "why Pascal strings store length" to see the design C rejected and what it cost.#include <stdio.h>
#include <string.h>
int main(void) {
char s[] = "HELLO";
printf("sizeof = %zu\n", sizeof s); /* 6 */
printf("strlen = %zu\n", strlen(s)); /* 5 */
for (size_t i = 0; i <= strlen(s); i++)
printf("[%zu] '%c' = %d\n", i,
s[i] ? s[i] : ' ', s[i]);
/* truncating a string: just move the terminator */
s[3] = '\0';
printf("after s[3]='\\0': \"%s\" len=%zu\n", s, strlen(s));
return 0;
}
Output:
sizeof = 6
strlen = 5
[0] 'H' = 72
[1] 'E' = 69
[2] 'L' = 76
[3] 'L' = 76
[4] 'O' = 79
[5] ' ' = 0
after s[3]='\0': "HEL" len=3
#include <stdio.h>
#include <string.h>
int main(void) {
char a[40] = "Nepal";
char b[] = " Engineering";
strcat(a, b);
printf("cat: %s\n", a);
char c[40];
strcpy(c, a);
printf("copy: %s\n", c);
/* strcmp compares byte by byte, ASCII order */
printf("cmp(\"apple\",\"banana\") = %d\n",
strcmp("apple", "banana"));
printf("cmp(\"banana\",\"apple\") = %d\n",
strcmp("banana", "apple"));
printf("cmp(\"same\",\"same\") = %d\n",
strcmp("same", "same"));
char *p = strstr(a, "Engine");
if (p) printf("found at index %ld\n", p - a);
return 0;
}
Output:
cat: Nepal Engineering
copy: Nepal Engineering
cmp("apple","banana") = -1
cmp("banana","apple") = 1
cmp("same","same") = 0
found at index 6
#include <stdio.h>
size_t my_strlen(const char *s) {
size_t n = 0;
while (s[n] != '\0') n++; /* scan for the terminator */
return n;
}
void my_strcpy(char *d, const char *s) {
size_t i = 0;
while (s[i] != '\0') { d[i] = s[i]; i++; }
d[i] = '\0'; /* MUST copy the terminator */
}
void reverse(char *s) {
size_t i = 0, j = my_strlen(s) - 1;
while (i < j) { char t=s[i]; s[i]=s[j]; s[j]=t; i++; j--; }
}
int is_palindrome(const char *s) {
size_t i = 0, j = my_strlen(s) - 1;
while (i < j) { if (s[i] != s[j]) return 0; i++; j--; }
return 1;
}
int main(void) {
char s[40] = "PROGRAM";
printf("len=%zu\n", my_strlen(s));
char d[40];
my_strcpy(d, s);
printf("copy=%s\n", d);
reverse(s);
printf("reversed=%s\n", s);
printf("\"madam\" palindrome? %d\n", is_palindrome("madam"));
printf("\"nepal\" palindrome? %d\n", is_palindrome("nepal"));
return 0;
}
Output:
len=7
copy=PROGRAM
reversed=MARGORP
"madam" palindrome? 1
"nepal" palindrome? 0
#include <stdio.h>
#include <string.h>
int main(void) {
char small[6];
/* "Engineering" is 11 chars + '\0' = 12 bytes
into a 6-byte buffer: 6 bytes written PAST the end */
/* strcpy(small, "Engineering"); <-- OVERFLOW */
/* the safe version - bounded, and terminate manually */
strncpy(small, "Engineering", sizeof small - 1);
small[sizeof small - 1] = '\0';
printf("truncated: \"%s\"\n", small);
/* safest for user input: fgets, which bounds itself */
char line[64];
printf("Enter a line: ");
if (fgets(line, sizeof line, stdin)) {
line[strcspn(line, "\n")] = '\0'; /* strip newline */
printf("got %zu chars: %s\n", strlen(line), line);
}
return 0;
}
Output:
truncated: "Engin"
Enter a line: hello world
got 11 chars: hello world
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
char text[] = "Nepal Engineering Council exam 2026";
int words = 0, vowels = 0, digits = 0, upper = 0;
for (int i = 0; text[i]; i++) {
char c = tolower(text[i]);
if (strchr("aeiou", c) && c) vowels++;
if (isdigit((unsigned char)text[i])) digits++;
if (isupper((unsigned char)text[i])) upper++;
}
/* strtok MODIFIES the string, inserting '\0' at
each delimiter - so work on a copy if you need
the original afterwards */
char copy[80];
strcpy(copy, text);
for (char *t = strtok(copy, " "); t; t = strtok(NULL, " ")) {
printf("word %d: %s\n", ++words, t);
}
printf("words=%d vowels=%d digits=%d upper=%d\n",
words, vowels, digits, upper);
return 0;
}
Output:
word 1: Nepal
word 2: Engineering
word 3: Council
word 4: exam
word 5: 2026
words=5 vowels=12 digits=4 upper=4
sizeof vs strlen (differ by 1) is asked every year. Be able to hand-implement strlen, strcpy, strcmp, reverse and palindrome check — these are standard programming questions. Know that strcmp returns the byte difference, that == compares addresses not contents, and that strcpy/gets cause buffer overflows (gets was removed from C11).Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.
Loading…