So think this subject later.
But the sample can pass, I got the same answer.
1038. Spell checker Time Limit: 0.5 second Memory Limit: 16 MB The boss of a firm that you are employed with is dissatisfied with the text processor Word. He wants you to write a better text processor by tomorrow. The interface of the new processor should be clearer, there should be more options, and the resulting text should be more beautiful. You told the boss that this work would take not less than four days. Then your boss asked you to begin with a spell checking program. This program should check capital and small letters. It should detect a mistake in each of the following cases. 1. The first letter in a sentence is small. 2. A capital letter is not the first letter in a word. A word is a sequence of letters not containing any other symbols or ends of line. The end of a sentence is defined a full stop, a question-mark or an exclamation mark. Input Input contains a text that consists of capital and small letters of the Latin alphabet (A–Z, a–z), digits (0–9), punctuation marks (.,;:-!?) and space characters. The text length is not more than 10000. Output Output should contain a number of mistakes in the input text. |
#include
#include
#include
char *mygetline(char *line, int size){
int len;
char *com_line;
com_line = (char *) malloc (size);
while ( fgets(line, size, stdin) != NULL ){
len = strlen(line);
strcat(com_line,line);
}
len = strlen(com_line);
com_line[len-1] = '\0' ;
/*
printf("%s",com_line);
printf("%d",strlen(com_line));
*/
return com_line;
}
int main(void)
{
size_t nbytes = 2;
char *c_string;
char text[2] = "";
int i,error;
int flag_first_letter,flag_punctuation;
/* These 2 lines are the heart of the program. */
c_string = (char *) malloc (nbytes + 1);
c_string = mygetline(text, sizeof text);
/* printf("%s",c_string); */
flag_first_letter = 0;
flag_punctuation = 1;
error = 0;
for ( i=0; i< strlen(c_string);i++ ){
if( c_string[i]=='.' || c_string[i]==',' || c_string[i]==';' || c_string[i]==':' || c_string[i]=='-' || c_string[i]=='!' || c_string[i]=='?' || c_string[i]=='\n'){
flag_punctuation = 1;
flag_first_letter = 0 ;
continue ;
}
if( c_string[i]==' ' ){
flag_first_letter = 0 ;
continue ;
}
if ( flag_punctuation == 1 ){
if( c_string[i] >= 'a' || c_string[i] >= 'z' ){
error++;
flag_punctuation = 0 ;
flag_first_letter = 1 ;
}
else{
flag_punctuation = 0 ;
flag_first_letter = 1 ;
}
}
else{
if( flag_first_letter == 1 && ( c_string[i] >= 'A' && c_string[i] <= 'Z' ) ){
error++;
continue;
}
else if( (c_string[i] >= 'A' && c_string[i] <= 'Z' ) || (c_string[i] >= 'a' && c_string[i] <= 'z' ) ){
flag_first_letter = 1 ;
continue;
}
else
continue;
}
}
/* printf("Error : %d",error); */
printf("%d\n",error);
return 0;
}
/*
http://acm.timus.ru/forum/thread.aspx?space=1&num=1038&id=5701&upd=633298366317090000
http://acm.timus.ru/forum/thread.aspx?space=1&num=1038&id=15439&upd=633298373142715000
http://www.seas.upenn.edu/cets/answers/gcc.html gdb
(gdb) b main
Breakpoint 1 at 0x804857e: file 1038.c, line 33.
(gdb) run < 2
*/
0 意見:
張貼留言