C语言中句子中的倒装词
所以我需要颠倒句子中的单词,例如: 你好,世界= 世界你好 鹦鹉说你好= 嗨,鹦鹉说 我的想法是使用数组从用户那里获取输入,然后通过查找每个单词上的空格来获取每个单词。例如,Hello是一个单词,因为在“o”后面有一个空格。然后我想将每个单词保存到堆栈中,然后简单地弹出每个单词。但我在将单词保存到堆栈中时遇到问题。我尝试过循环、while循环等等,但我做不好。我省略了一些我尝试过的东西,以便它可以编译而不出错 也不能使用strtok函数。很抱歉,如果格式有点不正确,但这是一个紧急编码情况,我真的需要帮助C语言中句子中的倒装词,c,reversing,C,Reversing,所以我需要颠倒句子中的单词,例如: 你好,世界= 世界你好 鹦鹉说你好= 嗨,鹦鹉说 我的想法是使用数组从用户那里获取输入,然后通过查找每个单词上的空格来获取每个单词。例如,Hello是一个单词,因为在“o”后面有一个空格。然后我想将每个单词保存到堆栈中,然后简单地弹出每个单词。但我在将单词保存到堆栈中时遇到问题。我尝试过循环、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);
}
#包括
#定义最大尺寸40
结构堆栈
{
字符stk[MAXSIZE];
int top;
};
typedef结构堆栈;
堆栈s;
/*函数将元素添加到堆栈中*/
无效推送()
{
字符x;
如果(s.top==(MAXSIZE-1))
{
printf(“堆栈已满\n”);
返回;
}
其他的
{
printf(“输入要推送的元素\n”);
scanf(“%c”、&x);
s、 top=s.top+1;
s、 stk[s.top]=x;
}
返回;
}
/*函数从堆栈中删除元素*/
int-pop()
{
int x;
如果(s.top==-1)
{
printf(“堆栈为空\n”);
返回(s.top);
}
其他的
{
x=s.stk[s.top];
printf(“poped元素为=%dn”,s.stk[s.top]);
s、 top=s.top-1;
}
返回(x);
}
//试图接收数组,但无法使用&
无效交换(字符v[40],整数x,整数y){
//尝试交换两个数组索引
焦炭温度;
温度=v[x];
v[x]=v[y];
v[y]=温度;
}
//编辑这里
无效反向内容(字符*已发送){
char*word=sent;
int i,j;
对于(i=0;inote:!
在d
之后,我认为应该在堆栈上推送一个单词而不是一个字母。我认为C不能接收字符串?我们在C中将其视为char*
。它是一个以'\0'
结尾的字符序列。因此它应该是:void push(){char*x;注意:!
在d
之后,我认为应该在堆栈上推送一个单词而不是一个字母。我认为C不能接收字符串?我们在C中将其视为char*
。它是一个以'\0'
结尾的字符序列。因此应该是:void push(){char*x;