String flip, stack implementation!

Source: Internet
Author: User

Tag: Stack string flip

Flip the string "this is very good", that is, "Good very is this ";Note that each word is still in the forward order.

Ideas:

1. stack implementation can be used to flip strings.

2. Flip all characters first, and then flip a word to meet the question requirements.

The C language code is as follows:

First, list the simple stack implementation:

 
# Ifndef datastruct_mystack_h # define kstacksize 100 typedef 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 );}

Specific call.

Int main {// 0. output the original string char * STR = "this is very good"; printf ("% 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, res Ultptr); reversalptr = index + 1; resultptr + = length; * (resultptr ++) = ''; length = 0 ;}else {length ++ ;} index ++;} // 3. flip the last word. Reverstring (reversalptr, length, resultptr); printf ("% s", result); // 4. clear data free (reversal); reversalptr = NULL; free (resultptr); resultptr = 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. char * TMP; while (TMP = (char *) Pop (stack) {* buffer = * TMP; buffer ++;} freestack (stack );}

The output result is:

This is very good

Doog yrev Si siht

Good very is this


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.