天天看点

c语言字符串反转利用数组,【C语言】利用栈将数组中字符串逆序

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#include"stdio.h"

#include"stdlib.h"

#define STACK_INIT_SIZE 100

#define STACKINCREMENT 10

typedef struct

{char *base;

char *top;

int stacksize;

}SqStack;

main()

{SqStack S;

char a[4];

int i;

InitStack(&S);

printf("请输入字符:\n");

for(i=0;i<4;i++)

scanf("%c",&a[i]);

for(i=0;i<4;i++)

Push(&S,a[i]);

for(i=0;i<4;i++)

Pop(&S,&a[i]);

for(i=0;i<4;i++)

printf("%c",a[i]);

}

int InitStack(SqStack *S)

{S->base=(char *)malloc(STACK_INIT_SIZE * sizeof(char));

if(!S->base) return 0;

S->top=S->base;

S->stacksize=STACK_INIT_SIZE;

return 1;

}

int Push(SqStack *S,char e)

{if(S->top-S->base>=S->stacksize)

{S->base=(char *)realloc(S->base,(S->stacksize+STACKINCREMENT) * sizeof(char));

if(!S->base) return 0;

S->top=S->base+S->stacksize;

S->stacksize+=STACKINCREMENT;

}

*S->top++=e;

return 1;

}

int Pop(SqStack *S,char *e)

{if(S->top==S->base) return 0;

*e=*--S->top;

return 1;

}