DSA Strings
Strings Complete Guide in Data Structures
A string is a chain of characters placed one after another. It’s often used to store words, sentences, or any text-based information. In programming, a string acts like a text container, where each letter or symbol has its own position.
Definition
A string is a group of characters like letters, numbers, or symbols, joined together and treated as one single piece of data.
How Strings Work in Memory:
In most languages like C, strings are stored as an array of characters ending with a special character called the null terminator (\0). This special symbol tells the system where the string ends.
Each character in a string sits next to the other in memory, and you can access them just like array elements using their index.
Key Features
- Made up of characters (e.g., 'a', 'b', '1', '#').
- Stored in sequence, like an array.
- Ends with \0 in C-style strings.
- Index starts from 0 for the first character.
Real-Life Analogy:
Imagine alphabet blocks placed in a line to spell a word. Each block is a character, and the whole line of blocks is the string. You can count each block by its position.
Example
#include <stdio.h>
int main() {
char name[] = "Parmi";
// Print each character using a loop
for(int i = 0; name[i] != '\0'; i++) {
printf("Character at index %d is %c\n", i, name[i]);
}
return 0;
} Output
Character at index 0 is P Character at index 1 is a Character at index 2 is r Character at index 3 is m Character at index 4 is i
String Operations
| Operation | Purpose |
|---|---|
| Length Check | Find how many characters are in the string |
| Copy | Duplicate one string into another |
| Compare | See if two strings are the same or different |
| Concatenate | Join two strings into one |
| Reverse | Flip the string backward |
| Search | Look for a letter or word inside the string |
Functions in C for Strings (from )
| Function | Description |
|---|---|
| strlen() | Gives the length of the string |
| strcpy() | Copies one string to another |
| strcmp() | Compares two strings |
| strcat() | Adds one string at the end of another |
| strchr() | Finds a character in a string |
Advantages of Strings
- Great for handling human-readable data.
- Easy to perform operations like searching or formatting.
- Many built-in tools and functions to work with them.
Limitations
- In C, you must manually handle the null terminator.
- Fixed size unless using dynamic memory.
- Slower than numbers when performing some operations.
When to Use Strings
- To store names, messages, file paths, etc.
- When you need to read or manipulate user text input.
- When you're dealing with alphabets, symbols, or digits in text form.
Bonus – 2 Ways to Declare Strings in C
1. Character Array
char city[10] = "Mumbai";
2. Pointer Method
char *city = "Mumbai";
Both represent strings, but the first lets you modify the characters (if not const), while the second is read-only in many cases.
Prefer Learning by Watching?
Watch these YouTube tutorials to understand DATA STRUCTURES ALGORITHMS Tutorial visually:
What You'll Learn:
- 📌 The String Data Structure in 4 Minutes
- 📌 String in Data Structure