C语言 在不使用库函数的情况下查找字符串中的子字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35631422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding substring in string without using library function
提问by Aung
Below is the code template and under /* write your code here */ is my own code. The template should be correct but there is sth wrong with my code.
下面是代码模板,在/* write your code here */ 下是我自己的代码。模板应该是正确的,但我的代码有问题。
My algorithm is to iterate through str until finding the null character. Then compare each character, if they are the same then iterate through both str and sub, otherwise set continue to iterate through str and reset to the first character of substr.
我的算法是遍历 str 直到找到空字符。然后比较每个字符,如果相同则遍历 str 和 sub,否则 set 继续遍历 str 并重置为 substr 的第一个字符。
#include <stdio.h>
int findSubstring(char *str, char *substring);
int main()
{
char str[40], substr[40];
printf("Enter the string: ");
gets(str);
printf("Enter the substring: ");
gets(substr);
printf("findSubstring(): %d\n", findSubstring(str, substr));
return 0;
}
int findSubstring(char *str, char *substr)
{
/* write your code here */
int i = 0, j = 0;
while ((str[j] != '#include <stdio.h>
int findSubstring(char *str, char *substring);
void safer_gets(char *str, size_t max);
int main(void)
{
char str[40], substr[40];
printf("Enter the string: ");
safer_gets(str, sizeof(str));
printf("Enter the substring: ");
safer_gets(substr, sizeof(str));
printf("findSubstring(): %d\n", findSubstring(str, substr));
return 0;
}
int findSubstring(char *str, char *substr)
{
int i = 0, j = 0;
while ((*(str + j) != '#include<stdio.h>
#include<conio.h>
void main()
{
char s[100],sub[50];
int i,j,c=0;
clrscr();
printf("enter string and substring\n");
gets(s);
printf("\n");
gets(sub);
printf("\n");
i=0;
j=0;
while(s[i]!='##代码##')
{
if(s[i]!=sub[j])
i++;
else if(s[i]==sub[j])
{
while(sub[j]!='##代码##')
{
if(s[i]==sub[j])
{
i++;
j++;
c++;
}
else
{
c=0;
break;
}
}
}
}
if(c!=0)
printf("\nsubstring is present \n ");
else
printf("\nsubstring is absent \n ");
getch();
}
')&&(*(substr + i) != '##代码##')) {
if (*(substr + i) != *(str + j)) {
j++;
i = 0;
}
else {
i++;
j++;
}
}
if (*(substr + i) == '##代码##')
return 1;
else
return -1;
}
void safer_gets(char *str, size_t max)
{
int i;
fgets(str, max, stdin);
for (i = 0; *(str + i) != '##代码##'; i++) {
if (*(str + i) == '\n') {
*(str + i) = '##代码##';
break;
}
}
}
')||(substr[i] != '##代码##')) {
if (substr[i] != str[j]) {
j++;
i = 0;
}
else {
i++;
j++;
}
}
if (substr[i] == '##代码##')
return 1;
else
return -1;
}
回答by MikeCAT
- Do not use
gets(), which has unavoidable risk of buffer overrun. - The condition of the loop is wrong. The loop should exited if one of
*(str + j)or*(substr + i)is a (terminating) null character.
- 不要使用
gets(),它有不可避免的缓冲区溢出风险。 - 循环条件错误。循环是否应该退出的一个
*(str + j)或*(substr + i)为(终止)空字符。
Fixed code:
固定代码:
##代码##
