notpron

git clone git://xatko.vsos.ethz.ch/notpron.git
Log | Files | Refs

shift.c (1047B)


      1 #ifdef __unix
      2 #include <unistd.h>
      3 #else
      4 #define STDOUT_FILENO 1
      5 #include <io.h>
      6 #endif
      7 unsigned short int inRange(char c){
      8 	return ((c>='a'&&c<='z')||(c>='A'&&c<='Z'));
      9 }
     10 char shiftChar(char c, unsigned short int shift){
     11 	return (c+shift);
     12 }
     13 int shiftString(char *str, unsigned short int shift){
     14 	char *strbase,*strwr;
     15 	strbase=strwr=str;
     16 	while(*str!='\0'){
     17 		if(*str==' '){
     18 			*strwr=' ';
     19 		}
     20 		else{
     21 			*strwr=shiftChar(*str,shift);
     22 		}
     23 		if(inRange(*strwr)){
     24 			strwr++;
     25 		}
     26 		str++;
     27 	}
     28 	*strwr='\0';
     29 	return strwr-strbase;
     30 }
     31 int atoi(const char *str){
     32 	int ret=0;
     33 	unsigned short int neg=0;
     34 	while(*str!='\0'){
     35 		if(ret==0&&*str=='-'){
     36 			neg=1;
     37 		}
     38 		if(*str>='0'&&*str<='9'){
     39 			ret*=10;
     40 			ret+=(*str-'0');
     41 		}
     42 		str++;
     43 	}
     44 	if(neg==1){
     45 		ret=-ret;
     46 	}
     47 	return ret;
     48 }
     49 int main(int argc, char **argv){
     50 	unsigned short int i;
     51 	if(argc<3){
     52 		return -1;
     53 	}
     54 	int shift=atoi(argv[1]);
     55 	for(i=2; i<argc; i++){
     56 		write(STDOUT_FILENO,argv[i], shiftString(argv[i],shift));
     57 		write(STDOUT_FILENO," ", 1);
     58 	}
     59 	write(STDOUT_FILENO,"\n", 1);
     60 	return 0;
     61 }