GeekInterview.com
Series: Subject: Topic:
Question: 117 of 135

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

Asked by: vigneshwaran | Member Since Dec-2005 | Asked on: Jan 18th, 2006

View all questions by vigneshwaran

Editorial / Best Answer

Answered by: Rohan

View all answers by Rohan

Member Since Nov-2005 | Answered On : 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

Answered On : Jan 29th, 2006

View all answers by dilipiyer

#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();}

Yes  1 User has rated as useful.
  
Login to rate this answer.
Rohan

Answered On : Feb 11th, 2006

View all answers by Rohan

#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();}

Yes  1 User has rated as useful.
  
Login to rate this answer.
Rohan

Answered On : Feb 18th, 2006

View all answers by Rohan

#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();}

  
Login to rate this answer.
umashankar

Answered On : Mar 14th, 2006

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

  
Login to rate this answer.
umaira

Answered On : 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.

  
Login to rate this answer.
Venkatesh

Answered On : 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.

  
Login to rate this answer.
Syed Aftab

Answered On : 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 thenindex will be 50 .we first decrement the index by 1 so thatit will become 49 and i am printing the 49th character i.e lastcharacter in the string then reverse() function is called againwith index as 49 and str pointing to 49th character  and againthe index will be decremented by 1 so that it will become 48 thiswill continue until zeroth index is reached.i hope this will solve the problem that you have reported , for anyqueries you can mail me.Regards,Syed   

  
Login to rate this answer.
neha

Answered On : 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; }

Yes  1 User has rated as useful.
  
Login to rate this answer.
BHARADWAJ

Answered On : 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

  
Login to rate this answer.
Gary

Answered On : Sep 23rd, 2006

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

  
Login to rate this answer.
sajag

Answered On : Oct 11th, 2006

Void ret_str(char* s){ if(*s != '') ret_str(s+1); cout

  
Login to rate this answer.
varun51

Answered On : 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?

  
Login to rate this answer.
venkataramana

Answered On : 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);}

  
Login to rate this answer.
Jurgen Grech

Answered On : 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;}

  
Login to rate this answer.
Vinayak S S

Answered On : 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"
}

  
Login to rate this answer.

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);}

  
Login to rate this answer.
akshatha

Answered On : Oct 28th, 2007

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

  
Login to rate this answer.
chibionos

Answered On : Dec 13th, 2007

View all answers by chibionos

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();}

Yes  2 Users have rated as useful.
  
Login to rate this answer.
raghu.neo

Answered On : Mar 2nd, 2008

View all answers by raghu.neo

Love your code neha.. nice way of thinking...

  
Login to rate this answer.

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?RegardsAmarendra
moharana.amarendra @ gmail . com 

  
Login to rate this answer.
yusufharoon84

Answered On : Jun 16th, 2008

View all answers by yusufharoon84

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; } }

  
Login to rate this answer.
Genesis7

Answered On : Dec 24th, 2008

View all answers by Genesis7

# 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();}

  
Login to rate this answer.

#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;}          

  
Login to rate this answer.
Rajippp

Answered On : Jun 11th, 2009

View all answers by Rajippp

#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();}

  
Login to rate this answer.
Aparana Gupta

Answered On : Sep 23rd, 2009

View all answers by Aparana Gupta

#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();}

  
Login to rate this answer.
ANJI

Answered On : 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;}

  
Login to rate this answer.
preparing

Answered On : 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 }

  
Login to rate this answer.
chaitu

Answered On : Sep 21st, 2011

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

  
Login to rate this answer.
Sharath

Answered On : 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.  

  
Login to rate this answer.
praneeth

Answered On : 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. }

  
Login to rate this answer.
SANDEEP

Answered On : 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. }

  
Login to rate this answer.
sudhir madi

Answered On : 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

  
Login to rate this answer.
Chandrabhan

Answered On : 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. }

  
Login to rate this answer.
indhu

Answered On : 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. }

  
Login to rate this answer.
rajkumar verma

Answered On : 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.  

  
Login to rate this answer.
ankur

Answered On : 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.  

  
Login to rate this answer.
Munish Katoch

Answered On : 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. }

  
Login to rate this answer.
Gabriel

Answered On : 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. }

  
Login to rate this answer.
M.AKHIL

Answered On : 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. }

  
Login to rate this answer.
kanwal

Answered On : 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

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.  

  
Login to rate this answer.
sunny

Answered On : Apr 25th, 2013

It is asking for prototype ..???

  
Login to rate this answer.
Ankur Verma

Answered On : 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.  

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Question Categories

Data Modeling Interview Questions

Data Structures Interview Questions

OLAP Interview Questions

OOPS Interview Questions

UML Interview Questions

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.