我的程序從輸入掃描並打印所有使用的大寫字母。爲什麼我的scanf輸入不正確?
我試圖在我的程序結尾處打印來自stdin的原始輸入。
但是,當我使用printf,它似乎跳過輸入表達式的第一部分,在我的字符數組中打印剩餘的東西。請幫我看看問題出在哪裏。 - 代碼註釋 -
#include <stdio.h>
int main(void){
char input[81];
int letters[91];
int i;
//initialize arrays input and letters
for (i = 0; i < 90; i++) letters[i] = 2;
for (i = 0 ; i < 80; i++) input[i] = 'a';
i = 0;
//reads into input array until EOF
while((scanf("%c",input)!= EOF)){
//checks input for characters A-Z
if((input[i]>= 'A' && input[i]<= 'Z'))
letters[input[i]] = 1;
}
//prints capital letters from input that occur at least once
for(i = 'A'; i < 'Z'; i++){
if (letters[i]==1)
printf("%c", i);} // this output works fine, the scan worked??
//print blank line
printf("\n\n");
// print input
printf("%s\n", input); //This is where the incorrect output comes from.
return 0;}
我的原始輸入更改了嗎?爲什麼? 是否我的輸入無法正確掃描? 請迅速回復!
它必須是'&input [i]','scanf()'需要一個地址。 –
感謝您的編輯。我輸錯了它。 – nkskalyan