Just wodering C code for reversing a sentence. I searched at this board but could not find it. Anybody knows where else can I find it?
For example.
If I input,
change is truth of life
output should be
life of truth is change
Please let me know.
Just wodering C code for reversing a sentence. I searched at this board but could not find it. Anybody knows where else can I find it?
For example.
If I input,
change is truth of life
output should be
life of truth is change
Please let me know.
Break it down into steps:
Read in the sentence
Identify were the words change
reverse them
functions that might help:
fgets()
strtok()
Other hints:
Using multiple arrays
Possibliy using a link list to store the location of each of the words after strtok()
You might want to look up the strlen function as well, that could come in handy.
Warning: Opinions subject to change without notice
The C Library Reference Guide
Understand the fundamentals
Then have some more fun
>Using multiple arrays
Why? This can be done inplace.
>Possibliy using a link list to store the location of each of the words after strtok()
Overkill. The inplace algorithm is shorter and easier to understand.
>Do a board search
He said he already did even though I know this question has be posed many times before. I've answered it quite a bit that I can remember.
>You might want to look up the strlen function as well, that could come in handy.
Maybe. My implementation didn't require any string.h functions at all.
>Just wodering C code for reversing a sentence.
Is this homework? If so, coming up with your own solution would be a much better use of your time. Here's a hint: Reverse the entire sentence first, then walk along the reversed sentence marking the beginning and end of each word and reverse those. The end result will be what you want. A quick paper run works as proof:
change is truth of life
efil fo hturt si egnach
life fo hturt si egnach
life of hturt si egnach
life of truth si egnach
life of truth is egnach
life of truth is change
Now for the easy part, writing an implementation in C.
My best code is written with the delete key.
I am aware that the OP claimed to have done a board search, but seriously, what were the search terms? They could not have been 'reverse a string' or 'reversing a string'Originally Posted by Prelude
~/
>but seriously, what were the search terms?
The same as everyone else who claims that a search was fruitless: "solve my problem for me"
My best code is written with the delete key.
Here you go...
Any changes/optimizations are welcome...
Code:#include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> #define DELIM ' ' #define NLINE '\n' #define EXCHANGE(a,b) \ a = a^b; \ b = b^a; \ a = a^b; void my_reverse(char *p,char *q); int main(void) { char string[100]; char *p,*walk; printf("Enter a string: "); fflush(stdout); if ( fgets(string,sizeof(string),stdin) != NULL ) { if ( ( p = strchr(string,'\n')) != NULL ) printf("\n<%s>",string); else exit(1); } else { printf("Error\n"); exit(1); } my_reverse(string,(p-1)); printf("\n<%s>",string); walk = string; while ( walk < p ) { char *sep; if ( ( (sep = strchr(walk,DELIM)) != NULL ) || ( (sep = strchr(walk,NLINE)) != NULL ) ) { my_reverse(walk,sep-1); while( sep++ == DELIM ) ; walk = sep; } else break; } printf("\n<%s>",string); return 0; } void my_reverse(char *p,char *q) { int i; i = (q-p); if (i%2) i = i/2+1; else i = i/2; while ( i-- ) { EXCHANGE(*p,*q); p++; q--; } }
Code:itsme@dreams:~/C$ cat strrev.c #include <stdio.h> #include <string.h> #include <stdlib.h> int main(int argc, char **argv) { char buf[4096], *p; size_t len; if(argc != 2) { puts("Usage: strrev <message>"); exit(EXIT_FAILURE); } if((len = strlen(argv[1])) >= sizeof(buf)) { puts("Message too long!"); exit(EXIT_FAILURE); } strcpy(buf, argv[1]); for(p = buf+len;p >= buf;--p) if(*p == ' ') { *p = '\0'; printf("%s ", p+1); } puts(p+1); return 0; }EDIT: Changed len to type size_titsme@dreams:~/C$ ./strrev 'I think this code is a bit more understandable'
understandable more bit a is code this think I
Last edited by itsme86; 08-29-2004 at 08:12 PM.
If you understand what you're doing, you're not learning anything.
is there any possible to solve it using recursion and without string.h?
i tried to solve using recursion, but failed.
This a really bad way to do a swap. Search the boards and you'll find plenty of threads discussing it.#define EXCHANGE(a,b)\
a = a^b; \
b = b^a; \
a = a^b;
Code:$ cat strrev2.c #include <stdio.h> void rev_it(char *str) { char *s; char buf[100], *b; for(s = str, b = buf;*s && *s != ' ';s++, b++) *b = *s; *b = '\0'; if(*s) rev_it(s+1); printf("%s ", buf); } int main(void) { char *str = "reverse this thing for me"; rev_it(str); printf("\n"); return 0; }Please ask if you have any questions on why/how that works. It's important for you to understand the concept of recursive functions.$ ./strrev2
me for thing this reverse
Last edited by itsme86; 08-30-2004 at 12:35 AM.
If you understand what you're doing, you're not learning anything.
Meh I'm bored so here goes:
It uses an in place solution and pretty much does exactly what Prelude said, reverse the entire thing and then reverse each word. It avoids using an extra array and avoids using recursion since IMO recursion is not the best for this.
Code:#include <stdio.h> #include <string.h> char * wordBasedStringRev(char *, int); char * myStringRev(char *, char *); void swap (char *, char *); int main(void) { char arr[] = "Hello my name is Thantos how are you today"; puts("Before"); puts(arr); wordBasedStringRev(arr, sizeof arr -1); puts("After"); puts(arr); return 0; } char * wordBasedStringRev(char *str, int len) { char *p=str, *q=NULL; myStringRev(str, str+len-1); while ( (q = strchr (p, ' ')) != NULL) { myStringRev(p,q-1); p = q + 1; } myStringRev(p, (str+len)-1); return str; } char * myStringRev(char *beg, char *end) { int offset; for ( offset = 0; offset < ((end - beg) / 2)+1; offset++) swap (beg+offset, end-offset); return beg; } void swap (char *a, char *b) { char c = *a; *a = *b; *b = c; }
You did huh? Are you sure about that?Originally Posted by Roaring_Tiger
Quzah.
Hope is the first step on the road to disappointment.