为什么此代码产生无限循环?
#include <Stdio.h>
#include <string.h>
int main(){
char str[51];
int k = 1;
printf("Enter string\n");
scanf("%s", &str);
for(int i = 0; i < strlen(str); i++){
while(str[k] != '\0')){
if(str[i] == str[k]){
printf("%c", str[i]);
k++;
}
}
}
return 0;
}
这是简单的C代码,检查字符串中的重复字符并打印字符。我不明白为什么它会产生无限的循环。内部循环应在str [k]
到达零终端时停止,但程序无限继续。
#include <Stdio.h>
#include <string.h>
int main(){
char str[51];
int k = 1;
printf("Enter string\n");
scanf("%s", &str);
for(int i = 0; i < strlen(str); i++){
while(str[k] != '\0')){
if(str[i] == str[k]){
printf("%c", str[i]);
k++;
}
}
}
return 0;
}
It is simple C code that checks for duplicate characters in string and prints the characters. I am not understanding why it is producing an infinite loop. The inner while loop should stop when str[k]
reaches the null terminator but the program continues infinitely.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要知道
str
的地址传达到scanf()
“%s”
,使用“%&lt; w width&gt; s”
,避免 buffer-overfloflfloflfloflfloflfloflsize_t
通过任何数组i&lt; strlen(str)
,使循环的时间复杂性 o(n 3 ),而不是 o(n 2 ),这也不是很好,您应该检查str [i]!= 0
。但是,c
的许多现代编译器都会顺其自然。#include&lt; stdio.h&gt;
这是非常错误的,stdio.h
!=stdio.h
printf()
可以使用puts()
和putc()
在没有任何特殊格式的情况下优化,现代编译器也可以优化while(str [k]) !='\ 0')){
具有括号(')'
)str
使用{}
,这将把0
分配给str
的所有元素更好地实现
我对此问题的实现是创建一个初始化的字符列表(256 max),然后添加1个到该列表中字符的ASCII值(来自
str
)。之后,打印那些值大于1的字符。时间复杂度= o(n),其中
n
是字符串的长度space complectity = o(no_of_chcharacters),其中
no_of_of_characters
是256最终代码
Points to know
str
toscanf()
"%s"
, use"%<WIDTH>s"
, to avoid buffer-overflowscanf()
conversion was successful or not, by checking its return valuesize_t
to iterator over any arrayi < strlen(str)
, makes the loop's time complexity O(n3), instead of O(n2), which also isn't very good you should check whetherstr[i] != 0
. But, many modern compilers ofC
will optimize it by the way.#include <Stdio.h>
it is very wrong,stdio.h
!=Stdio.h
printf()
can be optimized usingputs()
andputc()
without any special formatting, here also modern compiler can optimize itwhile(str[k] != '\0')){
has a bracket (')'
)str
using{}
, this will assign0
to all the elements ofstr
Better Implementation
My implementation for this problem is that create a list of character (256 max) with 0 initialized, and then add 1 to ASCII value of the character (from
str
) in that list. After that print those character whose value was greater than 1.Time Complexity = O(n), where
n
is the length of the stringSpace Complexity = O(NO_OF_CHARACTERS), where
NO_OF_CHARACTERS
is 256Final Code
用英语阅读您的代码:仅增量变量
k
如果索引的字符k
等于indexi
的字符。对于任何具有不同前两个字符的字符串,您将遇到无限循环:char
在索引i == 0
在索引上不等于char
k == 1
,因此k
未递增和,而(str [k]!= 0)
永远。Read your code in English: You only increment variable
k
if character at indexk
is equal to character at indexi
. For any string that has different first two characters you will encounter infinite loop:char
at indexi==0
is not equal tochar
at indexk==1
, sok
is not incremented andwhile(str[k]!=0)
loops forever.