簡體   English   中英

C語言中的反向詞

[英]Reverse words in C Language

我正在嘗試將句子中單詞的字母反轉。 我也試圖將這些單詞存儲在一個新的char數組中。 目前,我遇到了運行時錯誤,對於我所有的調整我都無法解決。 我的方法是創建一個與句子長度相同的新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;
}
#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;
}

邏輯上,這里:

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

startj是char數組中的索引位置,您正在嘗試為它們分配一個char ,這會破壞所有內容。

應該:

j= i;
start = i +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;
}

您的程序有幾個錯誤。 我曾嘗試在此程序中刪除的內容:

#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實時檢查

strcat(newSentence,word);

newSentence必須為字符串。 字符串是連續的字符序列,由第一個空字符終止並包括第一個空字符

編輯:這個答案已經被否決了4次以上所寫。 如果您認為它不正確,請解釋。 否則,請刪除您的否決票。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM