ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Do you have a collection of Interview Questions and interested to share with us!!
Please send that collection to along with your userid / name. ThanQ
Google
 
Categories  >>  Software  >>  Programming Languages  >>  C
 
 


 

 
 C interview questions  C Interview Questions
 C++ interview questions  C++ Interview Questions
 VC++ interview questions  VC++ Interview Questions
 Delphi interview questions  Delphi Interview Questions
 Programming Languages AllOther interview questions  Programming Languages AllOther Interview Questions
Question
How to reverse a string using a recursive function, without
swapping or using an extra memory?
 Question Submitted By :: Guest
I also faced this Question!!     Rank Answer Posted By  
 
  Re: How to reverse a string using a recursive function, without swapping or using an extra memory?
Answer
# 1
/* Following code does as intended */
#include <stdio.h>

#define REVERSE_STRING(X) Rstring(X, *(X), strlen(X)-1)

void Rstring( char *str, char c, int index )
{
	if( index != 0 )
		Rstring( str, *(str+(strlen(str))-index), 
index-1);	
	*(str+index) = c;		
}

int main( void )
{
	char str[] = "Dharmendra Patel";
	printf("Actual string is [%s]\n", str);
	REVERSE_STRING(str);
	printf("Reversed string is [%s]\n", str);
	return 0;
}
 
Is This Answer Correct ?    21 Yes 8 No
D G Patel
 
  Re: How to reverse a string using a recursive function, without swapping or using an extra memory?
Answer
# 2
void reverse(char *,int b);
void main()
{
char a[26];
int len;
clrscr();
printf("enter string ");
gets(a);
len=strlen(a);
reverse(a,len);
getch();
}
void reverse(char * a,int len)
{
  if(len==0)
  printf("%c",a[len]);
  else
  {
  printf("%c",a[len]);
  reverse(a,len-1);
  }
}
 
Is This Answer Correct ?    21 Yes 13 No
Vinay Tiwari
 
 
 
  Re: How to reverse a string using a recursive function, without swapping or using an extra memory?
Answer
# 3
void Rstring( char *str,int len)
{
	for(int i = 0; i < (len/2); i++)
	{
		str[i] ^= str[len-i-1];
		str[len-i-1] ^= str[i];
		str[i] ^= str[len-i-1];
	}
}

int main( void )
{
	char str[] = "my string";
	printf("Actual string is [%s]\n", str);
	Rstring(str,strlen(str));
	printf("Reversed string is [%s]\n", str);
	
}

I dont call this swaping, coz it's not, recursive creates 
new incarnations of the reverse func, EXTRA MEMORY BIG 
TIME!!!
 
Is This Answer Correct ?    10 Yes 12 No
Boomer
 
  Re: How to reverse a string using a recursive function, without swapping or using an extra memory?
Answer
# 4
#include <iostream>
#include <conio>

void reverse(char a[], int s, int sc );

void reverse(char a[], int s, int sc ){

if ((sc-s)<(s-1))
{
a[sc-s]^=a[s-1];
a[s-1]^=a[sc-s];
a[sc-s]^=a[s-1];
reverse (a, s-1, sc) ;

}

}

void main (){


char a[]="ABCDEFG";

reverse(a, 7, 7);
cout<<a;
getch(); //i just use it to freeze the screen

}
 
Is This Answer Correct ?    4 Yes 4 No
Moinom
 
  Re: How to reverse a string using a recursive function, without swapping or using an extra memory?
Answer
# 5
#include <iostream>


using namespace std;


char* reverse_str(char* s)
{
	char* reverse = new char[1];
	//char* reverse;


	int i;

	if(*s != '\0')
		reverse = reverse_str(s+1);

	i = strlen(s) - 1;

	if (i >= 0)
		reverse[i] = s[0];

	return reverse;
}


int main(void)
{
	char* str = "tsirhc oraivur odraude leafar";

	
	cout << "original:" << endl;
	cout << str << endl << endl;
	
	cout << "reversed:" << endl;
	cout << reverse_str(str) << endl;
	
	return 0;
}
 
Is This Answer Correct ?    3 Yes 9 No
Rafael Christ
 
  Re: How to reverse a string using a recursive function, without swapping or using an extra memory?
Answer
# 6
/* 
  reverse string between start and end indexes of a string 
*/

void reverse( char* str, int start, int end )
{
 if( str && ( start < end ) )
 {
   *( str + start ) ^= *( str + end  ) ^=  *( str + start )
^= *( str + end ) ;
   reverse( str, ++start, --end ); 
 }
}

int main()
{
  char sample[] = "My String!";
  reverse( str, 0, strlen( sample )-1 )
}
 
Is This Answer Correct ?    5 Yes 3 No
Pritam
 
  Re: How to reverse a string using a recursive function, without swapping or using an extra memory?
Answer
# 7
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main(void)
{

 char str1[] = "Mahesh";
 char str2[80], *p1, *p2;

 clrscr();

 p1 = str1 + strlen(str1) - 1;

 p2 = str2;

 while(p1 >= str1)
  *p2++ = *p1--;

 *p2 = '\0';

 printf("%s %s", str1, str2);

 getch();

 return 0;
}
 
Is This Answer Correct ?    6 Yes 4 No
Mahesh Auti
 
  Re: How to reverse a string using a recursive function, without swapping or using an extra memory?
Answer
# 8
#include<stdio.h>
#include <string.h>

char * reverse (char *);		//function prototype
int length(char *);                     //function prptotype

void main()
{
    int i;
    char *str, *rev;
    clrscr();
    gets(str);
    strcpy(rev,reverse(str));

    printf("Original %s     Reverse %s", str, rev);
    free(str);
    free(rev);
    getch();
}

int length(char *s)
{
    int i;
    for (i=0; *(s+i)!='\0' ; ++i);
    return i;
}

char *reverse(char *s)
{
   char *t;
   int i, n;
   n=length(s);
   for (i=0; i<n; ++i)
   {
       *(t+i)=*(s+n-1-i);
   }
   *(t+i)='\0';
   printf("\nOUT: %s\n", t);
   return t;
}
 
Is This Answer Correct ?    1 Yes 10 No
Mahesh Auti
 
  Re: How to reverse a string using a recursive function, without swapping or using an extra memory?
Answer
# 9
Reverse a string

void ReverseString (char *String)
{
char *Begin = String;
char *End = String + strlen(String) - 1;
char TempChar = '\0';

while (Begin < End) 
{
TempChar = *Begin;
*Begin = *End;
*End = TempChar;
Begin++;
End--;
}
}
 
Is This Answer Correct ?    10 Yes 6 No
Mahendra Aseri
 
  Re: How to reverse a string using a recursive function, without swapping or using an extra memory?
Answer
# 10
Using recursive Function:

void rev_str(char *str)
{
	if(*str!=NULL)

  	rev_str(str+1);   

  printf("%c",str);
}
 
Is This Answer Correct ?    4 Yes 11 No
Mahendra Aseri
 
  Re: How to reverse a string using a recursive function, without swapping or using an extra memory?
Answer
# 11
&#65279;#include <iostream>

using namespace std;

void rev_str(char* str, int pos=-1, char c='\0'){
    if(pos >= int(strlen(str)/2))
	return;
    if(c != '\0'){
	str[strlen(str) - pos - 1] = str[pos];
	str[pos] = c;
    }
    rev_str(str, ++pos, str[strlen(str) - pos - 2]);
}

int main(){
    char str[] = "reverse this string";
    cout << str << endl;
    rev_str(str);
    cout << str << endl;
    //:~
    return 0;
}
 
Is This Answer Correct ?    2 Yes 3 No
Smbrd
 
  Re: How to reverse a string using a recursive function, without swapping or using an extra memory?
Answer
# 12
#include <iostream>
#include <string>
 using namespace std;
char * reverse (char *);		//function prototype
int length(char *);                     //function prptotype
 
int main()
{
    int i;
    char *str = new char[6], *rev = new char[6];
    cin >> str;
    strcpy(rev,reverse(str));

	cout <<"Original "<< str << " reverse " << rev << endl;
    free(str);
    free(rev);
	return 0;
}

int length(char *s)
{
    int i;
    for (i=0; *(s+i)!='\0' ; ++i);
    return i;
}

char *reverse(char *s)
{
   char *t;
   int i, n;
   n=length(s);
   t = new char[n];
   for (i=0; i<n; ++i)
   {
       *(t+i)=*(s+n-1-i);
   }
   *(t+i)='\0';
   
   return t;
}

/*based off of answer 8 i took this an intialized the
pointers so that it would run, and switched it over to the
C++ standard output commands. His algorithm was correct, he
just forgot to setup the memory*/
 
Is This Answer Correct ?    3 Yes 4 No
Stephen
 
  Re: How to reverse a string using a recursive function, without swapping or using an extra memory?
Answer
# 13
#include <iostream>
#include <string>
 using namespace std;
char * reverse (char *);		//function prototype
int length(char *);                     //function prptotype
 
int main()
{
    int i;
    char *str = new char[6], *rev = new char[6];
    cin >> str;
    strcpy(rev,reverse(str));

	cout <<"Original "<< str << " reverse " << rev << endl;
    free(str);
    free(rev);
	return 0;
}

int length(char *s)
{
    int i;
    for (i=0; *(s+i)!='\0' ; ++i);
    return i;
}

char *reverse(char *s)
{
   char *t;
   int i, n;
   n=length(s);
   t = new char[n];   //opps have to add 1 here or there
wont be room for a null! 
   for (i=0; i<n; ++i)
   {
       *(t+i)=*(s+n-1-i);
   }
   *(t+i)='\0';
   
   return t;
}
//can only handle words 5 letters or less.
/*based off of answer 8 i took this an intialized the
pointers so that it would run, and switched it over to the
C++ standard output commands. His algorithm was correct, he
 
Is This Answer Correct ?    4 Yes 3 No
Stephen
 
  Re: How to reverse a string using a recursive function, without swapping or using an extra memory?
Answer
# 14
#include <stdio.h>

void reverse(char *str)
{
	if (*str == '\0')
		return;

	reverse(str+1);

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

int main()
{
	char str[50];

	printf("Enter the string: ");
	scanf("%s", str);

	printf("Reversed string: ");
	reverse(str);
	printf("\n");

	return 1;
}
 
Is This Answer Correct ?    5 Yes 9 No
Prakash
 
  Re: How to reverse a string using a recursive function, without swapping or using an extra memory?
Answer
# 15
Another version that actually reverses the string...

#include <stdio.h>

char *reverse(char *sstr, char *str, char c)
{
	if (*str == '\0')
		return sstr;

	sstr = reverse(sstr, str+1, *(str+1));

	*sstr = c;

	return (sstr+1);
}

int main()
{
	char str[100];

	printf("Enter the string: ");
	scanf("%s", str);

	reverse(str, str, *(str + 0));
	printf("Reversed string: %s\n", str);

	return 1;
}
 
Is This Answer Correct ?    11 Yes 1 No
Prakash
 
  Re: How to reverse a string using a recursive function, without swapping or using an extra memory?
Answer
# 16
main()
 {
  char str[10];
  cin>>str;
  int len=strlen(str);
  reverse(len);
  cout<<"Reversed string is: "<<str;
 }

void reverse(int len)
 {
  static int i=0;
  str[i]=str[len-1]; //put the char in last pos to first pos
  for(j=len-1;j>i;j--)
   str[j]=str[j-1]; //shift to right
  i++;
  
  if(i==len)
   return;
  
  reverse(len);
 }
 
Is This Answer Correct ?    1 Yes 4 No
Shivaraj
 
  Re: How to reverse a string using a recursive function, without swapping or using an extra memory?
Answer
# 17
void reverse_string(char *string) {

    static int start_index = 0;
    static int end_index = strlen(string) - 1;

    if (start_index <= end_index) {
        char temp = string[end_index];
        string[end_index] = string[start_index];
        string[start_index] = temp;
        start_index++;
        end_index--;
        reverse_string(string);
    }
}
 
Is This Answer Correct ?    2 Yes 3 No
Siva Kumar
 
  Re: How to reverse a string using a recursive function, without swapping or using an extra memory?
Answer
# 18
no ansawer
 
Is This Answer Correct ?    1 Yes 0 No
Fgy
 

 
 
 
Other C Interview Questions
 
  Question Asked @ Answers
 
What character terminates all strings composed of character arrays? 1) 0 2) . 3) END  3
hi how to convert program from notepad to turboc editor can u please help me  1
User define function contain thier own address or not.  2
what is op? for(c=0;c=1000;c++) printf("%c",c); Trigent18
what will be the result of the following program ? char *gxxx() { static char xxx[1024]; return xxx; } main() { char *g="string"; strcpy(gxxx(),g); g = gxxx(); strcpy(g,"oldstring"); printf("The string is : %s",gxxx()); } a) The string is : string b) The string is :Oldstring c) Run time error/Core dump d) Syntax error during compilation e) None of these IBM2
WHAT IS THE DIFFERANCE BITWIN GETS();AND SCANF();  2
what is the difference between normal variables and pointer variables.............. Satyam7
Convert the following expression to postfix and prefix X $ Y Z - M + N + P / Q / (R + S)  2
why should i select you? Wipro18
plz answer..... a program that reads non-negative integer and computes and prints its factorial  2
let's take a code struct FAQ { int a; char b; float c; double d; int a[10]; }*temp; now explain me how the memory will be allocated for the structure FAQ and what address will be in the structure pointer (temp)....................  7
f(char *p) { p=(char *)malloc(sizeof(6)); strcpy(p,"HELLO"); } main() { char *p="BYE"; f(p) printf("%s",p); } what is the output? Hughes6
Given a single Linked list with lakhs of nodes and length unknown how do you optimally delete the nth element from the list? Oracle1
hat is a pointer? Assurgent3
Here is alphabets : abcdefgh 1) how to reverse. as hgfedcba 2) after reversal, how to group them in a pair hg fe dc ba.  2
int i =10 main() { int i =20,n; for(n=0;n<=i;) { int i=10 i++; } printf("%d", i); HCL5
#include<stdio.h> int fun(); int i; int main() { while(i) { fun(); main(); } printf("hello \n"); return 0; } int fun() { printf("hi"); } answer is hello.how??wat is tat while(i) mean? Intel4
main() { char *p; p="Hello"; printf("%c\n",*&*p); } ME2
any string of bits of length 'n' represents a unique non- negative integer between.............?  2
What are Storage Classes in C ? HP15
 
For more C Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com