I am learning C and I am trying to flip a string that contains
"hello" into "olleh"
how can I do that?
I am learning C and I am trying to flip a string that contains
"hello" into "olleh"
how can I do that?
There are many ways.... why not try and think of some yourself first
Remember that the string is just an array of chars......
And remember that many people have asked the question before you, so the answer is going to be lying around somewhere.
Have a go, and post some code when you are stuck.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
start at the end of the string and work your way towards the beginning, printing off characters as you go.
-Govtcheez
[email protected]
Just make sure that 'in' buffer is long as 'out' bufferCode:int flip_str_to_str(char *in,char *out) { char *pin,*pout; int len = strlen(in) ; pin = in ; pout = out+len; *(pout+1) = '\0'; while( *pin) { *pout = *pin; pout--; pin++; } return 0; }
or else out is going to ring the bell !!
Is there a way to do it in a much simpler way..I just started to learn C soPin iin/out is too advanced for me now!!!!
I want to find a way to rotate the string without having printf to do it!!!
If this is homework, they'll likely not accept this answer, unless of course, you can actually explain what it does.Code:void r(char*s) { if(s&&*s!='\0') { r(s+1); printf("%c",*s); } }
Quzah.
Hope is the first step on the road to disappointment.
Haven't checked this one... (there could be mistakes
with strlen in my code..)
This is just to give you an idea.Code:char input[] = "My Homework"; for (i=strlen(input)-1; i>=0; i--) { printf("%c", input[i]); }
COOL PROGRAMS @ www.akilla.tk
oh, and also
int i
and all that..
Good luckCode:char ISay[] = "Can't expect me to do all your homework";
CHECK OUT MY NUMEROLOGY PROGRAM
(Tells Lucky days, Lucky Colors, etc.) www.akilla.tk