C程序-更改句子中的字符顺序

C程序-更改句子中的字符顺序,c,C,我很想在我的任务上得到一些帮助,但我似乎无法得到它。 我需要写一个函数来改变句子中字符的顺序。例子: “hello world”将变成“olleh Dllow”。 这是我得到的功能原型: int changeCharOrderInSentence(char table[][MAX_SENTENCE_LENGTH], int numOfSentences, int sentenceToChange) 我不能使用指针、静态变量或任何东西。 每个单词后面都有一个空格,我该怎么处理呢? 函数需要返回一

我很想在我的任务上得到一些帮助,但我似乎无法得到它。 我需要写一个函数来改变句子中字符的顺序。例子: “hello world”将变成“olleh Dllow”。 这是我得到的功能原型:

int changeCharOrderInSentence(char table[][MAX_SENTENCE_LENGTH], int numOfSentences, int sentenceToChange)
我不能使用指针、静态变量或任何东西。 每个单词后面都有一个空格,我该怎么处理呢? 函数需要返回一个值。。是什么?我不明白,因为函数只是改变了字符的顺序。 这是我写的:

  int changeCharOrderInSentence(char table[][MAX_SENTENCE_LENGTH], int numOfSentences, int sentenceToChange)
{
    int i,j,lensentence;
    char temp;
    lensentence=strlen(table[sentenceToChange]);
    while(table[sentenceToChange])// as long as we are not at the end of the chosen sentence-'\0'
    {
        for(i=sentenceToChange;(lensentence)/2;i++)
        {
                temp=table[sentenceToChange][i];
                table[sentenceToChange][i]=table[sentenceToChange][lensentence-1-i];
                table[sentenceToChange][lensentence-1-i]= temp; 
        }

    }



    }

谢谢:)

使用每个单词都以空格结尾的事实来查找每个单词,然后在每个单词上使用反转代码。

这可能有点帮助:-

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

int main() 
{
  char name[30] = "Hello";
  strrev(name);  //This will reverse your single word
  return 0;
}
#包括
#包括
int main()
{
字符名[30]=“你好”;
strev(name);//这将反转您的单个单词
返回0;
}

有趣的挑战是颠倒单词而不是句子(OP的代码就是这样做的)。像许多问题一样,将其分解为更简单的子函数

  • 调用函数来更改感兴趣的一句话

    int changeCharOrderInSentence1(char sentence[MAX_SENTENCE_LENGTH]);
    
    int changeCharOrderInSentence(char table[][MAX_SENTENCE_LENGTH], 
        int numOfSentences, int sentenceToChange) {
      if (sentenceToChange < numOfSentences) {
        return changeCharOrderInSentence1(table[sentenceToChange]);
      }
      return 0;
    }
    
  • 定义
    reverse\u word()

  • 不清楚
    changeCharOrderInstence()
    的返回值应该是什么。此代码假定它是颠倒的字数


    我不喜欢
    MAX\u-sension\u-LENGTH的名字,因为这个名字意味着字符串的长度,但OP的例子使用它就像字符数组的大小一样。

    有趣的是,我通过使用搜索引擎很快找到了几个关于这个主题的答案。删除
    while(表[sentenceToChange])
    或更改为
    if(LenSession>1)
    用于(i=sentenceToChange;(len句)/2;i++)
    -->
    用于(i=0;i@Tamar Kravitz,来自您的帖子“我不能使用指针…”。但是,第一个函数参数char table[]最大句子长度已经是指针了!:)嗨,我不能用strev…好的,最重要的是,这应该是一个for循环,对整个句子做相反的事情。。我不知道怎么写不,不要把整个句子颠倒过来:把每个单词都颠倒过来。您的反转代码应该能够完成任务,尽管我不清楚您所说的“只是更改字符的顺序”是什么意思——这就是它应该做的。嗨,非常感谢。对我来说,真正理解代码是很重要的。代码与我理解的单词相反,但另2。。我不知道你在那里干了什么。你能解释一下吗?我不明白每个变量的作用和用途,以及2号函数的真正作用。功能1也不清楚。做{
    void reverse_word(char sentence[], int i, int j);
    
    int changeCharOrderInSentence1(char sentence[MAX_SENTENCE_LENGTH]) {
      int i=0;
      int count = 0;
      do {
        while (sentence[i] == ' ') i++;
        int j = i;
        while (sentence[j] != ' ' && sentence[j] != 0) j++;
        reverse_word(sentence, i, j);
        i = j;
        count++;
      } while (sentence[i] != 0);
      return count;
    }
    
    void reverse_word(char sentence[], int i, int j) {
      while (j > i) {
        j--;
        char t = sentence[i];
        sentence[i] = sentence[j];
        sentence[j] = t;
        i++;
      }
    }