Write a C program to reverse the string without using strrev() function?

Questions by vigneshwaran

Editorial / Best Answer

Rohan  

  • Member Since Nov-2005 | Feb 18th, 2006


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

void main()
{
   char *str;
   int i,len;

  //not using any temp variable and assume we can use only string array and   length

   printf("Enter String : ");
   scanf("%s",str);
   len=strlen(str)-1;
   for(i=0;i<strlen(str)/2;i++)
   {
         str[i]+=str[len];
         str[len]=str[i]-str[len];
         str[i]=str[i]-str[len--];
   }
   printf("Reverse String is : %s",str);
   getch();
}

Showing Answers 1 - 42 of 42 Answers

dilipiyer

  • Jan 29th, 2006
 

#include<stdio.h>
#include<string.h>
main()
{
 char str[50],revstr[50];
 int i=0,j=0;
 printf("Enter the string to be reversed : ");
 scanf("%s",str);
 for(i=strlen(str)-1;i>=0;i--)
 {
 revstr[j]=str[i];
 j++;
 }
 revstr[j]='\0';
 printf("Input String : %s",str);
 printf("\nOutput String : %s",revstr);
 getch();
}

Rohan

  • Feb 11th, 2006
 

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

void main()
{
   char str[10],temp;
   int i,len;
   printf("Enter String : ");
   scanf("%s",str);
   len=strlen(str)-1;
   for(i=0;i<strlen(str)/2;i++)
   {
      temp=str[i];
      str[i]=str[len];
      str[len--]=temp;
   }
   printf("%s",str);
   getch();

}

Rohan

  • Feb 18th, 2006
 

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

void main()
{
   char *str;
   int i,len;

  //not using any temp variable and assume we can use only string array and   length

   printf("Enter String : ");
   scanf("%s",str);
   len=strlen(str)-1;
   for(i=0;i<strlen(str)/2;i++)
   {
         str[i]+=str[len];
         str[len]=str[i]-str[len];
         str[i]=str[i]-str[len--];
   }
   printf("Reverse String is : %s",str);
   getch();
}

  Was this answer useful?  Yes

umashankar

  • Mar 14th, 2006
 

Can u pls guide me to reverse a string using recursion without using assignment operator.Thanking Uma

  Was this answer useful?  Yes

umaira

  • Apr 17th, 2006
 

can u send me the program to reverse a string using recursion.And the input should be a list of characters i.e not getting the input as string.

  Was this answer useful?  Yes

Venkatesh

  • May 13th, 2006
 

How to write a c program to reverse the string with in the same string? I.e No other string variables should be used. Not even built functions also.

  Was this answer useful?  Yes

Syed Aftab

  • Jul 4th, 2006
 

program to reverse a string using recursive function:

---------------------------------------------------

void reverse (int index, char *str ) ;

int main (void)

{

   char name[100];

   printf ("Enter a mutli-word string") ; 

   gets(name) ;

   reverse (strlen(name) , name ) ;

}

void reverse (int index, char *str )

{

  if (--index < 0 )

  {

     return ;

  }

  else

  {

        putchar ( *(str + index) ) ;  

       reverse (index, str) ;

  }

}

Logic:

-----

For eg : let's say the user has entered 50 characters.

then index is initially 50.

when reverse() function is called for the first time then

index will be 50 .we first decrement the index by 1 so that

it will become 49 and i am printing the 49th character i.e last

character in the string then reverse() function is called again

with index as 49 and str pointing to 49th character  and again

the index will be decremented by 1 so that it will become 48 this

will continue until zeroth index is reached.

i hope this will solve the problem that you have reported , for any

queries you can mail me.

Regards,

Syed 

  

  Was this answer useful?  Yes

neha

  • Jul 29th, 2006
 

#include<stdio.h>
int main()
{
char c;
c=getchar();

if(c!='.') //dot is to terminate when u r finished given the string char by char/

main();//calling main recursively

putchar(c);
return 0;
}

BHARADWAJ

  • Aug 13th, 2006
 

Excellent code, Please give the solution for this RE: Write a C program to reverse the string by taking single char i.e by putting each time to that char

  Was this answer useful?  Yes

Gary

  • Sep 23rd, 2006
 

not really a C programmer.pseudocode:String reverse(str){ if(str.length == 1){ return str; }else{ return(reverse(secondHalfOfString) + reverse(firstHalfOfString)) }}

  Was this answer useful?  Yes

sajag

  • Oct 11th, 2006
 

void ret_str(char* s){ if(*s != '') ret_str(s+1); cout<<*(s);}int main(){ ret_str("born2c0de"); return 0;}~

  Was this answer useful?  Yes

varun51

  • Oct 14th, 2006
 

I have written this code:

#include <stdio.h>
void rev_str(char* s)
{

if(*s != '')
rev_str(s+1);


printf("%c",*s);
}

int main()
{
rev_str("born2c0de");
return 0;
}

Here i am able to reverse the string and print it out...but i want rev_str function to return the reversed string or ideally reverse the passed string itself.

Can you help me out?

  Was this answer useful?  Yes

venkataramana

  • Oct 17th, 2006
 

this is the program to revers the string without using string functions.

main()

{

int i,k,t,j;

char s1[20],s2[20];

printf("enter the stringn");

gets(s);

for(i=0;s[i]!='';i++)

{

j++;

}

t=0;

for(k=j;k>=0;k--)

{

s2[t]=s1[k];

t++;

};

s2[t]='';

printf("%s",s2);

}

  Was this answer useful?  Yes

Jurgen Grech

  • Dec 17th, 2006
 

#includeint main(){char c;c=getchar();if(c!='.') //dot is to terminate when u r finished given the string char by char/{ main();//calling main recursivelyputchar(c); }return 0;}

  Was this answer useful?  Yes

Vinayak S S

  • Jul 4th, 2007
 

1. Write a function to reverse string.
int main()
{
char a[10] = "mysore" ;
char b[10];
strrev(a , b);
printf("%s",b); //should print "erosym"
}


2. Write a function to capitalize first letter of every word
in string.
int main()
{
char a[50] = "mysore karnataka india" ;
char b[50];
Capitalize(a , b);
printf("%s",b); //should print "Mysore Karnataka India"
}


3. Write a function to reverse the words in string.
int main()
{
char a[50] = "mysore karnataka india" ;
char b[50];
reversewords(a , b);
printf("%s",b); //should print "india karnataka mysore"
}

  Was this answer useful?  Yes

void main()
{
            char str1[20],strev[20]
            int *i=str1;
            int *j=strev;
              printf("Enter the srting");
              scanf("%s",str1);

  while(*i!= '')
   {
    i++;
   }
                while(i>=&str[0])
                 {
                         *j++=*i--;

                 }
*j='';
printf("nthe rev string %s",strev);
}

  Was this answer useful?  Yes

akshatha

  • Oct 28th, 2007
 

Can you write this without using string lib function but with pointers

  Was this answer useful?  Yes

chibionos

  • Dec 13th, 2007
 

The Solution
/*
  Name: String Reverse Function
  Copyright: None
  Author: Chibionos a.k.a Chibi Chakaravarthi
  Date: 13-12-07 08:37
  Description: A Simple program to reverse a given string with the help
               of a function and return the reversed string to the program
               PS: No string manipulation headers and only one string variable
               used.
*/



#include <stdio.h>
#include <iostream.h>
#include <conio.h>

char* StringReverse(char *str1)
{
      // Create a String in the Function to Accomodate the
      // Incoming String as there is no reserved space for
      // Function parameters.
     
      char *str;
      str = (char *)malloc(sizeof(str1));
      strcpy(str,str1);
     
     
      // To get the length of the String.
      int iLength;
      char cTemp;
     
      for(iLength=0;*(str+iLength)!='';iLength++);
           
      /*/ Concept : One index starts from the First and the other starts
                    at the end and the characters are interchanged leaving
                    the middle charater when there is odd number of characters
                    and when even number of characters are present no special
                    case is encountered.
      /*/            
      for(int i=0,j=iLength-1;i<=j;i++,j--)
              {
                                        
              // Code to Exchange the Characters
                      cTemp = *(str+j);
                      *(str+j) = *(str+i);
                      *(str+i) = cTemp;
                                           
              }        
      
      return str;
}

main()
{
      cout<<StringReverse("abcdefghi");
      getch();
}

Hi Rohan,
Can you explain the logic that you write in the above, Reverse of
the string without using temp variable?

str[i]+=str[len];
str[len]=str[i]-str[len];
str[i]=str[i]-str[len]--;

How we can add and subtract a character from other character?

Please
clear me the logic that you explained above?

Regards
Amarendra
moharana.amarendra @ gmail . com
 

  Was this answer useful?  Yes

Below are part of the program that I've done so far.

#include
#include

main()
{
char s1[10];
char s2[10];
char s3[10];
int num;

printf("Enter Original String: ");
scanf("%s",s1);
printf("1) Reverse Stringn");
printf("2) Join Reverse Stringn");
printf("3) Insert Reverse Stringn");
printf("4) Encrypt Stringn");
printf("5) Find Total ASCII Valuen");
printf("Enter Choice:");
scanf("%d",&num);
switch(num){
case 1: strcpy(s2,s1);
strrev(s2)
printf("Result: %s",s2);
break;

case 2: strcpy(s2,s1);
strrev(s2);
printf("Result: %s",strcat(s1,s2));
break;

case 3: strcpy(s2,s1);
strrev(s2);
printf("Result: %s",strcat(s1+1,s2+1));
break;

case 4:
int num2;
printf("Enter Number:");
scanf("%d",&num2);
printf("Result: %s",
break;

case 5:
printf("Result: %s",
break;
}
}

  Was this answer useful?  Yes

Genesis7

  • Dec 24th, 2008
 

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

void main ()
{
    char word[20];
   int i;

   printf ("Enter Your Name: ");
   scanf ("%s",word);

   i = strlen(word)-1;

   while (i >= 0)
   {
       printf ("%c",word[i]);
      i--;
   }
   getch();
}

  Was this answer useful?  Yes

#include<stdio.h>

int main()
{
           char ch,str[20];
           int i,j;
           
           printf("Enter The string to be revresedn");
           gets(str);

           for(j=0;s[j]!='';j++)
           {
           }
           j--;

           for(i=0;i<j;i++)
           {
                 ch = s[i];
                 s[i]=s[j];
                 s[j]=ch;
          
                 j--;
            }

           printf("The Reversed string is %sn",str);

            return 0;
}


          

  Was this answer useful?  Yes

Rajippp

  • Jun 11th, 2009
 

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

void main()
{
   char str[10],temp;
   int i,len;
   printf("Enter String : ");
   scanf("%s",str);
   len=strlen(str)-1;
   for(i=0;i<strlen(str)/2;i++)
   {
      temp=str[i];
      str[i]=str[len];
      str[len--]=temp;
   }
   printf("%s",str);
   getch();

}


  Was this answer useful?  Yes

#include<stdio.h>
void main(){
int i;
char temp;
char str[9] = "Computer";
int length;
length = strlen(str1)-1;
printf("The length of the String is: %dn",length);
for(i=0;i<length;i++){
temp = str[i];
str[i]=str[length];
str[length]=str[i--];
}
printf("The Reversed string is: %sn", str);
getch();
}

  Was this answer useful?  Yes

ANJI

  • Aug 17th, 2011
 

#include
#include
int main()
{
char str[50],revstr[50];
int i=0,j=0;
printf("Enter the string to be reversed : ");
scanf("%s",str);
for(i=strlen(str)-1;i>=0;i--)
{
revstr[j]=str[i];
j++;
}
revstr[j]='�';
printf("Input String : %s",str);
printf("
Output String : %s",revstr);
return 0;
}

  Was this answer useful?  Yes

preparing

  • Sep 8th, 2011
 

Can anyone explain this piece of code to me ??

Code
  1. void reverse(char *str) {

  2. 2 char * end = str;

  3. 3 char tmp;

  4. 4 if (str) {

  5. 5 while (*end) {

  6. 6 ++end;

  7. 7 }

  8. 8 --end;

  9. 9 while (str < end) {

  10. 10 tmp = *str;

  11. 11 *str++ = *end;

  12. 12 *end-- = tmp;

  13. 13 }

  14. 14 }

  15. 15 }

  Was this answer useful?  Yes

chaitu

  • Sep 21st, 2011
 

Where did the variable initialize . Which is used in the program for 2 times

  Was this answer useful?  Yes

Sharath

  • Sep 22nd, 2011
 

Pre>

Code
  1. #include<stdio.h>

  2. #include<conio.h>

  3. #include<string.h>

  4. int main()

  5. {

  6.     int i;

  7.     char a[10];

  8.     printf("Enter a string:");

  9.     scanf("%s",&a);

  10.     for(i=strlen(a)-1;i>=0;i--)

  11. {

  12.     printf("%c",a[i]);

  13. }

  14.     getch();

  15.     return 0;

  16. }

  17.    

  18.  

  Was this answer useful?  Yes

praneeth

  • Oct 9th, 2011
 

Without using any sting manipulation functions.

Code
  1. char* str_reverse(const char *in_str)

  2. {

  3.         char *out_str;

  4.         out_str = (char *)malloc(sizeof(in_str));

  5.         int char_pos=0;

  6.         int str_len=0;

  7.         while(*in_str!='')

  8.         {

  9.                 in_str++;

  10.                 char_pos++;

  11.         }

  12.         in_str--;

  13.         str_len = char_pos;

  14.         while(char_pos >0)

  15.         {

  16.                 *out_str=*in_str;

  17.                 in_str--;

  18.                 char_pos--;

  19.                 out_str++;

  20.         }

  21.         *out_str='';

  22.         while(str_len>0)

  23.         {

  24.                 out_str--;

  25.                 str_len--;

  26.         }

  27.         return out_str;

  28. }

  Was this answer useful?  Yes

SANDEEP

  • Oct 11th, 2011
 

Code
  1. #include<stdio.h>

  2. #include<string.h>

  3. main()

  4. {

  5.         int l,i;

  6.         char s[100],*sp;

  7.         printf("Enter a string:");gets(s);

  8.         l=strlen(s);

  9.         sp=(s+l);

  10.         for(i=0;i<l;i++,sp--)

  11.                 printf("%c",*sp);

  12.         printf("

  13. ");

  14.        

  15. }

  Was this answer useful?  Yes

sudhir madi

  • Oct 17th, 2011
 

#include
#include
reverse(char str[]);
main()
char str[10];
printf("Enter the string to reverse - ");
scanf("%s",str);/*Read contents of string*/
reverse(str);/*Function call by using str */
return 0;
}
reverse(char str[10]) /*Function to reverse string*/
int i,j;
char c;
j=0;
while (str[j]!='�') /* Total count of characters*/
j++;
j--;/*Exact count including Array index 0, reduce total count by 1*/
for(i=0;i<=j;
i++) /*Character-by-character reversing*/
c=str[j];
str[j]=str[i];
str[i]=c;
j--;
}
printf("
The reverse of given string - %s
", str);/*Display the reverse of string*/
}



#include
#include
reverse(char str[]);
main()
{
char str[10];
printf("Enter the string to reverse - ");
scanf("%s",str);/*Read contents of string*/
reverse(str);/*Function call by using str */
return 0;
}
reverse(char str[10]) /*Function to reverse string*/
{
int i,j;
char c;
j=0;
while (str[j]!='�') /* Total count of characters*/
j++;
j--;/*Exact count including Array index 0, reduce total count by 1*/
for(i=0;i<=j;i++) /*Character-by-character reversing*/
{

c=str[j];
str[j]=str[i];
str[i]=c;
j--;
}
printf("The reverse of given string - %s
", str); /*Display the reverse of string*/
}

  Was this answer useful?  Yes

Chandrabhan

  • Dec 26th, 2011
 

Code
  1. //String Reverse without using strrev function

  2. #include"stdio.h"

  3. #include"conio.h"

  4. #include"string.h"

  5. void main()

  6. {

  7.  char str1[20],str2[20];

  8.  int i,j=0;

  9.  clrscr();

  10.  printf("***********String Reverse*********

  11. ");

  12.  printf("Enter String:");

  13.  gets(str1);

  14.  for(i=strlen(str1)-1;i>=0;i--,j++)

  15.  {

  16.   str2[j]=str1[i];

  17.  }

  18.  str2[j]=NULL;

  19.  printf("

  20. Reverse String is:");

  21.  puts(str2);

  22.  getch();

  23. }

  Was this answer useful?  Yes

indhu

  • Feb 7th, 2012
 

Code
  1. #include <stdio.h>

  2. void main()

  3. {

  4. char a[50];

  5. int b=0,i=0;

  6. printf("Enter the String

  7. ");

  8. scanf("%s",a);

  9. b=strlen(a);

  10. for(i=b;i>=0;i--)

  11. {printf("%c",a[i]);

  12. }getch();

  13. }

  Was this answer useful?  Yes

rajkumar verma

  • May 13th, 2012
 

Without using strlen and strrev functions.....

Code
  1. {geshibot language="c"}

  2. #include

  3. #define MAX 500

  4. int main()

  5. {

  6.     char v[MAX];

  7.     char r[MAX];

  8.     static int i=0,d=0;

  9.     int j;

  10.     printf("Enter string to be reversed :

  11. ");

  12.     gets(v);

  13.     while(v[i]!=)

  14.     {

  15.                      i++;

  16.     }

  17.     for(j=i;j>0;j--)

  18.     {

  19.                      r[j-1]=v[d];

  20.                      d++;

  21.     }

  22.     r[i]=;

  23.     printf("

  24. ");

  25.     printf("Reversed string :

  26. ");

  27.     puts(r);

  28.     return 0;

  29. }

  30.  

  Was this answer useful?  Yes

ankur

  • Jun 18th, 2012
 

Code
  1. #include

  2. #define MAX 500

  3. int main()

  4. {

  5. char v[MAX];

  6. char r[MAX];

  7. static int i=0,d=0;

  8. int j;

  9. printf("Enter string to be reversed :

  10.  

  11. ");

  12. gets(v);

  13.    while(v[i]!=)

  14.         {

  15.         i++;

  16.         }

  17.         for(j=i;j>0;j--)

  18.         {

  19.         r[j-1]=v[d];

  20.         d++;

  21.         }

  22.  

  23. r[i]=;

  24. printf("");

  25. printf("Reversed string :");

  26. puts(r);

  27. return 0;

  28. }

  29.  

  Was this answer useful?  Yes

Munish Katoch

  • Oct 10th, 2012
 

Code
  1. char* revString(char *input) {

  2.         int i,j;

  3.         if(input == NULL)

  4.                 return NULL;

  5.                

  6.        

  7.         for(i = strlen(input)-1,j=0; j <= ((strlen(input)-1)/2);i--,j++){

  8.        

  9.                 char temp = input[j];

  10.                 input[j] = input[i];

  11.                 input[i] = temp;

  12.                 //printf("%s

  13. ",input);

  14.         }

  15.        

  16.         return input;

  17. }


  Was this answer useful?  Yes

Gabriel

  • Oct 23rd, 2012
 

Using bitwise operators

Code
  1. void ReverseString(char* pszBuff)

  2. {

  3.   int iLen = strlen(pszBuff);

  4.   for (int i = 0; i < (iLen / 2); ++i)

  5.   {

  6.     char aux = *(pszBuff + i) ^ *(pszBuff + iLen - 1 - i);

  7.     *(pszBuff + iLen - 1 - i) ^= aux;

  8.     *(pszBuff + i) ^= aux;

  9.   }

  10. }

  Was this answer useful?  Yes

M.AKHIL

  • Jan 21st, 2013
 

Code
  1. #include <stdio.h>

  2. #include <conio.h>

  3. #include <string.h>

  4. void main()

  5. {

  6.    char *str;

  7.    int i,len;

  8.  

  9.   //not using any temp variable and assume we can use only string array and   length

  10.  

  11.    printf("Enter String : ");

  12.    scanf("%s",str);

  13.    len=strlen(str)-1;

  14.    for(i=0;i<strlen(str)/2;i++)

  15.    {

  16.          str[i]+=str[len];

  17.          str[len]=str[i]-str[len];

  18.          str[i]=str[i]-str[len--];

  19.    }

  20.    printf("Reverse String is : %s",str);

  21.    getch();

  22. }

  Was this answer useful?  Yes

kanwal

  • Jan 29th, 2013
 

#include
#include
#include
void main()
{
char string1[20],string2[20];
int i,j,length;
printf("
Enter the string : ");
gets(string1);
length=strlen(string1);
j=length;
printf ("%d",j);
for(i=0;i {

string2[i]=string1[j-1];
j--;
}
string2[i] = ;
printf("
Reversed string is : %s",string2);
getch();
}

Code
  1. #include<stdio.h>

  2. #include<conio.h>

  3. #include<string.h>

  4.  

  5. void main()

  6. {

  7. char string1[20],string2[20];

  8. int i,j,length;


  9. Enter the string : ");

  10. gets(string1);

  11. length=strlen(string1);

  12. j=length;

  13. printf ("%d",j);

  14.         for(i=0;i<length;i++)

  15.         {

  16.        

  17.                 string2[i]=string1[j-1];

  18.                         j--;

  19.  

  20.         }

  21.         string2[i] = ;


  22. Reversed string is : %s",string2);

  23. getch();

  24. }

  25.  

  Was this answer useful?  Yes

sunny

  • Apr 25th, 2013
 

it is asking for prototype ..???

  Was this answer useful?  Yes

Ankur Verma

  • May 18th, 2013
 

Code
  1.  

  2. #include <stdio.h>

  3. #include <string.h>  

  4. void main()

  5. {  

  6. int len=0,i=0;  

  7. char s[16]="virendra";      

  8. while (*(s+len) !=)

  9. {    

  10. len++;

  11. }  

  12. printf("%d",len);      

  13. for (i=0;i<=(len)/2;

  14. i++)

  15. {      

  16. printf("  i:

  17. %d len:%d and s[i]:

  18. %c s[len-1]:%c",i,len-1,s[i],s[len-1]);           s[i]+=s[len-1];    

  19. s[len-1]=s[i]-s[len-1];    

  20. s[i]-=s[len-1];            

  21. len--;  

  22. }  

  23. printf(" ");  

  24. puts(s);

  25. }

  26.  

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Answer Question

Click here to Login / Register your free account


 
Send   Reset

 

Related Answered Questions

 

Related Open Questions