Inversão de palavras de uma sentença. Escreva um programa que aceite uma linha de texto e use uma pilha para imprimir a linha invertida.
Passo 1
Neste exercício, precisamos escrever um programa no qual o usuário insere uma linha de texto. O programa usa uma pilha para imprimir a linha invertida. Para imprimir a linha invertida, o programa divide a linha em strings separadas por espaço em branco. Cada string representa uma palavra. As palavras são adicionadas à pilha da primeira à última palavra. Para imprimir a linha invertida, o programa pega as palavras do topo da pilha e as imprime uma a uma até chegar ao fundo da pilha. Para separar as palavras da frase e colocá-las na pilha, a função createStack é usada. Esta função usa a função push, para enviar cada palavra para a pilha. Para imprimir a linha em ordem inversa, a função printStack é usada.
Passo 2
O programa a seguir recebe a entrada do usuário e emite a linha em sentido inverso.
#include
#include
#include
/*Define structure and synonime*/
typedef struct stackNode{
char *data;
struct stackNode *next;
} StackNode;
typedef StackNode *stack;
/*Prototypes*/
void createStack(stack *newStack,char *string);
void push(stack *Stack,char *word);
void printStack(stack current);
char* pop(stack *Stack);
int isEmpty(stack Stack);
int main(void){
/*Pointer to the struct*/
stack reverseStack = NULL;
char string[100];
/*Pointer to string*/
char *dummy;
printf("Enter the sentence: \n");
/*Put everything into one string, until the
*end of the input
*/
scanf("%[^\n]%*c",string);
createStack(&reverseStack,string);
printStack(reverseStack);
return 0;
}
void createStack(stack *newStack,char *string){
char word[100];
int j=0;
/*For each char from the string, until the
*end of the string
*/
for(int i = 0; string[i]!='\0';i++){
/*If char is blank space, new word*/
if(string[i] ==' '){
word[j] ='\0';
/*Push word to stack*/
push(newStack,word);
j=0;
}else{
/*Otherwise, add char to the word*/
word[j] = string[i];
j++;
}
}
/*Push last word to the stack*/
push(newStack,word);
}
void push(stack *Stack,char *word){
/*Create new node*/
StackNode *newNode;
newNode = malloc(sizeof(StackNode));
if(newNode == NULL){
printf("%s not added. Not enough memory");
}else{
/*Allocate memory for each string*/
newNode->data = malloc((strlen(word)+1)
*sizeof(char));
/*Copy string*/
strcpy(newNode->data, word);
newNode->next = *Stack;
/*New top of the string*/
*Stack = newNode;
}
}
void printStack(stack current){
if(current == NULL){
printf("The stack is empty.");
}else{
while(current!=NULL){
printf("%s ", current->data);
current=current->next;
}
}
return;
}
char* pop(stack *current){
/*Temporary node pointer*/
stack temp;
/*Pointer to string*/
char *value;
if(isEmpty(*current)){
printf("The stack is empty");
}else{
temp = *current;
value = temp->data;
*current = (*current)->next;
free(temp);
}
return value;
}
int isEmpty(stack Stack){
return Stack==NULL;
}
Passo 3
A saída do programa é
Enter the sentence:
This is easy
easy is This
Resposta
Ver o desenvolvimento.