所以我需要翻译句子中的单词,例如: Hello World!= 世界你好!
鹦鹉说嗨! = 嗨说鹦鹉!我的想法是使用数组从用户那里获取输入,然后通过查找每个单词来抓取每个单词。例如,Hello就是一个单词,因为'o'后面有一个空格。然后我想将每个单词保存到一个堆栈中,然后简单地弹出每个单词。但是我在将一个单词保存到堆栈时遇到了问题。我试过循环,循环等等,但我无法做到正确。我省略了一些我尝试过的东西,以便它可以编译而不会出错。
也不能使用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);
}