Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with this problem: Write a program that inputs a line of text and us

ID: 3823411 • Letter: I

Question

I need help with this problem:

Write a program that inputs a line of text and uses a stack to print the line reversed.

I am trying to get this program to work but I keep getting an error "a value of type "void*" cannot be assigned to an entity of type "RevLine*":

This is the code I'm trying to execute:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct revLine
{
   char lineTokken[20];
   struct revLine *next;
};
typedef struct revLine RevLine;

revLine* popStack(RevLine *);
revLine* pushStack(RevLine*, char *);


int main()
{
   char sentence[80];
   char tokken[80];
   int length, loop;
   int insPosition;

   RevLine *stackHead;
   stackHead = NULL;

   printf("Enter a sentence");
   scanf("%s",sentence);
   strcat(sentence, " ");
   length = strlen(sentence);
   insPosition = 0;

   for (loop = 0; loop < length; loop++)
   {
       if (sentence[loop] != ' ')
       {
           tokken[insPosition++] = sentence[loop];
       }

       else
       {
           tokken[insPosition] = '';
          
           stackHead = pushStack(stackHead, tokken);

           insPosition = 0;
       }

   }

   stackHead = popStack(stackHead);

   return(0);
}

RevLine *pushStack(RevLine *headStack, char *word)
{
   RevLine *newNode;
   newNode = 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);
}

Where am I going wrong?

Explanation / Answer

I have fixed compilation warning. Basically when using malloc you need to type cast otherwise it won't work.

I haven't looked into correctness of code (I tried to run it and its running fine but am not sure on what you are trying to do)

What is expected output for: abcd efgh ijkl?

is it: ijkl efgh abcd?

or : lkji dcfe dcba.

Good luck. (Just one tip, if you want to read a space separted string from command line try using getline and not scanf)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct revLine
{
char lineTokken[20];
struct revLine *next;
};
typedef struct revLine RevLine;
revLine* popStack(RevLine *);
revLine* pushStack(RevLine*, char *);

int main()
{
char sentence[80];
char tokken[80];
int length, loop;
int insPosition;
RevLine *stackHead;
stackHead = NULL;
printf("Enter a sentence");
scanf("%s",sentence);
strcat(sentence, " ");
length = strlen(sentence);
insPosition = 0;
for (loop = 0; loop < length; loop++)
{
if (sentence[loop] != ' ')
{
tokken[insPosition++] = sentence[loop];
}
else
{
tokken[insPosition] = '';
  
stackHead = pushStack(stackHead, tokken);
insPosition = 0;
}
}
stackHead = popStack(stackHead);
return(0);
}
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);
}