#include <conio.h>
#include <stdio.h>
#include <string.h>
int fun(char *s, char *t)
{
int n; char *p, *r;
n = 0;
while(*s)
{
p = s; r = t;
while(*r)
if(*r == *p)
{
r++;
p++
}
else
break;
if(r == “\0”)
n++;
s++;
}
return n;
}
main( )
{
char s[100], t[100]; int m;
clrscr( );
printf(“\nPlease enter string S:” );
scanf(“%s”, s);
printf(“\nPlease enter substring t:”);
scanf(“%s”, t);
m = fun(s, t);
printf(“\nThe result is : m = %d\n”, m);
}
#include <stdio.h>
#include <string.h>
int fun(char *s, char *t)
{
int n; char *p, *r;
n = 0;
while(*s)
{
p = s; r = t;
while(*r)
if(*r == *p)
{
r++;
p++
}
else
break;
if(r == “\0”)
n++;
s++;
}
return n;
}
main( )
{
char s[100], t[100]; int m;
clrscr( );
printf(“\nPlease enter string S:” );
scanf(“%s”, s);
printf(“\nPlease enter substring t:”);
scanf(“%s”, t);
m = fun(s, t);
printf(“\nThe result is : m = %d\n”, m);
}
解决方案
198
if (r == “\0”)应该是if (*r == “\0”)
你这个仅仅只是统计子串的个数,而不是位置对吧?
你这个仅仅只是统计子串的个数,而不是位置对吧?
#include <conio.h> #include <stdio.h> #include <string.h> int fun(char *s, char *t) { int n; char *p, *r; n = 0; while (*s) { p = s; r = t; while (*r) if (*r == *p) { r++; p++; } else break; if (*r == "\0") n++; s++; } return n; } void main() { char s[100], t[100]; int m; //clrscr(); printf("\nPlease enter string S:"); scanf("%s", s); printf("\nPlease enter substring t:"); scanf("%s", t); m = fun(s, t); printf("\nThe result is : m = %d\n", m); }
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。