#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] = {' '}; int number[Num],number2[Num],flag = 0; printf("Please input a string: "); while((c = getchar()) != EOF && c != ' ' && count1 < Num) { s[count1] = c; count1++; } printf("Please input another string: "); while((c = getchar()) != EOF && c != ' ' && 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 "); for(w = 0;w < count2;w++) { flag2 = 0; for(l = 0;l < m;l++) { if(w == number2[l]) { printf("%c",str[l]); printf(",%d ",number[l]); flag2 = 1; break; } } if(flag2 == 0) printf("%c,-1 ",t[w]); } return 0; }
费了好大劲,完成期间错误百出!要仔细点啊。