Word, string flip, stack implementation!

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.