flips the string "This is very good", which means "good very is this". Note that each word is still a positive sequence
Ideas:
1. The problem of flipping strings. Ability to use stack implementations.
2. Turn all the characters upside down, then flip the word to another, and finally realize the problem.
C language code such as the following:
The simple stack implementation is listed first:
#ifndef datastruct_mystack_h#define datastruct_mystack_h#define kstacksize 100typedef struct{ void* entity[ Kstacksize]; int top; } Mystack;mystack* initstack (); void push (mystack* s,void* p); void* pop (mystack* s); void Freestack (mystack* s); #endif
#include <stdlib.h> #include "myStack.h"//mystack* Initstack () { mystack* s = (mystack*) malloc (sizeof ( Mystack)); memset (s->entity,0,sizeof (kstacksize)); S->top =-1; return s;} void push (mystack* s,void* p) { if (S->top < kStacksize-1) { s->entity[++s->top] = p; }} void* Pop (mystack* s) { if (s->top>=0 && s->top < kstacksize) { return s->entity[s-> top--]; } return NULL;} void Freestack (mystack* s) {free (s);}
A detailed call.
int main {//0. Output original string char* str = "This is very good";p rintf ("%s\n", str); char* reversal= (char*) calloc (1, 100); char* result= (char*) calloc (1, 100); int length=0; 1. Flip All Strings reverstring (Str,strlen (str), reversal); printf ("%s\n", reversal); 2. Flip the word char* index = reversal; char* reversalptr = reversal; char* resultptr = result; while (*index) {if (*index = = ") {reverstring (reversalptr,length,resultptr); Reversalptr= index+1; Resultptr + = length; * (resultptr++) = '; length = 0; } else {length++; } index + +; }//3. The last word. Flip it. Reverstring (REVERSALPTR,LENGTH,RESULTPTR); printf ("%s", result); 4. Clean up data free (reversal); Reversalptr=null; Free (result); result=NULL; }void reverstring (char* str,int length,char*buffer) {if (length<=0) {return; } mystack * stack = Initstack (); int i=0; while (i<length) {push (STACK,STR); str++; i++; }//1. Overall rollover over char* tmp; while (TMP = (char*) pop (stack)) {*buffer = *tmp; buffer++; } freestack (stack);}
The output is:
This is very good
Doog Yrev si siht
Good very is this
Word, string flip, stack implementation!