25+ C Programs to Reverse a String

This post contains a total of 25+ Hand-Picked C Program examples to Reverse a String, with Source Code. All the Programs are made using C Programming language.

You can use the source code of these programs for educational use with credits to the original owner.

Related Posts

Click a Code to Copy it.

1. By BroFar

Made by BroFar. C Program to reverse a input string. ( Source )

#include<stdio.h> 
#include<string.h> 
int main() { int i,j=0; 
char str1[200],str2[200]={0}; 
printf("Enter a string to get reverse of string\n"); 

scanf("%s",str1); 
for(i=strlen(str1)-1;i>=0;i--) { str2[j]=str1[i]; j++; } str2[j]='\0';
printf("Created by BroFarOps 11-13-2018\n");
printf("This is my 5th c language code\n\n");

printf("The original string is:\n");
printf("%s\n",str1); 
printf("\n");
printf("The reverse string is: \n");
printf("%s\n",str2); }

2. By Jully Fredy

Made by Jully Fredy. Enter a string to print it in reverse. ( Source )


#include <stdio.h>
#include <string.h> 
int main(){
   char arr[100];
   printf("Enter a string\n");
   gets(arr);
   printf("Original string is: %s\n", arr);
   strrev(arr);
   printf("Reverse of the string is: %s\n", arr);
   return 0;
} 

3. By Amar Dahake

Made by Amar Dahake. C program to Reverse String without using Library. ( Source )


#include <stdio.h>

int main() {
    char string[] = "olleH";
    int len = 0;
    while(string[len++] != 0) {
        //Nothing Done Here !
    }
    int i = 0;
    char temp;
    len--;
    while(i <= len/2) {
        temp = string[i];
        i++;
        string[i-1] = string[len-i];
        string[len-i] = temp;
    }
    string[len] = '\0';
    printf("Reverse String %s\n",string);
    return 0;
}

4. By Saquib Imam

Made by Saquib Imam. ( Source )


#include <stdio.h>

int main() {
    int i;
    char str[50];
    printf("Enter your name:");
    scanf("%s",str);
    for(i=0;str[i]!='\0';i++);
    for(i=i-1;i>=0;i--)
     printf("%c",str[i]);
    return 0;
}

5. By Yamin Mansuri

Made by Yamin Mansuri. ( Source )

#include <stdio.h>
#include<string.h>
int main() {

    char s[100];
    int len;
    while(1)
    {    
        printf("Enter a string : ");
        gets(s);
        printf("%s\n",s);
        len=strlen(s);
        printf("Reverse string : ");
        if(len==0)
        break;
        while(len>0)
        {
            len--;
            printf("%c",s[len]);
        }
        printf("\n");
        break;
    }
    
    return 0;
}

6. By Shubham Parate

Made by Shubham Parate. ( Source )

#include <stdio.h>

int main() {
char s[10],r[10];
int b,e,count=0;
printf ("Enter the string \n");
scanf("%s",s);
while(s[count]!='\0')
count++;
e=count-1;
for(b=0;b<count;b++)
{
    r[b]=s[e];
    e--;
}
r[b]='\0';
printf ("Reverse the string \n%s\n",r);
    return 0;
}

7. By Yesh Jadav

Made by Yesh Jadav. C code for reversing a string using pointer. ( Source )

#include <stdio.h>
int main() {
char str[1000],*ptr;
int i,len;
printf ("enter a string");
printf("\n");
scanf ("%s",str);
ptr=str;
for(i=0;i<1000;i++){
    if(*ptr=='\0')
    break;
    ptr++;
}
len=i;
ptr--;
printf("reversed string\n");
for(i=len;i>0;i--){
    printf ("\t");
    printf("%c",*ptr--);
}
    return 0;
}

8. By BroFar

Made by BroFar. ( Source )


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

int main(void) {     
   char Str[100], RevStr[100];     
   int i, j, len;
   printf("\n Please Enter any conversational String :\n ");
       gets(Str);          
       j = 0; 
       len = strlen(Str);     
     for (i = len - 1; i >= 0; i--){         
     RevStr[j++] = Str[i];}     
     RevStr[i] = '\0';
  printf(Str);
  printf("\n \n String after Reversing \n %s", RevStr );
            return 0; }

9. By Robert BrownFarley

Made by Robert BrownFarley. Pre-selected c reverse string. ( Source )


// reference aid StackOverflow
#include <stdio.h> 
#include <string.h> 
// basic reverse string 
char *revStr (char *str) { char tmp, *src, *dst; size_t len; if (str != NULL)

{ len = strlen (str); if (len > 1) { src = str; dst = src + len - 1; while (src < dst) { tmp = *src; *src++ = *dst; *dst-- = tmp;

} } } return str;
 }

// general input of what you plan to reverse
char *str[] = {"", "b", "br", "bro", "brof", "brofa", "brofar", "brofaro", "brofarop", "brofarops"}; 

int main(int argc, char *argv[]) { int i; char s[10000]; for (i=0; i < sizeof(str)/sizeof(str[0]); i++) { strcpy (s, str[i]);
 printf ("'%s','changes','%s'\n", str[i], revStr(s));
 
}
printf("\nCreated by BroFarOps on 10-17-2018\n");
return 0; }

10. By WTH

Made by WTH. ( Source )

#include <stdio.h>

int main() {
char str[100],temp;
int i,l=0,a=0;
scanf("%[^\n]",str);
puts(str);
printf("string in reverse order:-\n\n");
while(str[i]!='\0')
{
    l++;
    i++;
    a++;
}
a=(l-1);
for(i=0;i<l;i++)
{
if(i<a){
temp=str[i];
    str[i]=str[a];
    str[a]=temp;
    a--;
    }
}

puts(str);

    return 0;
}

11. By Sฯ‰ฮฑฯะธรฎL

Made by Sฯ‰ฮฑฯะธรฎL. This will reverse only words of your string. Give a space on both sides of special characters like (.), (?), (!),(,) etc. for best results. ( Source )

#include <stdio.h>

int main(void) 
{
    char str[50];
    int i,ch,total;
    scanf("%[^\n]", str);
    puts("INPUT:");
    puts(str);
    puts("OUTPUT:");
    for(total=0;str[total] != '\0';++total);
    while(total)
    {
        for(i=total-1,ch=0;i>=0;--i)
        {
            if(str[i] != ' ')
                ++ch;
            else
                i= -1;
        }
        
        for(i=total-ch;i<total;++i)
        {
            printf("%c",str[i]);
        }
        
        total=total-ch;
        if(total != 0)
        {
            printf(" ");
            --total;
        }
    }




return 0;




//created by swapnil sahu
//like it if you like it...
}

12. By Charan Leo25

Made by Charan Leo25. ( Source )

#include <stdio.h>

int main() {
char str[20],rev[20];
int i,j,l;
gets(str);
i=0;
//l=strlen(str);
for(l=0;str[l]!=0;l++);
j=l-1;
while(rev[i]<l){
    rev[i]=str[j];
    i-=-1;
    j--;
}
rev[i]='\0';
printf("hi %s",rev);
    return 0;
}

13. By Michail Getmanskiy

Made by Michail Getmanskiy. Enter your string in line ‘ strcpy( instr, “aduga duga tuda suda”); ‘. ( Source )

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

char *strrev(char *str) {
  if (!str || !*str)
    return str; 
  int i = strlen(str) - 1, j = 0; 
  char ch; 
  while (i > j) { 
    ch = str[i]; 
    str[i] = str[j]; 
    str[j] = ch; 
    i--; j++; 
  } 
  return str; 
}

int main() {
    char instr[1000];
    strcpy( instr, "aduga duga tuda suda");
    printf("\"%s\"\n", instr );
    strrev( instr );
    printf("\"%s\"\n", instr );
    return 0;
}

14. By Sฯ‰ฮฑฯะธรฎL

Made by Sฯ‰ฮฑฯะธรฎL. ( Source )

#include <stdio.h>
//created by (swapnil sahu)
void main(void) 
{
    char str[50];
    int i;
    gets(str);
    for(i=0; str[i] != '\0'; ++i);
    
    while (i-- >= 0)
    {
        printf("%c", str[i]);
    }
}

15. By kavya

Made by kavya. ( Source )

#include <stdio.h>

int main() {
char name[20]; 
int n=0,i;
printf("enter a string\n");
gets(name);
while(name[n]!='\0')
n++;
for(i=n-1;i>=0;i--) 
putchar(name[i]);
    return 0;
}

16. By Kamlesh Kumar

Made by Kamlesh Kumar. Simple C program to reverse a text string. ( Source )

#include<stdio.h>
void main()
{
    int i, j, k;
    char str[1000];
    char rev[1000];
    printf("Enter a string:\t");
    scanf("%s", str);
    printf("The original string is %s\n", str);
    for(i = 0; str[i] != '\0'; i++);
    {
        k = i-1;
    }
    for(j = 0; j <= i-1; j++)
    {
        rev[j] = str[k];
        k--;
    }
    printf("The reverse string is %s\n", rev);

}

17. By ๐Ÿ‚B. Eitan ๐ŸŒต

Made by ๐Ÿ‚B. Eitan ๐ŸŒต. ( Source )



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


/*
 *  Please supply a nice string.
 */
int main() {
    int length, i;
    char word[50];

    fgets(word, 100, stdin);
    printf(">You say:\n %s\n\n", word);
    length = strlen(word);

    /* Traverse half of the strig, will do. */
    for(i = 0; i < length / 2; i++) {
        char temp = word[length - i - 1];
        word[length - i-1] = word[i];
        word[i] = temp;
    }

    printf("Reversed:\n%s\n", word);

    return 0;
}

18. By Amz42

Made by Amz42. C program to reverse a string without strrev. ( Source )

#include <stdio.h>
#include <string.h>
int main() 
{ char ch;
  char name[100];
  printf("Enter your name: ");
  gets(name); puts(name);
  int l=strlen(name);
  int lp=l%2==0?(l/2):((l/2)+1);
  l--;
  for(int i=0;i<lp;i++)
     { ch=name[i];
       name[i]=name[l];
       name[l]=ch;
       l--;
     }
  printf("Reversed string is: ");
  printf("%s",name);
    return 0;
}

19. By hacker002

Made by hacker002. ( Source )

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

int main() {
    char a[100];
    int l;
    int i;
    
    scanf("%s",a);
    
    l=strlen(a);
    
    for(i=l;i>=0;--i)
    {
        printf("%c",a[i]);
    }
    
    return 0;
}

20. By Laxman Kamat

Made by Laxman Kamat. Input a string in terminal, the string will get reversed. ( Source )


#include <stdio.h>

int main() {
    char input[100];
    int n = 0;
    scanf("%s", input);
    int length = 0;
    while(input[n] != '\0'){
        length = length +1;
        n++;
    }
    
    for( int i= length-1;i >= 0;i--){
        printf("%c", input[i]);
    }
}

21. By Vishal Sharma

Made by Vishal Sharma. ( Source )

#include<stdio.h>
int main(){
    int begin,end,count=0;
    char s[100],r[100];
    printf("enter a string you want to reverse\n");
    gets(s);
    //calculating the length
    while(s[count]!='\0')
    count++;
    end=count-1;
    for(begin=0;begin<count;begin++){
        r[begin]=s[end];
        end--;
    }
    printf("the required string is: %s",r);
    
}

22. By Natasha

Made by Natasha. ( Source )

#include <stdio.h>
#include<string.h>
int main() 
{
char str[100];
int n;

printf("enter no. of characters (including space , punctuation marks etc): \n");

scanf("%d",&n);

printf ("enter the string :\n");

for(int i=0;i<n;i++)
{
    scanf ("%c",&str[i]);
}

printf("reverse string is :\n");

for (int j=strlen(str)-1;j>=0;j--)
{
    printf(" %c",str[j]);
}

    return 0;
}

23. By Hasanuzzaman Shimul

Made by Hasanuzzaman Shimul. ( Source )

#include <stdio.h>
#include <conio.h>
void reverse(void)
{
    char ch;
    if((ch=getchar())!='\n')
    reverse();
    putchar(ch);
}
int main() {
    printf("Type a string:");
    reverse();
    return 0;
}

24. By John Piper

Made by John Piper. Basic C program to reverse a string. ( Source )

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

#define MAXLENGTH 50

void writePrintRevF();

void reverseString(char oldString[]);

int main() {
    
    writePrintRevF();
    
	return 0;
}

void writePrintRevF() {
    char inputString[MAXLENGTH];
    printf("Enter a word to reverse\n");
    //scanf("%[^\n]%*c", inputString);
    scanf("%s\n", inputString);
    reverseString(inputString);
    
}

void reverseString(char oldString[]) {
    int stringEnd = strlen(oldString);
    char newRevString[MAXLENGTH];
    int newStringIndex = 0;
    for (int i=stringEnd-1; i>-1; i--) {
        newRevString[newStringIndex] = oldString[i];
        ++newStringIndex;
    }
    newRevString[newStringIndex] = '\0';
    printf("%s", newRevString);
}

25. By Hichem Abdi

Made by Hichem Abdi. Reverse a string without string.h ( Source )

#include <stdio.h>
#include <time.h>

char number [200];
char reverse [200];
int v;
   

static int length_funct (char * string)
{
    int e,len=0;
    for (e=0;e<=200;e++)
    {
        if (string[e]!= '\0')
        {
            len++;
        }
        else 
        {
            break;
        }
    }
    
    return len;
}

static int reverse_funct(char* reverse_out, char* number_in)
 {
int i,j,p;


i=length_funct(&number_in[0]);
p=i;
for(j=0;j<=i;j++) 
   {
    reverse_out[j]=number_in[p];
    p--;
    }
    
   return i; 
}
static void print_string (char * string_print, int len_string )
{
    
  int local;
  
  for (local=0; local<=len_string;local++)  
  {
      
      printf("%c",string_print[local]);
  }
}
int main () {
    
        time_t givemetime = time(NULL);
        printf(" \n this code is run in : \n");
        printf("%s", ctime(&givemetime));
    
    scanf("%s",number);
    v= reverse_funct(&reverse[0],&number[0]);
  
  
  printf ("the original string is : \n");
  
  print_string(&number[0],v);
  
  printf(" \n the reverse string is : \n");
  
    print_string(&reverse[0],v);
}

//update in 20/05/2020 with 100 views
//update in 22/05/2020 ; add the function time , please consider the difference in time !

26. By Sweet heart

Made by Sweet heart. ( Source )

#include <stdio.h>
int main() 
{
char s[1000],r[1000];
int count=0,end,begin;
printf("Enter a string\n");
gets(s);
printf("%s\n",s);

while (s[count]!='\0')
{ count++;
 end=count-1;} 
 
for(begin=0;begin<count;)
{ r[begin++]=s[end--];}
r[begin]='\0';

printf("REVERSE OF THIS STRING IS  : %s\n",r);

    return 0;
}