Topic 2- Strings
Tags: #◇CCPROG2 #◇C Programming #◇Programming #◇Computer Science #◇Data Types #◇Data Structures #◇Structured Data #◇Structured Programming #◇Memory Management
◇Overview
In C, strings are null-terminated arrays of characters. There is no dedicated
stringtype — strings are represented aschararrays ending with'\0'.
◇Key Concepts
A string is a char array terminated by '\0' String literals (e.g., "Hello") are stored in read-only memory The <string.h> library provides essential string functions scanf("%s", ...) stops at whitespace; fgets() reads full lines
◇Details / Theory
String Declaration and Initialization
char str1[6] = "Hello"; // Size 6: 'H','e','l','l','o','\0'
char str2[] = "Hello"; // Compiler infers size = 6
char str3[6] = {'H','e','l','l','o','\0'}; // Character-by-character
char *str4 = "Hello"; // Pointer to string literal (read-only)
String Input/Output
char name[50];
// scanf — stops at whitespace
scanf("%s", name);
// fgets — reads entire line (safer, includes newline)
fgets(name, sizeof(name), stdin);
// printf
printf("Name: %s\n", name);
// puts — prints string + newline
puts(name);
Common <string.h> Functions
| Function | Purpose | Example |
|---|---|---|
strlen(s) | Length (excluding '\0') | strlen("Hi") → 2 |
strcpy(dest, src) | Copy src to dest | strcpy(dest, "Hi") |
strncpy(dest, src, n) | Copy at most n chars | Safer than strcpy |
strcat(dest, src) | Append src to dest | strcat(buf, " World") |
strncat(dest, src, n) | Append at most n chars | Safer than strcat |
strcmp(a, b) | Compare: 0 if equal | strcmp("ab", "ab") → 0 |
strncmp(a, b, n) | Compare first n chars | Partial comparison |
strchr(s, c) | Find first occurrence of c | Returns pointer or NULL |
strstr(s, sub) | Find substring | Returns pointer or NULL |
String Comparison
char a[] = "apple";
char b[] = "banana";
if (strcmp(a, b) < 0)
printf("%s comes before %s\n", a, b);
// Cannot use == to compare strings; that compares addresses
String Manipulation Example
#include <stdio.h>
#include <string.h>
int main() {
char first[20] = "John";
char last[20] = "Doe";
char full[50];
strcpy(full, first); // full = "John"
strcat(full, " "); // full = "John "
strcat(full, last); // full = "John Doe"
printf("Full name: %s (length: %lu)\n", full, strlen(full));
return 0;
}
Character Functions (<ctype.h>)
| Function | Description |
|---|---|
isalpha(c) | Is letter? |
isdigit(c) | Is digit? |
isalnum(c) | Is letter or digit? |
toupper(c) | Uppercase version |
tolower(c) | Lowercase version |
isspace(c) | Is whitespace? |
◇Common Pitfalls
Using == to compare strings instead of strcmp(). Buffer overflow: writing more characters than array size allows. Forgetting the null terminator when building strings manually. scanf("%s", ...) is unsafe — no bounds checking. Prefer fgets(). Modifying a string literal via pointer (char *s = "abc"; s[0] = 'x'; → undefined behavior).
◇Summary
TL;DR — C strings are null-terminated
chararrays. Use<string.h>functions (strlen,strcpy,strcmp,strcat) for manipulation. Always ensure buffers are large enough and terminated with'\0'. Never compare strings with==.
◇Links
Related: ◇What is an Array — ◇Declaration — ◇Accessing Elements — ◇Structs — ◇File Handling Source / Reference:
◇References & Navigation
- Previous Topic: ◇Advantages and Disadvantages
- Next Topic: ◇Structs
- Related Notes: ◇One-Dimensional Arrays (1D Arrays), ◇Data Representation - Characters and Boolean
ACTIVE RECALL VERIFICATION
Select an answer to verify conceptual comprehension and reveal the technical rationale.
How are strings fundamentally represented in C?
What happens when you initialize a string as char str[] = "Hello";?
Which function from <string.h> is used to find the length of a string excluding the null terminator?
Why is using scanf("%s", name) generally considered unsafe or problematic?
Which function is recommended over scanf for reading full lines of string input safely?
What is the correct way to compare two strings in C?
If strcmp(a, b) returns a value of 0, what does it mean?
What is the primary difference between strcpy and strncpy?
Which header file contains functions like isalpha and isdigit for character manipulation?
What undefined behavior occurs if you attempt to modify a string literal like char *s = "abc"; s[0] = 'x';?