C language interview questions solution for freshers beginners placement tricky good pointers answers explanation operators data types arrays structures functions recursion preprocessors looping file handling strings switch case if else printf advance linux objective mcq faq online written test prime numbers Armstrong Fibonacci series factorial palindrome code programs examples on c++ tutorials and pdf
Reverse a string using recursion in c
Sum of n numbers using recursion in c
Matrix multiplication using recursion in c
Multiplication using recursion in c
Lcm using recursion in c
Using recursion in c find the largest element in an array
Prime number program in c using recursion
Decimal to binary conversion in c using recursion
C program for fibonacci series using recursion
Reverse a string using recursion
Write a program for palindrome using recursion
Find factorial of a number using recursion in c program
Find gcd of a number using recursion in c program
Find sum of digits of a number using recursion using cprogram
Find power of a number using recursion using c program
Binary search through recurssion using c program
Reverse a number using recursion in c program
Big list of c program examples
Subscribe to:
Post Comments (Atom)
14 comments:
Local character array cannot return from any function. That have function scope only...
Local character array cannot return from any function. That have function scope only..
Any how this is static variable. Take memory in data segment only, but unallocate memory cannot used and string reverse must done in same string only..
how can I reverse a string including spaces with recursion or not?
Plz explain step wise, the working of function 'get Reverse'. I am a beginner
thanx
#include
void reverse(char *str, char *res)
{
static idx = 0;
if ( *str != '\0')
{
reverse(str+1, res);
res[idx++] = *str;
}
return;
}
int main()
{
char str[100];
reverse("binoj", str);
printf("%s", str);
}
char * rec_rev_str(char * str) {
if(*str != '\0'){
char * ret = (char *)malloc(strlen(str));
char * tmp = rec_rev_str(str+1);
if(tmp){
strcpy(ret, tmp);
free(tmp);
tmp = NULL;
strncat(ret, str, 1);
} else {
strncat(ret, str, 1);
}
return ret;
}
return NULL;
}
A recusive way to reverse the same string:
void reverse(char str[], int l, int h)
{
if(l < h){
swap(&str[l], &str[h]);
reverse(str, l+1, h-1);
}
}
#include
int main()
{
char str[50];
void rev_str(char *ptr);
fgets(str,50,stdin);
printf("Reverse of string\n");
rev_str(str);
}
void rev_str(char *ptr)
{
char i;
if(*ptr)
{
i=*ptr;
ptr++;
rev_str(ptr);
printf("%c",i);
}
}
the simple method to reverse input using recursion:
main()
{
char ch=getchar();
if(ch!='\n')
main();
printf("%c",ch);
}
Recursion finished by ch have '\n',so the function return to calling function,After printing the value of ch.
so it print the last character in begin,and each characters are printed before each function returns.
hi , this code suppose to be likeur last recursion example : #include
void print_reverse() {char c;
scanf("%c", &c); if (c == '\n') return;
print_reverse();
putchar(c);}
int main() {
printf("Input a line\n");
print_reverse();
printf("\n"); return 0; }
I still dont get\ understand something about the way recursion works, can u please clarify ?
1 when ur getting the ch by getchar or scanf the fun is getting 1 char each time ? where is it saved? or does it save a string ?
2 how\ what is the flow the reverse the string ? or is it printing 1 char each time from the end ? thanks
#include
void reverse();
int main()
{
reverse();
return 0;
}
void reverse()
{
char c;
scanf("%c",&c);
if(c!='\n')
{
reverse();
printf("%c",c);
}
}
very nice proram
Post a Comment