Using C, how to write a program to reverse a string without using loop and recursion.
Using C, how to write a program to reverse a string without using loop and recursion.
Last edited by sultan raja; 11-09-2003 at 04:41 AM.
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.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?Originally posted by sultan raja
Yes.
Quzah.
Hope is the first step on the road to disappointment.
Easy money - though I don't think you're going to like it.
#include <std_pause_for_dramatic_effect.h>
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Well, you can use strrev from string.h if your compiler supports it. But that's probably not what you had in mind
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.
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?
My best code is written with the delete key.
what is the string?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...
Is it safe to post my answer yet....
safe = homework deadline passed
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Not the most elegant, but at least no obvious loopsCode:#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; }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
of course strlen has a loop in it...
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
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
are you allowed to use goto?
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.