#include <stdio.h>
#include <string.h>
int main(){
char c[20], result[50];
int bool = 0, count = 0, i;
while(fgets(c,20,stdin) != NULL){
int stringSize = strlen(c);
if(stringSize == 11){
int ascii = (int)(c[i]);
for(i = 0; i < stringSize; i ){
if(ascii >= 'A' && ascii <= 'Z'){
bool = 1;
}
}
}
}
if(bool == 1){
count ;
strcat(result,c);
}
printf("%d", count);
printf("%s",result);
}
早上好,我對編程還很陌生,我已經花了很長時間在谷歌上搜索和搜索這個問題,但我似乎無法解決這個問題。基本上我試圖過濾一個 fgets 以便它讀取每個字串,如果它們是大寫字母,它們是“有效的”。但是,我什至無法讓 fgets 停止接受更多輸入。
編輯:這個想法是將每個有 10 個大寫字母的字串存盤在結果中,并且一旦用戶沒有輸入('\0'),fgets while 回圈就會中斷
uj5u.com熱心網友回復:
如果您從標準輸入流輸入字串,那么最好按以下方式重寫 while 回圈的條件
while( fgets(c,20,stdin) != NULL && c[0] != '\n' ){
在這種情況下,如果用戶只是按下 Enter 鍵而不輸入字串,則回圈將停止其迭代。
注意 fgets 可以在輸入的字串后附加換行符 '\n'。你應該洗掉它
c[ strcspn( c, "\n" ) ] = '\0';
然后你可以寫
size_t n = strlen( c );
if ( n == 10 )
{
size_t i = 0;
while ( i != n && 'A' <= c[i] && c[i] <= 'Z' ) i;
bool = i == 10;
}
請注意,使用該名稱是一個壞主意,bool因為這樣的名稱是作為標題中的宏引入的<stdbool.h>。
似乎這個 if 陳述句
if(bool == 1){
count ;
strcat(result,c);
}
必須在while回圈內。并且必須初始化陣列結果
char c[20], result[50] = { '\0' };
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/363014.html
