Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 编译器未在代码块中执行程序_C_Codeblocks - Fatal编程技术网

C 编译器未在代码块中执行程序

C 编译器未在代码块中执行程序,c,codeblocks,C,Codeblocks,我正在写一个代码来打印我输入的字符串中最长的单词。 但是程序没有执行,我收到一条错误消息来关闭程序 #include <stdio.h> #include <string.h> int main(){ char my_str[] = "in this program we will find the longest keyword used so that we can"; int longest = 0; char word[20];

我正在写一个代码来打印我输入的字符串中最长的单词。 但是程序没有执行,我收到一条错误消息来关闭程序

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

int main(){
   char my_str[] = "in this program we will find the longest keyword used so 
   that we can";


int longest = 0; 
char word[20];      
char longestWord[20]; 

int i = 0, j;

while(my_str[i]!='\0'){
    j = 0;
    while(my_str[i]!=' ' && my_str[i]!='\0'){
        word[j++] = my_str[i++];
    }
           word[j] = '\0';
    if (strlen(word) > longest){
        longest = strlen(word);
        strcpy(longestWord, word);
    }

    if (my_str[i] == ' '){
        i++;
    }
}
printf("Longest word: %s\n", longestWord);
printf("Length: %d\n", longest);

return 0;
}
#包括
#包括
int main(){
char my_str[]=”在这个程序中,我们将找到使用的最长关键字
“我们可以”;
int=0;
字符字[20];
char最长单词[20];
int i=0,j;
while(my_str[i]!='\0'){
j=0;
while(my_str[i]!=''&&my_str[i]!='\0'){
word[j++]=my_str[i++];
}
字[j]='\0';
if(strlen(word)>最长){
最长=strlen(字);
strcpy(最长的单词,单词);
}
如果(我的[我]=''){
i++;
}
}
printf(“最长单词:%s\n”,最长单词);
printf(“长度:%d\n”,最长);
返回0;
}
固定代码

这是我重写的代码——答案---`

#包括
#包括
int main(){
char my_str[]=”在这个程序中,我们将找到使用的最长关键字
“我们可以”;
int=0;
字符字[20];
char最长单词[20];
int i=0,j;
while(my_str[i]!='\0'){
j=0;
while(my_str[i]!=''&&my_str[i]!='\0'){
word[j++]=my_str[i++];
}
字[j]='\0';
if(strlen(word)>最长){
最长=strlen(字);
strcpy(最长的单词,单词);
}
如果(我的[我]=''){
i++;
}
}
printf(“最长单词:%s\n”,最长单词);
printf(“长度:%d\n”,最长);
返回0;
}

这不是代码块的错误。您的程序开始实际运行,但随后崩溃(由于错误),windows/代码块显示此“…已停止工作”消息

导致崩溃的实际行是行中最长的

strcpy(longestword,longest);
我相信你的意思是那里最长的单词


还请注意,您的代码在语法(在执行之前阅读编译器的警告)和逻辑方面都有更多错误。

编译正确吗?检查strcpy(最长单词,最长单词)的
2nd
参数是否应在第二个
中使用
j
,而
?。此代码仍有一些错误(它甚至没有在我的机器中编译)。也许再多看看。既然我们已经回答了您的问题,就没有必要发布实际正确的代码了,先生,但是它在在线编译器中运行正常。您能帮我查找吗?抱歉耽搁了这么久。作为一个初学者,我想我永远也学不会编码了。你好。编译错误似乎只是因为上面代码中写入的字符串
my_str
的值占据了两行,这是复制粘贴时我没有看到的。这就是它不起作用的原因。当我把它放在一行上时,你的程序执行得很好。很抱歉,我在之前的评论中没有提供足够的信息。事实上你是对的。不要担心自己是初学者。每个人都是。干杯,谢谢,先生,这帮了大忙。同样感谢您的激励。您能帮助我吗?我怎样才能参与到能够进一步帮助我提高知识水平的现场项目中去呢?@AshutoshKumarMishra请记住,这不是您的代码中唯一的错误,而是它崩溃的原因。(另外,我编辑了一个涉及分段错误的链接)
strcpy(longestword,longest);