Using C, how to write a program to reverse a string without using loop and recursion.
Printable View
Using C, how to write a program to reverse a string without using loop and recursion.
hmm... could you clarify what you mean?
You're asking for an algorithm to reverse the characters of an arbitrary string, without using iteration or recursion?
Yes.Quote:
Originally posted by laserlight
hmm... could you clarify what you mean?
You're asking for an algorithm to reverse the characters of an arbitrary string, without using iteration or recursion?
Welcome to reality. No one here is going to do your home work. Perhaps you should actually read the forum rules?Quote:
Originally posted by sultan raja
Yes.
Quzah.
Easy money - though I don't think you're going to like it.
#include <std_pause_for_dramatic_effect.h>
Well, you can use strrev from string.h if your compiler supports it. But that's probably not what you had in mind :rolleyes:
If the string is always the same length there is a way to code it yes.
If the string is different lengths each time I'm not experienced enough to help you.
>without using loop and recursion
How are you allowed to define loops? :)
what is the string? :oQuote:
Originally posted by sultan raja
Using C, how to write a program to reverse a string without using loop and recursion.
I'm new to this stuff too, but I have done some exercises with reversing digits, which could be done using remainder operator (%) or you could try this:
printf("%d%d%d", a, b ,c)
reversed:
printf("%d%d%d", c, b, a)
no loop and recursion used i believe...:o :o :o
Is it safe to post my answer yet....
safe = homework deadline passed ;)
Not the most elegant, but at least no obvious loops :)Code:#include <stdio.h>
#include <string.h>
int main () {
char string[100];
size_t len;
scanf("%s",string);
len = strlen( string );
switch ( len ) {
case 10: putchar( string[len--] );
case 9: putchar( string[len--] );
case 8: putchar( string[len--] );
case 7: putchar( string[len--] );
case 6: putchar( string[len--] );
case 5: putchar( string[len--] );
case 4: putchar( string[len--] );
case 3: putchar( string[len--] );
case 2: putchar( string[len--] );
case 1: putchar( string[len--] );
case 0: putchar( string[len ] );
}
putchar( '\n' );
return 0;
}
of course strlen has a loop in it... :)
The problem is with the "arbitrary string" part.
We dont know how long the string will be.
If we did know, we can manually code it in.
I wonder if sultan raja was given a trick question and misinterpreted it.
After all, it is possible to reverse an arbitrary string using iteration XOR recursion, thus satisfying not using iteration AND recursion
:D
are you allowed to use goto?
:D
What about about using a stack.
Push a letter down on the stack every letter that gets entered.
When input is done just read the stack from the top to bottom.