我試圖找到共同的子字符串和打印「是」如果它存在的代碼。沒有得到正確的輸出檢查共同的子字符串
實施例:
T=1 (NUMBER OF TEST CASES)
hello
world
輸出:
YES ('O' is common in both)
代碼:
#include<stdio.h>
#include<string.h>
int main(void){
int T,t;
scanf("%d",&T);
char A[100],B[100];
int i,j,flag=0;
t=1;
while(t<=T){
scanf("%[^/n]",&A);
scanf("%[^/n]",&B);
for(i=0;A[i]!='\0';i++){
for(j=0;B[j]!='\0';j++){
if(A[i]==B[j]){
flag=1;
break;
}
}
}
t++;
if(flag==1)
printf("YES");
else
printf("NO");
flag=0;
printf("\n");
}
return 0;
}
如果我讀取使用fgets(A,100,stdin)
隨後fgets(B,100,stdin)
字符串,編譯器不讀取第二串(它由於某種原因正在跳過聲明)。
此外,當我在while
循環內使用fgets
語句時,它根本不讀取字符串。
'的scanf( 「%d%* C」,&T);','的scanf(「%[^/n的]「,&A);' - >'scanf(」%[^ \ n]%* c「,A);' – BLUEPIXY
當使用函數:scanf()和函數族時,總是檢查返回的值2)使用格式輸入/轉換器進行刺激時,總是將輸入字符的最大數量限制爲比輸入緩衝區中的字符數少1。 – user3629249
第一次調用scanf()將在輸入緩衝區中保留'\ n'。需要在調用'string'輸入函數之前讀入/放棄。 – user3629249