《c程序设计语言》读书笔记-第二个字符串任意一个在第一个字符串出现的位置,未出先返回-1
#include <stdio.h> #include <string.h> #define Num 1000 int main() { int c,i,j = 0,m = 0,n = 0,k,count1 = 0,count2 = 0,w,h = 0,l = 0,flag2 = 0; char s[Num],t[Num],str[Num] = {'\0'}; int number[Num],number2[Num],flag = 0; printf("Please input a string:\n"); while((c = getchar()) != EOF && c != '\n' && count1 < Num) { s[count1] = c; count1++; } printf("Please input another string:\n"); while((c = getchar()) != EOF && c != '\n' && count2 < Num) { t[count2] = c; count2++; } for(i = 0;i < count1;i++) { flag = 0; for(k = 0;k < m;k++) { if(s[i] == str[k]) { flag = 1; break; } } if(flag == 0) { for(j = 0;j < count2;j++) { if(s[i] == t[j]) { str[m++] = s[i]; number[n++] = i; number2[h++] = j; break; } } } } printf("The first location in str1 where any character from str2 occurs\n"); for(w = 0;w < count2;w++) { flag2 = 0; for(l = 0;l < m;l++) { if(w == number2[l]) { printf("%c",str[l]); printf(",%d\n",number[l]); flag2 = 1; break; } } if(flag2 == 0) printf("%c,-1\n",t[w]); } return 0; }
费了好大劲,完成期间错误百出!要仔细点啊。