这是一个c程序,用于反转句子中的单词(使用堆栈)作为输入读取的顺序,但我能得到的只是句子中每个单词的反转。如何撤销句子(用''或','分隔)?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
// A structure to represent a stack
struct Stack
{
int top;
unsigned capacity;
char* array;
};
struct Stack* createStack(unsigned capacity)
{
struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
stack->capacity = capacity;
stack->top = -1;
stack->array = (char*) malloc(stack->capacity * sizeof(char));
return stack;
}
int isFull(struct Stack* stack)
{ return stack->top == stack->capacity - 1; }
// Stack is empty when top is equal to -1
int isEmpty(struct Stack* stack)
{ return stack->top == -1; }
// Function to add an item to stack. It increases top by 1
void push(struct Stack* stack, char item)
{
if (isFull(stack))
return;
stack->array[++stack->top] = item;
}
char pop(struct Stack* stack)
{
if (isEmpty(stack))
return 0;
return stack->array[stack->top--];
}
// A stack based function to reverese a string
void reverse(char str[])
{
int n = strlen(str);
struct Stack* stack = createStack(n);
// Push all characters of string to stack
int i;
for (i = 0; i < n; i++)
push(stack, str[i]);
for (i = 0; i < n; i++)
str[i] = pop(stack);
}
int main()
{
char str[50];
fgets(str, sizeof(str), stdin);
str[strlen(str)-1]='\0';
reverse(str);
printf("Reversed string is %s", str);
return 0;
}
答案 0 :(得分:0)
试试此代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
// A structure to represent a stack
struct Stack
{
int top;
unsigned capacity;
//char* array;
char** array;
};
struct Stack* createStack(unsigned capacity)
{
if( capacity < 1 )
{
capacity = 1;
}
struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
stack->capacity = capacity;
stack->top = -1;
stack->array = (char**) malloc(stack->capacity * sizeof(char*));
return stack;
}
void resizeStack( struct Stack* stack , int new_size )
{
if( NULL != stack && new_size > stack->capacity ) // Only support expansion
{
char ** old_array = stack->array ;
stack->array = (char**) malloc(new_size * sizeof(char*));
memcpy(stack->array,old_array,stack->capacity * sizeof(char*));
free(old_array);
stack->capacity = new_size ;
}
}
int isFull(struct Stack* stack)
{ return stack->top == stack->capacity - 1; }
// Stack is empty when top is equal to -1
int isEmpty(struct Stack* stack)
{ return stack->top == -1; }
// Function to add an item to stack. It increases top by 1
void push(struct Stack* stack, char *item)
{
if ( isFull(stack) )
{
resizeStack(stack, stack->capacity * 2 );
}
stack->array[++stack->top] =(char *) malloc(sizeof(char)*(strlen(item) +1));
strcpy(stack->array[stack->top] , item);
}
char * pop(struct Stack* stack)
{
char * ret = NULL;
if(! isEmpty(stack) )
{
ret = stack->array[stack->top];
stack->array[stack->top] = NULL ;
stack->top --;
}
return ret;
}
void freeStack(struct Stack* stack)
{
if( NULL != stack && NULL != stack->array )
{
while( ! isEmpty(stack) )
{
free(pop(stack));
}
free(stack->array);
}
else
{
printf(" freeStack try to free NULL ");
}
}
#define SEPARATER ' '
// A stack based function to reverese a string
void reverse(char str[])
{
int n = strlen(str);
struct Stack* stack = createStack(4);
char sep[2];
sep[0] = SEPARATER;
sep[1] = 0 ;
char * pch = strtok(str,sep);
while( NULL != pch )
{
push(stack,pch);
pch = strtok(NULL,sep);
}
char * swap_buff = (char*)malloc((n+1) * sizeof(char));
char * cp_buff = swap_buff;
do
{
char * top = pop(stack);
strcpy(cp_buff , top);
cp_buff += strlen(top);
*cp_buff++ = SEPARATER;
}while( ! isEmpty(stack) );
swap_buff[n] = 0;
strcpy(str,swap_buff);
freeStack(stack);
}
int main()
{
char str[50];
fgets(str, sizeof(str), stdin);
str[strlen(str)-1]='\0';
reverse(str);
printf("Reversed string is %s\n", str);
return 0;
}