The error is here:

while (*sPtr != '\0') {
    ++length;
    ++sPtr;
}

At this point the sPtr point at the end of the string so in the second loop it never decrement.

for (int i = length; i >= 0; --i) {
    printf("%c", *(sPtr+1));
}

A possible solution can be this:

for (int i = length; i >= 0; --i) {
    printf("%c", *(sPtr));
    --sPtr;
}
Answer from Zig Razor on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › c language › program-to-reverse-a-string-using-pointers
Program to Reverse a String using Pointers - GeeksforGeeks
August 7, 2024 - #include <bits/stdc++.h> using namespace std; void reverseString(char* str) { int l, i; char *begin_ptr, *end_ptr, ch; // Get the length of the string l = strlen(str); // Set the begin_ptr // initially to start of string begin_ptr = str; // Setting end_ptr initially to // the end of the string end_ptr = str + l - 1; // Swap the char from start and end // index using begin_ptr and end_ptr for (i = 0; i < l / 2; i++) { // swap character ch = *end_ptr; *end_ptr = *begin_ptr; *begin_ptr = ch; // update pointers positions begin_ptr++; end_ptr--; } } int main() { // Define a string char str[100] = "GeeksforGeeks"; cout << "Original String: " << str << endl; // Reverse the string reverseString(str); cout << "Reverse of the string: " << str << endl; return 0; }
🌐
Reddit
reddit.com › r/cprogramming › reversing a string with pointers
r/cprogramming on Reddit: Reversing a String With Pointers
October 1, 2024 -

So i just got to pointers in the K&R C programming book and one of the challenges is to rewrite the functions we worked on previously and implement pointers. i am trying to understand the topics as well as i can before moving forward in the book so if you guys could tell me the best practices and what i should have done in this snippet of code i would greatly appreciated. for reference i was thinking about how i see temp numbers like i used less and less in replacement of ( ; check ; increment ). sorry if this post seems amateur.

#include <stdio.h>
#include <string.h>

void reverse(char *s) {
    char temp[20];
    int len = strlen(s); 
    s += len - 1;
    int i = 0;
    while (len--) {
        temp[i++] = *s--;
    }
    temp[i] = '\0';        // Null-terminate the reversed string
    printf("%s\n", temp);  // Print the reversed string
    
}

int main(void) {
    char string[20] = "hello world";
    reverse(string);
    return 0;
}
#include <stdio.h>
#include <string.h>


void reverse(char *s) {
    char temp[20];
    int len = strlen(s); 
    s += len - 1;
    int i = 0;
    while (len--) {
        temp[i++] = *s--;
    }
    temp[i] = '\0';        // Null-terminate the reversed string
    printf("%s\n", temp);  // Print the reversed string
    
}


int main(void) {
    char string[20] = "hello world";
    reverse(string);
    return 0;
}
🌐
Medium
medium.com › @togunchan › reversing-a-string-in-c-using-pointers-a-step-by-step-guide-236746346254
How to Reverse a String in C Using Pointers — Step-by-Step Explanation
November 23, 2025 - A simple guide to reversing a C string using pointers — find the end, swap characters, and apply efficient pointer arithmetic.
🌐
w3resource
w3resource.com › c-programming-exercises › pointer › c-pointer-exercise-22.php
C : Print a string in reverse order using pointer
#include <stdio.h> int main() { // Declaration of variables char str1[50]; // Original string char revstr[50]; // Reversed string char *stptr = str1; // Pointer to the original string char *rvptr = revstr; // Pointer to the reversed string int i = -1; // Counter initialized with -1 // Displaying the purpose of the program printf("\n\n Pointer : Print a string in reverse order :\n"); printf("------------------------------------------------\n"); printf(" Input a string : "); scanf("%s", str1); // Taking input of a string // Loop to find the length of the string by moving the pointer to the end w
🌐
Java Guides
javaguides.net › 2023 › 09 › c-program-to-reverse-string-using-pointers.html
C Program to Reverse a String Using Pointers
September 12, 2023 - 2. Set one pointer at the start and another at the end of the string. 3. Swap the characters at the pointers' positions and move the pointers towards each other. 4. Repeat until the pointers meet or cross each other.
🌐
LabEx
labex.io › tutorials › c-reverse-a-string-using-pointer-123325
Reverse a String Using Pointer
Now, we will declare two pointer variables: one to point to the first element of the string 'str', and the other to point to the first element of the reversed string 'rev'. ... We need to find the end of the string so that we can start traversing ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › c language › reverse-string-in-c
Reverse String in C - GeeksforGeeks
#include <stdio.h> #include <string.h> void rev(char* s) { // Initialize l and r pointers int l = 0; int r = strlen(s) - 1; char t; // Swap characters till l and r meet while (l < r) { // Swap characters t = s[l]; s[l] = s[r]; s[r] = t; // Move pointers towards each other l++; r--; } } int main() { char s[100] = "abcde"; // Reversing s rev(s); printf("%s", s); return 0; } ... Apart from the simple method mentioned above, there are few more methods which we can use to reverse the string.
Published   December 5, 2024
🌐
Teachics
teachics.org › home › data structures using c examples › c program to reverse a string using pointers
C program to reverse a string using pointers | Data Structures Using C Examples | Teachics
May 12, 2024 - #include <stdio.h> #include <string.h> int main() { char str[100]; int len, i; char *begin, *end, ch; printf("Enter a string: "); gets(str); len = strlen(str); begin = str; end = str; for (i = 0; i < len - 1; i++) end++; for (i = 0; i < len / 2; i++) { ch = *end; *end = *begin; *begin = ch; begin++; end--; } printf("Reverse of the string is: %s", str); return 0; }
🌐
Hero Vired
herovired.com › learning-hub › blogs › reverse-a-string-in-c
C Program to Reverse a String Using for Loop and Recursion
... There are multiple ways to reverse a string, The most common approach is to iterate through the string from both ends and swap characters until reaching the middle. The other methods are using the strrev() function, using pointers, recursion, ...
🌐
ProCoding
procoding.org › c-program-to-reverse-a-string-using-pointers
C program to reverse a string using pointers | ProCoding
May 23, 2024 - Pointers, which store memory addresses, allow direct manipulation of array elements, making operations like reversing a string straightforward and efficient. Initialize pointers: Use pointers to point to the start and end of the string. Swap ...
🌐
Javatpoint
javatpoint.com › reverse-a-string-in-c
Reverse a String in C - javatpoint
Reverse a String in C with Tutorial, C language with programming examples for beginners and professionals covering concepts, c pointers, c structures, c union, c strings etc.
🌐
Scanftree
scanftree.com › programs › c › reverse-a-string-using-pointers
Reverse a string using pointers | Basic , medium ,expert programs example in c,java,c/++
C program to reverse a string using pointers. #include <stdio.h> #include <conio.h> void main() { char *s; int len,i; clrscr(); printf("\nENTER A STRING: "); gets(s); len=strlen(s); printf("\nTHE REVERSE OF THE STRING IS:"); for(i=len;i>=0;i--) printf("%c",*(s+i)); getch(); }
🌐
Interviewing.io
interviewing.io › questions › reverse-string
How to Reverse a String [Interview Question + Solution]
September 13, 2018 - So the space complexity is O(n). If the input to the function is a character array itself or the language supports string mutability, then the space complexity would be O(1), because the algorithm does an in-place reversal of the character array / string. Note: While recursion will work for this problem, it overcomplicates the problem without added benefit and as such many interviewers will prefer one of the non-recursive approaches above. We have included the recursive solution here for completeness. In the previous approach, we iterated two pointers toward the middle of the string and used them to swap the characters at the index of each pointer.
🌐
Simplilearn
simplilearn.com › home › resources › software development › c program to reverse a string using different methods
Program to Reverse a String in C Using Different Methods
September 11, 2025 - Learn C program to reverse a string using two different principles and reasoning without utilizing any preset functions with example code. Start learning now!
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
YouTube
youtube.com › cs classroom
Reverse a string using pointer | C program | Malayalam tutorial - YouTube
AboutPressCopyrightContact usCreatorsAdvertiseDevelopersTermsPrivacyPolicy & SafetyHow YouTube worksTest new features · © 2024 Google LLC
Published   December 27, 2020
Views   7K
🌐
Medium
willfindu.medium.com › how-to-reverse-a-string-in-c-using-pointers-9cdfa3331474
How to reverse a string in C using Pointers | by Saurabh Dwivedy | Medium
March 8, 2024 - #include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char str[] = "Abbas"; for(int x = strlen(str) - 1; x >= 0; x--) { printf("%c ", str[x]); } printf("\n"); int len = strlen(str); char* reversed = malloc(len + 1); if (reversed == NULL) { printf("Error allocating memory for pointer\n"); return -1; } // core logic for reversal for(int i = len - 1; i >= 0; i--) { reversed[len - i - 1] = str[i]; printf("str[%d] = %c, reversed[%d] = %c ", i, str[i], (len - i - 1), reversed[len - i - 1]); printf("\n"); } reversed[len] = '\0'; printf("The reversed string is: %s\n", reversed); free(reversed); return 0; }
🌐
Sanfoundry
sanfoundry.com › c-program-reverse-string
Reverse a String in C - Sanfoundry
October 18, 2023 - 1. Start the program. 2. Take a string as input. 3. Declare a function to reverse a string which takes a string as parameter. 4. In the function: Declare two pointers start and end. Initially Start and end both points at the first character of string.
🌐
Notes
notes.iamdev.in › home › how to reverse a string using two pointers method in c
How to reverse a string using two pointers method in C » Notes
July 11, 2024 - Moves the end pointer to the end of the string by iterating until it encounters the null terminator ('\0'). Adjusts end to point to the last character of the string. Prints characters from end to start using a while loop. ... Defines a character array str containing the string "Hello, World!". Prints the original string. Calls printReverse to print the reversed string.
🌐
Studytonight
studytonight.com › c › programs › pointer › reverse-a-string-using-pointer
C Program to Reverse a String using Pointer | C Programs | Studytonight
Simple Interest · Greatest Common Divisor(GCD) Roots of Quadratic Roots · Identifying a Perfect Square · Calculate nPr and nCr · Windows Shutdown · Without Main Function · Menu Driven Program · Changing Text Background Color · Current Date and Time · Below is a program to reverse a string using pointer: #include <stdio.h> int main() { printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); char str[100]; char rev[100]; char *sptr = str; // sptr stores the base address of the str char *rptr = rev; // rptr stores the base address of the reverse int i = -1; printf("\n\nEnter a string