
This is a very simple C program to reverse all the characters in a given string. For example, considering the string “hello world”, the program should output “dlrow olleh”. The string reversal problem is a common embedded software interview question asked for both entry-level and mid-level firmware engineering positions.
#include <stdio.h>void strRev(char *str){char temp;char *begin = str;char *end = str;if (!str || !(*str))goto error;// Go to the end of the stringwhile (*(++end));// Discard the null characterend--;while (begin < end){// Swaptemp = *end;*end = *begin;*begin = temp;// Move the pointersbegin++;end--;}error:return;}int main(void){char str[] = "hello world";puts(str);strRev(str);puts(str);return 0;}
String manipulation is a cornerstone of embedded C programming, especially when developing interfaces for human-machine interaction, parsing sensor telemetry strings, or processing NMEA sentences from a GPS module. While high-level languages offer built-in string reversal functions, embedded C requires direct pointer manipulation, offering developers absolute control over memory allocation and execution cycles.
Reversing a string is also one of the most frequently asked C programming interview questions for embedded roles. The interviewer typically looks for a candidate’s understanding of null-terminated strings (\0), pointer arithmetic, array indexing, and how to swap elements in place without allocating extra memory on the heap (avoiding malloc() which can cause heap fragmentation in constrained systems).
This C program accepts a string from the user, reverses the order of the characters in place using a simple loop, and then displays the reversed string.mmonly. In this given example, the function strRev() reverses the characters present in the character array.
The character pointers begin and end are assigned with the address of the received string str.
char *begin = str;char *end = str;
The lines,
if (!str || !(*str))goto error;
checks for a null pointer or for a null value and jumps to error: label.
A character pointer end is moved to the last character (excluding the null character) in the received string in the following lines,
// Go to the end of the stringwhile (*(++end));// Discard the null characterend--;
Finally the begin and end pointers’ dereferenced characters are swapped until all characters are swapped.
// Swap all characterswhile (begin < end){// Swaptemp = *end;*end = *begin;*begin = temp;// Move the pointersbegin++;end--;}
Now the string characters are reversed and the new string is printed from main().
Quick Links
Legal Stuff





