🌐
Reddit
reddit.com › r/c_programming › how to reverse a string in c without using strrev?
How to reverse a string in c without using strrev? : r/C_Programming
September 9, 2019 - In the end, we explicitly add the end of the character symbol in the string. In the end, we print the reverse string. //Using Recursion In this, we will try to reverse the string using the recursive method. Recursion is a method in which a function gives a call to itself.
🌐
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.
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
🌐
Programming9
programming9.com › programs › c-programs › 127-c-program-to-print-reverse-of-a-string-without-strrev-function
C Program to Print Reverse of a String without strrev() function
strrev · user defined · built in function · #include <stdio.h> #include <string.h> int main() { int i,j,len=0; char str[50],revstr[50]; printf("\n Enter a String to Reverse : " ); // Use fgets to read input, with a maximum size of 50 characters fgets(str, sizeof(str), stdin); // Remove the newline character from the end of the string str[strcspn(str, "\r\n")] = 0; // Count the length of the string for(i=0; str[i]!='\0'; i++) { len++; } j=0; // Reverse the string by copying each character from the original string to the new string in reverse order for(i=len-1; i>=0; i--) { revstr[j++]=str[i]; } printf("\n Reverse of the Given String is: %s",revstr); return 0; } OUTPUT: Enter a String to Reverse : www.programming9.com Reverse of the Given String is: moc.9gnimmargorp.www ·
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › reverse a string in c
Reverse a String in C | Without strrev & With Examples
June 23, 2025 - 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.
🌐
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
🌐
TutorialsPoint
tutorialspoint.com › write-a-c-program-to-reverse-a-string-without-using-a-library-function
Program to Reverse String in C
September 10, 2023 - #include <stdio.h> int main() { char s1[] = "TajMahal"; // String Given char s2[8]; // Variable to store reverse string int length = 0; int loop = 0; while(s1[length] != '\0') { length++; } printf("\nPrinting in reverse - "); for(loop = --length; loop>=0; loop--) printf("%c", s1[loop]); loop = 0; printf("\nStoring in reverse - "); while(length >= 0) { s2[length] = s1[loop]; length--; loop++; } s1[loop] = '\0'; // Terminates the string printf("%s\n", s2); return 0; }
🌐
Coderanch
coderanch.com › t › 1010714 › languages › PROGRAMMING-reverse-string-built-functions
C PROGRAMMING Question how to reverse a string in C without using built-in functions (C / C++ forum at Coderanch)
March 8, 2026 - To reverse a string in C without using built-in functions, you can manually swap characters from the start and end of the string, moving towards the center. Here’s the step-by-step logic: Find the length of the string by iterating until the null terminator '\0'. Use two indices: one starting ...
🌐
Blogger
programjoy.blogspot.com › 2019 › 08 › reverse-string-in-c-language.html
Reverse a string without strrev() in C Language
The Reversed String will be printed on the screen. #include<stdio.h> #include<string.h> int main() { int n,i=0; char temp,str[100]; printf("Please, Enter the String : "); gets(str); n=strlen(str); while(i<n) { temp=str[i]; str[i]=str[n-1]; str[n-1]=temp; i++; n--; } puts(str); return 0; } Download the C-Program file of this Program.
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]); } }
🌐
Coder Scratchpad
coderscratchpad.com › home › c program to reverse a string without strrev()
C Program to Reverse a String Without strrev()
September 5, 2025 - The in-place loop or pointer method is best, as they do not require additional memory. Reversing a string without strrev() teaches how to manipulate character arrays and use loops, pointers, or recursion effectively.
🌐
IncludeHelp
includehelp.com › code-snippets › reverse-string-without-using-strrev-function-and-temporary-string-in-c-programming.aspx
C - Reverse the String without using strrev() function and temporary string in C programming - IncludeHelp
September 5, 2016 - In this program we are reversing the string by calculating length (without using strlen()) and by swapping characters of the string. /*C - Reverse the String without using strrev() function in C programming.*/ #include <stdio.h> //function to calculate length of string short getStrLen(char* s){ short len=0; while(s[len]!='\0') len++; return len; } ///function to reverse the string void reverseString(char *str){ short len=0,i,j; char temp; //temp character //get length of string len=getStrLen(str); //reverse string for(i=len-1,j=0;j<(len/2); i--,j++){ //swap characters temp=str[j]; str[j]=str[i]; str[i]=temp; } } int main(){ char text[100]={0}; printf("Enter string: "); //scanf("%s",text); //this is for without space scanf ("%[^\n]%*c", text); printf("String before reverse: %s\n",text); //reverse string reverseString(text); printf("String after reverse: %s\n",text); return 0; }
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;
}
🌐
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.....
🌐
Tricky Edu
trickyedu.com › home › education › c program to reverse a string without using strrev
C Program to Reverse a String Without using Strrev - Tricky Edu
August 30, 2025 - } printf(“First string %s\n”,str1); printf(“Reversed string %s\n”, reversed); //print the original and reverse string ... As opposed to the ANSI C compiler, which has it.
🌐
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); }
🌐
Coders-hub
coders-hub.com › 2013 › 04 › reverse-string.html
C code to reverse a string without strrev() Function | Coders Hub: Android Code Examples and Programming Tutorials
//C program to reverse a given string without using strrev() function// #include<stdio.h> #include<string.h> #include<malloc.h> int main() { int i=0,length; char *p; p=(char*)malloc(sizeof(25)); printf("\nEnter String= "); gets(p); //take the length of the string length=strlen(p); printf("Reverse string\n"); for(i=length-1;i>=0;i--) printf("%c",p[i]); getch(); return 0; } Output of the above Program: Posted by ·
🌐
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...
🌐
IncludeHelp
includehelp.com › c-programs › c-program-to-reverse-a-string.aspx
C program to reverse a string without using library function
Here, we are declaring two strings (character arrays), first string will store the input string and other will store the reversed string.