1

したがって、文の単語を逆にする必要があります。たとえば、次のようになります。 Hello World!= World Hello!

オウムがこんにちは!= やあ、オウムだ!

私の考えは、配列を使用してユーザーから入力を取得し、その上のスペースを見つけて各単語を取得することです。たとえば、'o' の後にスペースがあるため、Hello は単語になります。次に、各単語をスタックに保存してから、各単語を単純にポップします。しかし、単語をスタックに保存する際に問題があります。for ループや while ループなどを試しましたが、うまくいきません。エラーなしでコンパイルできるように、試したことのいくつかを省略しました。

strtok 関数も使用できません。フォーマットが少しずれている場合は申し訳ありませんが、これは緊急のコーディング状況であり、本当に助けが必要です

#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);   
}
4

0 に答える 0