编译器练习:将C程序中的除注释外的所有保留字全部大写

/*programed by:alex shoal
edit date:2009-3-22, Sunday 
revision:2
****************
*Program Objective:将C程序中的除注释外的所有保留字全部大写
****************
*int,long,float,double,bool,char,
if,while,switch,case,continue,break,else,
void,unsigned,extern,global,default,
*/
#include <stdio.h>
#include <stdlib.h> 
#include <string.h>

char kw[][18]={"int","long","float","double","bool","char","if","while","switch","case","continue","break","else","void","unsigned","extern","global","default"}; 
int main(int argc, char *argv[])
{
char token[32];
int i;
int j;
int check=0;
int sz=0;
int state=0;
int ch; // place to store each character as read
FILE *fp; // "file pointer"
  if (argc != 2){
  printf("Usage: %s filename/n", argv[0]);
  exit(1);
  }
  if ((fp = fopen(argv[1], "r")) == NULL){
  printf("Can't open %s/n", argv[1]);
  exit(1);
  }

  while (((ch = getc(fp)) != EOF)){  
  i=0;
  j=0;
  sz=0;
  check=0;
  switch(state){
  case 0:{ if(ch=='/') state=1; break; }
  case 1:{ switch(ch){
  case '*': state=2;break;
  case '/': state=4;break;
  default : state=0;break;
  } 
  break;
  }
  case 2:{ if(ch=='*') state=3;break; }
  case 3:{ if(ch=='/') state=0;else state=2;}
  case 4:{ if(ch==10 ) state=0;} //if ch == '/n', exit
   
  } 
  if((!isalpha(ch))||state==2||state==4) putc(ch,stdout);
  else{
  for(i=0;i<32;i++) token[i]='/0';
  i=0;
  do{
  token[i++]=ch;
  ch=getc(fp);} while(isalpha(ch)&&ch!=EOF);
  for(i=0;i<18;i++)
  if (strcmp(token,kw[i])==0) { 
  check=1;
  break;}
  //sz=sizeof(token); 
   
  for(j=0;token[j]!='/0';j++){
  if(check==1) putc(token[j]-32,stdout);
  else putc(token[j],stdout); }
  putc(ch,stdout);  
  }  
}  
   
  fclose(fp);
  return 0; 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值