Reverse a string in C
Channel: Solve Computer Science
Date: 2023-10-07

In this video I show you different algorithms for reversing a string and their implementation in C.

Generally there are two possibilities in this case:

- create a new array to store the result
- reverse the string in-place

We'll see in detail how the two algorithms work and some caveats specific to the C programming language.

Links

Blog post and source code: https://blog.franco.net.eu.org/post/reverse-a-string-in-c.html

CHAPTERS

0:00 Intro
0:18 First algorithm: use indices `i` and `j` to control the iterations of the two arrays.
1:37 C implementation of the first solution: use of `strlen` and the for loop
3:08 Caveat of the `strlen` function
3:44 Improving the first solution: coherent data types
4:12 Changing the for loop condition
5:18 Underflow using `size_t` and the original condition
6:29 A solution without underflow using `size_t` variables
7:35 The second solution: a different way to count the length of the string. Use the terminator character
8:52 The third solution: move to last element and then move backwards instead of counting the length of the string
11:05 Fourth solution: swap the elements in the original array to reverse it. This is an in-place algorithm
15:06 Outro

#programming #cprogramming #strings #solvecomputerscience