PSUC Lab 9 — Null-Terminated Arrays
Active Logic: Strings & Null Terminator '\0'
Master C string manipulation: ASCII offset conversion, last character occurrence search, string.h library functions (strlen, strcpy, strcat), word count, string reversal, and palindrome checks.
C String Memory Rules
| Concept | Rule / Formula | Key Trap / Pitfall |
|---|---|---|
| Null Terminator | str[n] == '\0' (ASCII 0) | Omitting '\0' causes memory overrun! |
| Case Conversion | 'a' - 'A' = 32 | c - 32 converts lowercase to uppercase |
| Word Count | Count spaces + 1 | Must ignore leading/consecutive spaces! |
| Palindrome | str[i] == str[len - 1 - i] | Compare 2 pointers moving inward! |
🎯 Learning Philosophy: Active Logic Building
Test your string logic across 3 Interactive Learning Modes:
- 🧠 Tab 1 (Logic & Hints): String memory & null terminator rules.
- 🧩 Tab 2 (Logic Puzzle): Fill in missing ASCII offsets/indices & test live text tools!
- 💻 Tab 3 (C Solution): Reference verified C code solutions.
Solve string logic puzzles before inspecting C source code!
Exercise 1 of 5
Convert Lower-case Letters to Upper-case
Converts all lower-case characters in a string to upper-case using ASCII offset calculation (c - 32).
ASCII Table Offset
'a'= ASCII 97 |'A'= ASCII 65- Offset = 97 - 65 = 32
- Formula:
if (c >= 'a' && c <= 'z') c = c - 32;
Interactive Logic Hints
💡 Hint 1: What does toupper() in do internally? ▼
It checks if character is lowercase and subtracts 32 automatically!
🧩 Complete Lowercase to Uppercase Loop
while (str[i] != ) {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = str[i] ;
}
i++;
}
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = str[i] ;
}
i++;
}
Select statements above and click Check!
🔤 Uppercase Converter Simulator
Converted text...
lower_to_upper.cReference C Code
#include <stdio.h>
int main() {
char str[] = "welcome to psuc visual lab";
int i = 0;
while (str[i] != '\0') {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = str[i] - 32;
}
i++;
}
printf("Uppercase: %s\n", str);
return 0;
}
Exercise 2 of 5
Find Last Occurrence of a Character
Traverses string to find the last index position where character key appears.
Reverse Search Optimization
- Method 1: Forward scan & overwrite
lastIdx = iwhen match found. - Method 2: Backward scan from
strlen(str) - 1down to 0 &breakon first match!
Interactive Logic Hints
💡 Hint 1: Which method is faster for finding LAST occurrence? ▼
Backward scan is faster! It finds the last occurrence immediately and breaks!
🧩 Complete Backward Character Search Loop
int len = strlen(str), lastIdx = -1;
for (i = len - 1; i 0; i--) {
if (str[i] key) {
lastIdx = i; ;
}
}
for (i = len - 1; i 0; i--) {
if (str[i] key) {
lastIdx = i; ;
}
}
Select statements above and click Check!
🔍 Last Occurrence Search Visualizer
Search status...
last_char_search.cReference C Code
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "programming in c language";
char key = 'g';
int i, len = strlen(str), lastIdx = -1;
for (i = len - 1; i >= 0; i--) {
if (str[i] == key) {
lastIdx = i;
break;
}
}
if (lastIdx != -1) printf("Last occurrence of '%c' is at index %d\n", key, lastIdx);
else printf("Character '%c' NOT found.\n", key);
return 0;
}
Exercise 3 of 5
Count Number of Words in a Sentence
Counts total words by scanning spaces and non-space word transitions.
Word Counting State Machine
- Track state:
inWord = false - If char is non-space AND
!inWord→words++; inWord = true; - If char is space →
inWord = false;
Interactive Logic Hints
💡 Hint 1: Why not just count spaces + 1? ▼
Counting spaces fails if user types multiple consecutive spaces ("hello world")! State tracking handles extra spaces correctly!
🧩 Complete Word Count Logic
int words = 0; bool inWord = false;
while (str[i] != '\0') {
if (str[i] != ' ' && str[i] != '\t' && str[i] != '\n') {
if (!inWord) { words; inWord = ; }
} else {
inWord = false;
}
i++;
}
while (str[i] != '\0') {
if (str[i] != ' ' && str[i] != '\t' && str[i] != '\n') {
if (!inWord) { words; inWord = ; }
} else {
inWord = false;
}
i++;
}
Select statements above and click Check!
📝 Word Counter Visualizer
Word count...
word_count.cReference C Code
#include <stdio.h>
#include <stdbool.h>
int main() {
char str[] = "C programming is essential for computer engineering";
int i = 0, words = 0;
bool inWord = false;
while (str[i] != '\0') {
if (str[i] != ' ' && str[i] != '\t' && str[i] != '\n') {
if (!inWord) { words++; inWord = true; }
} else {
inWord = false;
}
i++;
}
printf("Total words = %d\n", words);
return 0;
}
Exercise 4 of 5
String Reversal & Palindrome Check
Reverses string using two-pointer swap and checks if str[i] == str[len - 1 - i] for all indices.
Two-Pointer In-Place Swap
- Pointers:
left = 0, right = len - 1 - Swap:
temp = str[left]; str[left] = str[right]; str[right] = temp; - Increment/Decrement:
left++; right--;
Interactive Logic Hints
💡 Hint 1: What are famous string palindromes? ▼
"madam", "racecar", "level", "radar"!
🧩 Complete Two-Pointer Reversal Loop
int left = 0, right = len - 1;
while (left right) {
char temp = str[left];
str[left] = str[];
str[right] = temp;
left++; right--;
}
while (left right) {
char temp = str[left];
str[left] = str[];
str[right] = temp;
left++; right--;
}
Select statements above and click Check!
🔄 String Reversal & Palindrome Engine
Reversal status...
string_reverse.cReference C Code
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
int main() {
char str[] = "racecar";
int i, len = strlen(str);
bool isPal = true;
for (i = 0; i < len / 2; i++) {
if (str[i] != str[len - 1 - i]) {
isPal = false;
break;
}
}
printf("String '%s' IS %sa Palindrome!\n", str, isPal ? "" : "NOT ");
return 0;
}
Exercise 5 of 5
Manual Strlen & Substring Extraction
Computes string length without library functions and extracts sub-string from index start for length characters.
Manual Strlen & Substring Rules
- Manual Strlen:
while (str[len] != '\0') len++; - Substring Extraction: Copy
sub[i] = str[start + i]fori = 0 to subLen - 1, then appendsub[subLen] = '\0'!
Interactive Logic Hints
💡 Hint 1: What happens if you forget sub[subLen] = '\0'? ▼
The extracted substring will print garbage characters! Always null-terminate new strings!
🧩 Complete Substring Extraction Loop
for (i = 0; i < subLen; i++) {
sub[i] = str[start i];
}
sub[subLen] = ;
sub[i] = str[start i];
}
sub[subLen] = ;
Select statements above and click Check!
✂️ Substring Extractor Tool
Extracted Substring...
substring_manual.cReference C Code
#include <stdio.h>
int main() {
char str[] = "Manipal University Jaipur";
char sub[50];
int start = 8, length = 10, i;
for (i = 0; i < length; i++) {
sub[i] = str[start + i];
}
sub[length] = '\0';
printf("Extracted Substring: '%s'\n", sub);
return 0;
}
Knowledge Check
Test Your String Logic Mastery
Question 1 of 5
Loading question...