i want to make rot13 encryption
this program convert ascii code from a caracter, and work only with uppercase character (65-90)
please help me

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void rot13(const char *input);

int main(void) {
	char *input;
	
	printf("Enter string (max. 255 characters) : ");
	gets(input);
	printf("\nConvert result %s to ROT13 = ", input);
	rot13(input);

return 0;
}

void rot13(const char *input) {
	int tmp[255], i;
	
	for (i=0;i<strlen(input);i++) {
		if ((int)input[i] < 65 || (int)input[i] > 90) {
			printf("not valid\n");
			exit(1);
		}
	}
	
	for (i=0;i<strlen(input);i++) {
		tmp[i] = ((int)input[i])+13;
	}
	
	printf("%i", tmp);
}

Recommended Answers

All 2 Replies

i want to make rot13 encryption
this program convert ascii code from a caracter, and work only with uppercase character (65-90)

Think it might have something to do with if ((int)input[i] < 65 || (int)input[i] > 90) :icon_question:

Try this code:

#include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
   
      unsigned char * rot13(unsigned char *input);
   
      int main(void) {
   
      unsigned char *input;
// Don't forget to allocate memory for your buffer... 
      input= (unsigned char *)malloc(256);
  
      printf("Enter string (max. 255 characters) : ");
  
      gets(input);
      printf("\nConvert result %s to ROT13 = ", input);
		printf("%s\n",rot13(input));
      return 0;
      }
  
      unsigned char * rot13(unsigned char *input) {
//len: variable storing the length of input..It's better to store it and call strlen only once, than to call strlen 255 times 
//I'm using unsigned char type rather than int ,because it consumes less memory and it's sufficent since the string's length is less than 256
      unsigned char len,tmp;
      len = strlen(input);
      for(tmp =0; tmp < len;tmp++)
      {
      // no need for char to int conversion
      if(input[tmp] < 65 || input[tmp] > 90) 
      	exit(1);//invalid character
      if(input[tmp] < 78)
      	input[tmp] += 13;	
      else
      	input[tmp] -=13;
      } 
      return input;
   }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.