-1

我正在尝试反转句子中单词的字母。我也试图将这些单词存储在一个新的 char 数组中。目前我得到一个运行时错误,对于我所有的调整我都无法解决。我的方法是创建一个与句子长度相同的新字符数组。然后循环遍历句子,直到我到达一个 ' ' 字符。然后向后循环并将这些字符添加到一个单词中。然后将单词添加到新句子中。任何帮助将非常感激。

int main(void) {
    char sentence [] = "this is a sentence";
    char *newSentence = malloc(strlen(sentence)+1);
    int i,j,start;
    start = 0;

    for(i = 0; i <= strlen(sentence); i++)
    {

        if(sentence[i] == ' ')
        {
            char *word = malloc((i - start)+1);
            for(j = sentence[i]; j >= start; j--)
            {
                word[j] = sentence[j];
            }
            strcat(newSentence,word);
            start =sentence[i +1];
        }
    }
    printf("%s",newSentence);
    return 0;
}
4

5 回答 5

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

int main(void) {
    char sentence [] = "this is a sentence";
    char *newSentence;
    int i,j,start, len;
    start = 0;
    len = strlen(sentence);
    newSentence = malloc(len+1);
    *newSentence = '\0';

    for(i = 0; i <= len; i++)
    {
        if(sentence[i] == ' ' || sentence[i] == '\0')
        {
            char *word = malloc((i - start)+1);
            int c = 0;
            for(j = i - 1; j >= start; j--)
            {
                word[c++] = sentence[j];
            }
            word[c]='\0';
            strcat(newSentence,word);
            if(sentence[i] == ' ')
                strcat(newSentence," ");
            start = i + 1;
            free(word);
        }
    }
    printf("%s",newSentence);
    return 0;
}
于 2013-04-15T18:55:36.333 回答
2

从逻辑上讲,这里:

j = sentence[i]
start =sentence[i +1];

start并且j是 char 数组中的索引位置,您试图将 a 分配char给它们,这会搞砸一切。

应该:

j= i;
start = i +1;

如果你的算法是正确的。

于 2013-04-15T18:30:25.020 回答
1

相同的另一个变体......

int main(int argc, const char *argv[])
{
    char sentence [] = "this is a sentence";
    size_t len = strlen(sentence);
    char *newSentence = malloc(len + 1);
    char *ptr_src = sentence;
    char *ptr_dst = newSentence;

    while(ptr_src)
    {
        char *next, *t;

        next = strchr(ptr_src, ' '); // find next space
        if (!next) next = sentence + len; // if not found, next = EOL

        for (t = next; t > ptr_src;)
        {
            *ptr_dst++ = *--t;
        }
        if (*next)
        {
            *ptr_dst++ = *next++;
            ptr_src = next;
        }
        else
        {
            *ptr_dst = 0;
            break;
        }
    }
    printf("[%s]",newSentence);
    return 0;
}
于 2013-04-15T18:53:59.043 回答
1

您的程序几乎没有错误。我试图在这个程序中删除:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
    char sentence [] = "this is a sentence";
    char *newSentence = (char *)malloc(strlen(sentence)+1);
    int i,j,start, k;
    start = 0;

    for(i = 0;; i++)
    {

        if(sentence[i] == ' ' || sentence[i] == '\0')   //sentence[i] == '\0' for the last word.
        {
            char *word = (char *) malloc((i - start)+1);
            for(j = i-1, k = 0; j >= start; j--, k++)
            {
                word[k] = sentence[j];
            }
        word[k++] = ' ';                    //space after each word
        word[k] = '\0';                 

            strcat(newSentence,word);

            start = i+1;
        }

    if (sentence[i] == '\0')
        break;
    }
    printf("%s\n",newSentence);
    return 0;
}

在http://ideone.com/Z9ogGk现场查看

于 2013-04-15T18:54:33.450 回答
0
strcat(newSentence,word);

newSentence必须是一个字符串。字符串是由第一个空字符终止并包括第一个空字符的连续字符序列

编辑:对于上面写的内容,这个答案已被否决了 4 次。如果您认为不正确,请解释。否则,请删除您的反对票。

于 2013-04-15T18:29:01.200 回答