๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ how to reverse a string in c without using strrev?
r/C_Programming on Reddit: How to reverse a string in c without using strrev?
September 9, 2019 -

So I have a task which requires me to manipulate arrays and reverse a string. I have written the code but it doesn't work quite perfectly. The problem is that it reverses the string BUT it also includes weird symbols after the reversed string. I'm pretty new to programming and I have tried to find an answer elsewhere but don't know what to look for exactly. I appreciate any constructive feedback, Thanks!

#include <stdio.h>

int main(){

char string[256];
char output[256];
int begin;int end;
int count = 0;

printf("Input a string\n");
fgets(string, 256, stdin);

while (string[count] != '\0'){
count++;

end = count - 1;
}
for (begin = 0; begin < count; begin++) {
output[begin] = string[end];
end--;
}
string[begin] = '\0';

printf("%s\n", output);
}
๐ŸŒ
Upgrad
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ reverse a string in c
Reverse a String in C | Without strrev & With Examples
October 14, 2024 - No, strrev() is not part of the standard C library. It is available in some compilers like Turbo C, but for portability, it's better to implement your own reversal logic. Yes, you can reverse a string using recursion.
People also ask

What is the best way to reverse a string in C?
The best method depends on the context. The two-pointer approach is efficient and operates in place, while recursion provides an elegant solution, and using strrev() is quick if available in your compiler.
๐ŸŒ
upgrad.com
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ reverse a string in c
Reverse a String in C | Without strrev & With Examples
Can I reverse a string with the for loop in C?
Yes, you can use a for loop to reverse a string by iterating through the string and swapping characters from both ends, ensuring that the loop runs only until the middle of the string.
๐ŸŒ
upgrad.com
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ reverse a string in c
Reverse a String in C | Without strrev & With Examples
Can I reverse a string without using a loop?
Yes, you can reverse a string using recursion. Instead of a loop, recursion repeatedly calls the function to swap characters from both ends of the string.
๐ŸŒ
upgrad.com
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ reverse a string in c
Reverse a String in C | Without strrev & With Examples
๐ŸŒ
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.
๐ŸŒ
Geek Interview
geekinterview.com โ€บ question_details โ€บ 21969
Write a C program to reverse the string without using strrev() function?
Reverse String is:"); puts(str2); getch(); } Feb 7th, 2012 ยท Code ยท #include <stdio.h> void main() { char a[50]; int b=0,i=0; printf("Enter the String ยท "); scanf("%s",a); b=strlen(a); for(i=b;i>=0;i--) {printf("%c",a[i]); }getch(); } May 13th, 2012 ยท Without using strlen and strrev functions.....
๐ŸŒ
Programming Simplified
programmingsimplified.com โ€บ c-program-reverse-string
C program to reverse a string | Programming Simplified
C program to reverse a string that a user inputs. If the string is "hello" then, the output is "olleh." C program to reverse a string using strrev, without using strrev, recursion and pointers.
๐ŸŒ
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
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ c-program-to-reverse-a-string
C program to Reverse a String
April 2, 2025 - In this article, we show you how to write a C program to Reverse a String without using the strrev function, for loop, functions, and pointers
Find elsewhere
๐ŸŒ
CODEDOST
codedost.com โ€บ home โ€บ basic c programs โ€บ c program to reverse a string without using string function
C program to reverse a string without using string function | CODEDOST
July 15, 2018 - #include<stdio.h> #include<string.h> void main() { int i,n; char str[20]; printf("Enter the String to get reversed\n"); gets(str); n=strlen(str); printf("\nReversed string is \n"); for(i=n-1;i>=0;i--) { printf("%c",str[i]); } }
๐ŸŒ
Hero Vired
herovired.com โ€บ learning-hub โ€บ blogs โ€บ reverse-a-string-in-c
C Program to Reverse a String Using for Loop and Recursion
In the C programming language, a given string can be reversed using the strrev function, without strrev, recursion, pointers, or another string, or displaying it in the opposite order.
๐ŸŒ
myCompiler
mycompiler.io โ€บ view โ€บ KJGhl7A4gwe
Program to reverse a string without using strrev : Amay (C) - myCompiler
#include <stdio.h> #include <string.h> int main() { char s1[10]="Tanishka"; char s2[10]; int len1,i; len1=strlen(s1); for(i=0;i<len1;i=i+1) { s2[i]=s1[len1-i-1]; } printf("%s",s2); }
๐ŸŒ
Scribd
scribd.com โ€บ document โ€บ 427517408 โ€บ C-Programs
Reverse String in C Without strrev | PDF | String (Computer Science) | Integer (Computer Science)
The document contains C program code examples for various algorithms and operations: 1) The first section shows programs to reverse a string without built-in functions and using pointers. 2) Later sections demonstrate calculating factorials using for loops and recursion, and programs to check if a number is even or odd.
Top answer
1 of 2
1

For starters according to the C Standard the function main without parameters shall be declared like

int main( void )

The function gets is unsafe and is not supported by the C Standard. Instead use either scanf or fgets.

The function strlen is a standard C string function. So according to the requirement you may not use it.

You are not reversing a string. You are trying to copy a string in the reverse order into another string.

The program can look the following way

#include <stdio.h>

int main(void)
{
    enum { N = 15 };
    char in[N] = "", rev[N];

    printf("Enter a word (upto %d letters): ", N - 1 );
    scanf( " %14s", in );

    size_t n = 0;
    while ( in[n] ) ++n;

    rev[n] = '\0';

    for ( size_t i = 0; i < n; i++ )
    {
        rev[n - i - 1] = in[i];
    }

    puts( rev );
}

If you actually need to reverse a string in place then the program can look the following way

#include <stdio.h>

int main(void)
{
    enum { N = 15 };
    char in[N] = "";

    printf("Enter a word (upto %d letters): ", N - 1 );
    scanf( " %14s", in );

    size_t n = 0;
    while ( in[n] ) ++n;

    for ( size_t i = 0; i < n / 2; i++ )
    {
        char c = in[i];
        in[i] = in[n - i - 1]; 
        in[n - i - 1] = c;
    }

    puts( in );
}
2 of 2
0

EDIT: getline is not standard C, and it is only recognized by POSIX systems. Another solution is to use fgets that works for both OSes. I provided both examples.

As others have already pointed out, you are making some mistakes:

  • Unsafe practice when getting input from the user.
  • Always starting from 15 even if the input string has less chars.

I have created a little example with dynamic allocation that works with more than 15 characters and fixes the afore-mentioned issues. Comments inline to key points.

Example: getline - POSIX

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[]) {

    // Idea from https://stackoverflow.com/questions/7709452/how-to-read-string-from-keyboard-using-c
    char *line = NULL;  /* forces getline to allocate with malloc */
    size_t len = 0;     /* ignored when line = NULL */
    ssize_t read;

    read = getline(&line, &len, stdin);

    if (read > 0)
    {
        printf ("\n  String from user: %s\n",  line);
    }else
    {
        printf ("Nothing read.. \n");
        return -1;
    }    

    // Now we need the same amount of byte to hold the reversed string
    char* rev_line = (char*)malloc(read);
    
    // "read-1" because we start counting from 0. 
    for (int i = 0, j = read-1; i < read; i++, j--)
    {
        rev_line[i] = line[j];
    }
    printf("%s\n",rev_line);

    free (line);  /* free memory allocated by getline */
    free(rev_line);

    return 0;
}

Example: fgets - C standard

fgets does not return the number of characters read, so it has to be chained with strlen to decide how many characters to allocate for the reversed string.

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

int main (int argc, char *argv[]) {

    char line[LINE_MAX];
    size_t len = 0;     /* ignored when line = NULL */
    ssize_t read;


    if (fgets(line, LINE_MAX, stdin) != NULL)
    {
        line[strcspn(line, "\n")] = '\0'; //fgets() reads the \n character (that's when you press Enter). 
        read = strlen(line);
        printf ("\n  String from user: %s\n",  line);
    }else
    {
        printf ("Nothing read.. \n");
        return -1;
    }    

    // Now we need the same amount of byte to hold the reversed string
    char* rev_line = (char*)malloc(read);

    for (int i = 0, j = read-1; i < read; i++, j--)
    {
        rev_line[i] = line[j];
    }
    printf("%s\n",rev_line);

    free(rev_line);

    return 0;
}
๐ŸŒ
Quora
quora.com โ€บ How-do-I-write-a-C-program-to-reverse-a-string-without-using-the-inbuilt-strrev-function
How to write a C program to reverse a string without using the inbuilt strrev() function - Quora
Answer (1 of 2): since you have not specify that whether you want to print the reversed string or want to store the revsed i am considering both of the cases: ok let me give youa complete example on this: * #include * int main() * { * char ar[]="you are my hero"; * int i; * int z...
๐ŸŒ
YouTube
youtube.com โ€บ watch
Reverse a String in C | with and without strrev function | C programming - YouTube
Reverse a String in C | with and without strrev function | C programming #strings #reversestring #strrev #cprogramming #coding #csit #hnbgu #btech #btechcse...
Published ย  March 4, 2025
๐ŸŒ
WsCube Tech
wscubetech.com โ€บ resources โ€บ c-programming โ€บ programs โ€บ reverse-string
How to Reverse a String in C? (8 Programs)
October 23, 2025 - Learn How to Reverse a String in C with 8 programs, including loops, recursion, and functions. Easy-to-follow examples for beginners and experts!