我正在编写一个代码,用于打印由我输入的字符串中最长的单词。 但程序没有执行,并且收到错误消息以关闭程序。
<b>POST 1:</b> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation<br>
<form action="" method="POST">
<button type="submit" class="rep" name="dec" value="1">
reply
</button>
<button type="submit" class="" name="dec" value="2">
delete
</button>
<button type="submit" class="" name="dec" value="3">
save
</button>
</form>
<hr>
<b>POST 2:</b> Ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla<br>
<form action="" method="POST">
<button type="submit" class="rep" name="dec" value="1">
reply
</button>
<button type="submit" class="" name="dec" value="2">
delete
</button>
<button type="submit" class="" name="dec" value="3">
save
</button>
</form>
<hr>
<b>POST 3:</b> Pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
<form action="" method="POST">
<button type="submit" class="rep" name="dec" value="1">
reply
</button>
<button type="submit" class="" name="dec" value="2">
delete
</button>
<button type="submit" class="" name="dec" value="3">
save
</button>
</form>
答案 0 :(得分:0)
这是我重写的代码 - 答案---`
#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;
}
答案 1 :(得分:0)
这不是代码块的错误。你的程序开始实际运行,但随后它崩溃(由于segmentation fault)和windows / codeblock显示这个&#34; ...已停止工作&#34;消息。
导致崩溃的实际行是longest
行
strcpy(longestword,longest);
我相信你的意思是longestword
。
还要注意你的代码在语法(在执行它之前阅读编译器的警告)和逻辑中都会有更多的错误。