how to change "C is fun!" to "C si !nuf"..
how to change "C is fun!" to "C si !nuf"..
get the size of the string ex: "C is fun!\0" size = 10
then just loop through the character array starting from the end of the string (less NULL) copying character by character to another array
then append '\0'
i need to change from "C is fun!" to "C si !nuf"..
not to "!nuf si C"
note the diff?
What have you tried so far? Post some code and someone will guide you.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
i tried breaking the words into 3...
then strrev() them all...but how to break the 3 words and store them...by searching for the space ' '?...
strrev() is non-standard, so I don't know if you want to use it or not....
Personally, I'd do something like this:
Create a function called reverseme that takes 2 parameters, a char* and an int, prototyped like so:
>>char *reverseme(char *s, int len);
Make this function reverse the chars pointed to by s for a length of len. Don't make this function rely on nul terminated strings or anything like that, just make it reverse chars for that length.
Then make another function, this will drive the reverseme one. Within this function, loop though the input string to find the first occurence of a space. Then call reverseme(), passing it a pointer to the start of the word, and the length of the word. Once reverseme has reversed that word, simply move your pointer past the space, and repeat the process, until you reach \0.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
You could always do it the easy way and use recursion...
Have fun.Code:#include<stdio.h> int q( char *u ) { int z=u==NULL?0:*u=='\0'?0:*u==' '?0:q(u+1)+1; if(z) putchar(*u); return z; } int main ( void ) { char*u="C is fun!",*z=u; int a=0; while(z&&*z) { a=q(z); z+=a; if(*z==' ') { putchar(' '); z++; } } return 0; }
Quzah.
Hope is the first step on the road to disappointment.
>> int z=u==NULL?0:*u=='\0'?0:*u==' '?0:q(u+1)+1;
It hurts my eyes just trying to read that... lol.
Cant stand professionally coded stuff... im such a noob
wat does that means..i forgotten...
Another recursive example:Code:#include<stdio.h> #define SP ' ' char*p(char*t) { char*s; t&&*t&&*t!=SP&&((s=p(t+1))==s)&&putchar(*t); return(t&&*t?(*t==SP?t:s):NULL); } int main(void) { char*t="C is fun!"; while(t=p(t))putchar(*t++); }
wow..
i need some explainations on tat...
is the ugly (I mean short ; ) way of writing (more or less)Code:int z=u==NULL?0:*u=='\0'?0:*u==' '?0:q(u+1)+1;
Code:int z; if (u == NULL) z = 0; else if (*u == '\0') z = 0; else if (*u == ' ') z = 0; else z = q(u+1)+1;
"He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson
Hi Monster,
I appreciate the peice of code you wrote,
But i think there is no need of so many logical AND's.
the function p as follows will also work
Beucase the varaible passed to the function p is already initialized. Its not NULL.Code:char*p(char*t) { char*s; *t&&*t!=SP&&((s=p(t+1))==s)&&putchar(*t); return(*t?(*t==SP?t:s):NULL); }
Pls. correct me if i am strong.
"There is no alternative to consitency..."
Hi gotyatech,
Yes in this case you are right, but what if I wrote this function as part of a DLL and someone else called this function with a NULL pointer? Always expect the unexpected...
b.t.w. the code is crap and a perfect example of bad programming... but you already knew that...
>>You could always do it the easy way and use recursion...
The recursive solution isn't easy enough to be worth using recursion compared to the iterative solution :-)
Code:#include <stdio.h> #include <string.h> void reverse_substr(char *string, size_t length) { int start, end; for (start = 0, end = length-1; start < end; start++, end--) { char swap = string[start]; string[start] = string[end]; string[end] = swap; } } char *reverse_words(char *str) { int length; char *p = str, *next; for (p = str; p != 0; p = strpbrk(p, " ")) { if (*p == ' ') { p++; } next = strpbrk(p, " "); length = (next != 0) ? next - p : &str[strlen(str)] - p; reverse_substr(p, length); } return str; } int main(void) { char str[] = "C is fun!"; printf("%s\n", reverse_words(str)); return 0; }
*Cela*