#11101: 然後...然後line6就死掉了


Benson86 (nobodyzxc)


以下是小弟的code ,Uva 送有過,卻死在貴j的line6,有路過的善心人士,如果知道小code的死因,還望不吝指點指點

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void){
    char str[111];
    int cnt = 0;
    while(fgets(str , sizeof(str) , stdin)){
    int i , isword = 0;
    for(i = 0 ; i < strlen(str) ; i++){
        if((str[i] <= 'z' && str[i] >= 'a') ||
            (str[i] <= 'Z' && str[i] >= 'A')){
                isword = 1;
        }
        else{
            if(isword){
                cnt++;
                isword = 0;
            }
         }
    }
    printf("%d\n" , cnt);
    cnt = 0;
    }
    return 0;
}

 

#11110: Re:然後...然後line6就死掉了


Layaccc (Layaccc)


以下是小弟的code ,Uva 送有過,卻死在貴j的line6,有路過的善心人士,如果知道小code的死因,還望不吝指點指點

#include
#include
#include
int main(void){
    char str[111];
    int cnt = 0;
    while(fgets(str , sizeof(str) , stdin)){
    int i , isword = 0;
    for(i = 0 ; i < strlen(str) ; i++){
        if((str[i] <= 'z' && str[i] >= 'a') ||
            (str[i] <= 'Z' && str[i] >= 'A')){
                isword = 1;
        }
        else{
            if(isword){
                cnt++;
                isword = 0;
            }
         }
    }
    printf("%d\n" , cnt);
    cnt = 0;
    }
    return 0;
}

 

 

array的大小不夠大導致overflow
char str[111] 改成 char str[500];

#11115: Re:然後...然後line6就死掉了


Benson86 (nobodyzxc)


 

array的大小不夠大導致overflow
char str[111] 改成 char str[500];

萬分感謝,看來用fgets難免會有跑不出RE的盲點。