C Programming: Array and String Manipulation
Master fundamental C programming techniques for array manipulation, including sorting, searching, insertion, deletion, and string reversal using stacks.
Core Principles
- Linear Search: Iterates through an array sequentially to find a target element.
- Insertion Sort: Builds the final sorted array one item at a time by inserting elements into their correct position.
- Bubble Sort: Repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order.
- Stack for String Reversal: Uses a stack's LIFO (Last-In, First-Out) property to reverse a string character by character.
Action Steps
- 1. Linear Search: Prompt for array size and elements, then for the key to search. Iterate, compare, and report if found with position (index + 1).
- 2. Insertion Sort (Descending): Prompt for array size and elements. For each element from the second, shift larger elements to the right to make space and insert.
- 3. Bubble Sort (Descending): Prompt for array size and elements. Repeatedly iterate, comparing adjacent elements and swapping if the left is smaller than the right.
- 4. String Reversal with Stack: Prompt for a string. Push each character onto a stack. Pop characters and print to display the reversed string.
- 5. Array Element Deletion: Prompt for array size and elements. Prompt for the number to delete. Find the number, shift subsequent elements left, and decrement size.
- 6. Array Element Insertion: Prompt for array size and elements. Prompt for position and number. Shift elements from the position to the right, insert the number, and increment size.
Key Terms
- Stack: A data structure following LIFO principle; used here for string reversal.
- Linear Search: A simple search algorithm that checks each element sequentially.
- Insertion Sort: A sorting algorithm that builds a sorted array by inserting elements one by one.
- Bubble Sort: A sorting algorithm that repeatedly swaps adjacent elements if they are in the wrong order.
Real World Examples
- Reversing a string 'hello' using a stack.: Push 'h', 'e', 'l', 'l', 'o'. Pop and print: 'o', 'l', 'l', 'e', 'h'.
- Searching for '16' in the array {11, 12, 15, 16, 17}.: Linear search finds '16' at position 4 (index 3).
Related cheat sheets
More like this