C语言用栈反转字符串,字符反转的问题C语言

题意:

输入一行字符串,将每一个单词(只有空格隔开)倒序输出!

注意:

开始和结尾可能有多个空格,单词之间可能有多个空格!

Sample Input

3

olleh !dlrow

m’I morf .udh

I ekil .mca

Sample Output

hello world!

I’m from hdu.

I like acm.

我下面的代码跑的时候开始和结尾的空格都是按原空格输出的啊。可为什么还是Presentation Error,望指教问题出在哪里。谢谢!

bVNN1L?w=193&h=185

#include

#include

#define MAXSIZE 1001

typedef char ElementType;

typedef struct Node *PtrToNode;

typedef PtrToNode Stack;

typedef struct Node

{

ElementType Data;

PtrToNode Next;

};

Stack CreateStack();

int IsEmpty( Stack S );

void Push(ElementType X, Stack S);

void Pop(Stack S);

void MakeEmpty(Stack S);

void DisplayStack(Stack S);

int main() {

char string[MAXSIZE], warp;

char *PtrToChar;

int instance;

Stack S;

scanf("%d",&instance);

warp = getchar();

S = CreateStack();

if( warp == '\n')

{

while( instance-- ) {

gets(string);

PtrToChar = string;

while( *PtrToChar != '\0' ){

if( *PtrToChar != ' ' )

{

Push( *PtrToChar,S );

}

else

{

DisplayStack(S);

printf("%c",*PtrToChar);

MakeEmpty(S);

}

PtrToChar++;

}

if( !IsEmpty(S))

{

DisplayStack(S);

MakeEmpty(S);

}

puts("\n");

}

}

return 0;

}

Stack CreateStack( )

{

Stack S;

S = malloc( sizeof( struct Node) );

if(S == NULL)

exit(0);

S->Next = NULL;

return S;

}

int IsEmpty( Stack S )

{

return S->Next == NULL;

}

void Push(ElementType X, Stack S)

{

PtrToNode TmpCell;

TmpCell = malloc(sizeof( struct Node));

if(TmpCell == NULL)

exit(0);

else

{

TmpCell->Data = X;

TmpCell->Next = S->Next;

S->Next = TmpCell;

}

}

void Pop( Stack S)

{

PtrToNode firstCell;

if(S == NULL)

exit(0);

if( S->Next == NULL)

{

printf("Empty Stack");

exit(0);

}

else

{

firstCell = S->Next;

S->Next = S->Next->Next;

free(firstCell);

}

}

void DisplayStack( Stack S)

{

if( S == NULL )

exit(0);

else

{

/*栈顶指针,不含元素,要特殊处理一下*/

S = S->Next;

while( S != NULL){

printf("%c",S->Data);

S = S->Next;

}

}

}

void MakeEmpty( Stack S)

{

if(S == NULL)

exit(0);

else

while( !IsEmpty( S ) )

Pop( S );

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用实现字符串中单词的反转,可以按照以下步骤进行操作: 1. 创建一个字符。 2. 遍历字符串中的每个字符: - 如果当前字符不是空格,则将其入。 - 如果当前字符是空格: - 如果不为空,说明中存储了一个完整的单词,将中的字符依次出并打印。 - 打印空格。 3. 最后,如果中还有字符,说明最后一个单词没有被打印出来,将中的字符依次出并打印。 以下是使用C语言实现的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 定义字符结构 struct Stack { char* data; int top; int maxSize; }; // 初始化 void initStack(struct Stack* stack, int maxSize) { stack->data = (char*)malloc(maxSize * sizeof(char)); stack->top = -1; stack->maxSize = maxSize; } // 判断是否为空 int isStackEmpty(struct Stack* stack) { return stack->top == -1; } // 判断是否已满 int isStackFull(struct Stack* stack) { return stack->top == stack->maxSize - 1; } // 入 void push(struct Stack* stack, char c) { if (isStackFull(stack)) { return; } stack->data[++stack->top] = c; } // 出 char pop(struct Stack* stack) { if (isStackEmpty(stack)) { return '\0'; } return stack->data[stack->top--]; } // 字符串中单词反转 void reverseWords(char* str) { int len = strlen(str); struct Stack stack; initStack(&stack, len); for (int i = 0; i < len; i++) { if (str[i] != ' ') { push(&stack, str[i]); } else { while (!isStackEmpty(&stack)) { printf("%c", pop(&stack)); } printf(" "); } } while (!isStackEmpty(&stack)) { printf("%c", pop(&stack)); } } int main() { char str[] = "Hello World! This is a test."; printf("原始字符串:%s\n", str); printf("反转后的单词:"); reverseWords(str); return 0; } ``` 以上是一个使用实现字符串中单词反转的示例代码。它会将字符串中的每个单词反转输出。您可以根据自己的需求进行修改和使用。希望对您有帮助!如有任何疑问,请随时追问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值