C语言中的反向词
我试着把句子中单词的字母颠倒过来。我还试图将这些单词存储在一个新的字符数组中。目前,我得到了一个运行时错误,这是我所有的调整,我不能解决。我的方法是创建一个与句子长度相同的新字符数组。然后循环这个句子,直到我找到一个“”字符。然后向后循环并将这些字符添加到单词中。然后在新句子中添加单词。任何帮助都将不胜感激C语言中的反向词,c,arrays,strcpy,strcat,C,Arrays,Strcpy,Strcat,我试着把句子中单词的字母颠倒过来。我还试图将这些单词存储在一个新的字符数组中。目前,我得到了一个运行时错误,这是我所有的调整,我不能解决。我的方法是创建一个与句子长度相同的新字符数组。然后循环这个句子,直到我找到一个“”字符。然后向后循环并将这些字符添加到单词中。然后在新句子中添加单词。任何帮助都将不胜感激 int main(void) { char sentence [] = "this is a sentence"; char *newSentence = malloc(st
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;
}
newSentence必须是一个字符串。字符串是由第一个空字符终止并包括第一个空字符的连续字符序列
编辑:这个答案因上面写的内容而被否决了4次。如果你认为不正确,请解释。否则,请删除您的否决票。逻辑上,这里:
j = sentence[i]
start =sentence[i +1];
start和j是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;
}
你的程序几乎没有bug。我已尝试在此程序中删除:
#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;
}
Check live atnewSentence被正确地声明为*char,malloc看起来也不错。我不明白你的答案。newSentence从未初始化,因此它不能是字符串。一个字符串应该包含一个空终止符。@如果你改变你的答案,把注意力集中在没有初始化的新句子上,我可能会考虑撤销我的下落。对不起,这个答案很清楚:第一个参数是一个字符串,这里不是。您指出了这个问题,但对于一个刚接触编程的人来说,这并不是很难理解的。因此,尽管这在技术上可能是正确的,但这仍然是一个非常低质量的答案。堆栈溢出不是一个琐碎的游戏,你磨声誉。如果你想让我取消我的否决票,你所要做的就是让你的答案不那么神秘word@pm100,你能解释一下你的意思吗?变量“word”包含一个字符串。C字符串的最终字符必须为0 null。这就是c如何知道字符串的结尾在哪里。在mallocAs之后将word设置为所有0,并在Mallocs之后将newSentence设置为以null结尾的字符串,否则strcat在第一次调用时将无法正常工作。谢谢tacp,我确信已修复了某些问题,但我仍然有那个讨厌的运行时错误。@Calgar99看到我更新的帖子了吗?还有一个地方也有同样的错误。谢谢。非常感谢。
#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;
}
#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;
}