1

So I need to reverse the words in a sentence, for example: Hello World!= World Hello!

Parrot says hi! = hi says Parrot!

My idea is to take input from the user using an array, and then grabbing each word by finding the space on it. For example Hello would be a word because there is a space after the 'o'. Then I want to save each word into a stack, and then simply pop each one. BUT I am having problems saving a word into a stack. I have tried for loops, while loops and so on, but I can't get it right. I omitted some of the stuff I have tried so that it may compile without errors.

Cannot use the strtok function either. SO SORRY IF THE FORMATTING IS A BIT OFF, BUT THIS IS AN EMERGENCY CODING SITUATION THAT I REALLY NEED HELP ON

#include<stdio.h>
#define MAXSIZE 40

struct stack
{
    char stk[MAXSIZE];
    int top;
};
typedef struct stack STACK;
STACK s;

/*  Function to add an element to the stack */
void push ()
{
    char x;
    if (s.top == (MAXSIZE - 1))
    {
        printf ("Stack is Full\n");
        return;
    }
    else
    {
        printf ("Enter the element to be pushed\n");
        scanf ("%c", &x);
        s.top = s.top + 1;
        s.stk[s.top] = x;
    }
    return;
}

/*  Function to remove an element from the stack */
int pop ()
{
    int x;
if (s.top == - 1)
{
    printf ("Stack is Empty\n");
    return (s.top);
    }
    else
    {
        x = s.stk[s.top];
        printf ("poped element is = %dn", s.stk[s.top]);
        s.top = s.top - 1;
}
return(x);
}

//trying to take in an array, but I cannot use &
void swap(char v[40],int x,int y){
    //trying to swap two array indexes
    char temp; 
    temp = v[x];
    v[x] = v[y];
    v[y] = temp; 
}
   //EDIT HERE    
   void reverseSentence(char *sent){
       char* word = sent;
       int i,j;

       for (i=0; i<sizeof(sent);i++){
          if (i == isalpha){
             //need to save word from start to end
             word = i;
          }
          //i need to push word to stack
      }
   } 
       void sortAlphabetically(char order[]){
       int i;
        for (i =0; i < sizeof(order); i++){
           //how can i compare if i cannot especify indexes on c?
          if (order[i] < order[i+1]){
            swap(order,i,i+1);
           i++;
     } else if (order[i] == ' '){
         //don't do a comparison here just continue to next value
         i++; 
     } else{
        //not comparable so continue thorugh the array
        i++;
     }
    //not sure how anything would change the array since & is not used
}
}

main(){
    //created an array to store input
    char message[40];

//output to user
printf("Hello, please enter some words.");

//input from user, takes in all of it
fgets(message, 40, stdin);
printf("Your message: %s", message);

/*reversing the oder*/
printf("Order reversed: %s", message);
reverseSentence(message);

/*sorting each word alphabetically*/
sortAlphabetically(message);
printf("Once words are alphabetized: %s", message);   
}
Xariazs
  • 11
  • 2

0 Answers0