poj 1488 TEX Quotes 双引号的改写 (☆☆☆☆☆)

http://poj.org/problem?id=1488

曾经做过一个类似的,也是对双引号进行修改。

这题要使用整行读人,我习惯使用gets()函数,当然也有其他的函数get(cin,string s)、cin.getline(charArray, max_length,'\n')。但有时做字符串题时会发生与换行符有关的错误,不能理解!

Sample Input

"To be or not to be," quoth the Bard, "that
is the question".
The programming contestant replied: "I must disagree.
To `C' or not to `C', that is The Question!"

Sample Output

``To be or not to be,'' quoth the Bard, ``that
is the question''.
The programming contestant replied: ``I must disagree.
To `C' or not to `C', that is The Question!''

 

  • Source Code
    #include <stdio.h>
    #include <string.h>
    
    
    const int N = 1000;
    int main(){
    	int i,j,cnt=0;
    	char src[N],dst[N];
    	while(gets(src)){
    
    		for(i=j=0;i<strlen(src);i++){
    			if(src[i]!='"'){
    				dst[j++]=src[i];
    			}
    			else {
    				cnt=(cnt+1)%2;
    				if(cnt){
    					dst[j++]='`';
    					dst[j++]='`';
    				}
    				else{
    					dst[j++]='\'';
    					dst[j++]='\'';				
    				}
    			}
    		}
    		dst[j]='\0';
    		printf("%s\n",dst);
    	}
    	return 0;
    }
  • posted @ 2011-04-26 20:55  Pengchao Bai  阅读(486)  评论(0编辑  收藏  举报