【发布时间】:2014-03-04 18:01:58
【问题描述】:
你好,我正在做一个软件,它在字符串 1 中搜索字符串 2 中的至少一个字符并打印我们找到的位置。
我的程序中的问题是它打印的值不正确
我的代码 -
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str1[] = "a,s,e,d,8,f";
char str2[] = "1,2,3,4,5,6,7,8,9,0";
int i, j, len1, len2, con = 0;
len1 = strlen(str1);
len2 = strlen(str2);
for (i = 0; i < len1; i++)
{
for (j = 0; j < len2; j++)
{
if (str1[i] == str2[j])
{
break;
}
}
con++;
}
printf("%d", con);
system("PAUSE");
}
返回值应该是5,因为8出现在第二个字符串中,位置5
感谢那些能理解问题并帮助我解决问题的人,非常感谢
【问题讨论】:
-
什么位置?在第一个字符串上还是在第二个字符串上?
-
第一个字符串中数字 8 的位置
-
但要注意不是
5th,是9th,你没算逗号。 -
没有。第一个字符串中的第 5 位是 8
-
我认为 strcspn(str1,"0123456789") 不是可接受的答案?