Question: I need help with this problem: Problem. Reverse the words of a sentence. Write a program that inputs a line of text and uses a
I need help with this problem:
Problem. Reverse the words of a sentence. Write a program that inputs a line of text and uses a stack to print the line reversed.
What can be modified to make this output correctly?
#include #include #include struct revLine { char lineTokken[20]; struct revLine *next; }; typedef struct revLine RevLine;
RevLine *pushStack(RevLine *headStack, char *word) { RevLine *newNode; newNode = (RevLine*)malloc(sizeof(RevLine)); strcpy(newNode->lineTokken, word); newNode->next = headStack; headStack = newNode; return(headStack); } RevLine* popStack(RevLine *headStack) { RevLine *delNode; int counter = 0; printf("Output is : "); while (headStack != NULL) { delNode = headStack; printf("Counter = %d and element popped is: %s ", ++counter, delNode->lineTokken); headStack = headStack->next; free(delNode); } return(headStack); }
int main() { char sentence[80]; char tokken[80]; int length, loop; int insPosition; RevLine *stackHead; stackHead = NULL; printf("Enter a sentence"); gets(sentence); puts(sentence); strcat(sentence, " "); length = strlen(sentence); insPosition = 0; for (loop = 0; loop < length; loop++) { if (sentence[loop] != ' ') { tokken[insPosition++] = sentence[loop]; } else { tokken[insPosition] = '\0';
stackHead = pushStack(stackHead, tokken); insPosition = 0; } } stackHead = popStack(stackHead); return(0); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
