Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rot13 in C
#1
huzzah
Code:
#include <stdio.h>

char rotateChar(int oldChar);
void encryptString(char *str);
void encryptFile(FILE *fp, char *name);

int main(int argc, char *argv[]) {

if(argc < 2) {
  printf("fudge you.\n");
  return 0;
}

FILE *fp = fopen(argv[1], "r");

if(fp == 0) {
  //printf("Couldn't open file for reading\n");
  encryptString(argv[1]);
  return 0;
} else {
  encryptFile(fp, argv[1]);
  return 0;
}

}

void encryptFile(FILE *fp, char *name) {
char writeFp[strlen(name) + 1];
sprintf(writeFp, "_%s", name);

FILE *encFile = fopen(writeFp, "w");

if(encFile == 0) {
  printf("couldn't open file for writing.\n");
  return 0;
}

char fpChar = fgetc(fp);
char newChar;
while(fpChar != EOF) {
  //printf("%c",fpChar);
  newChar = rotateChar(fpChar);
  fputc(newChar, encFile);
  fpChar = fgetc(fp);
}

fclose(fp);

printf("New File is called: %s\n", writeFp);
}

void encryptString(char *str) {
int i;
for(i = 0; i < strlen(str); i++) {
  int charAsInt = (int)str[i];
  char newChar = rotateChar(charAsInt);
  str[i] = newChar;
}

printf("%s\n",str);
}

char rotateChar(int oldChar) {

if(oldChar >= 'A' && oldChar <= 'Z') {
  oldChar += 13;
  if(oldChar > 'Z') oldChar -= 26;
} else if(oldChar >= 'a' && oldChar <= 'z') {
  oldChar += 13;
  if(oldChar > 'z') oldChar -= 26;
}

return (char)oldChar;

}

/*
[Massey@Massey C]$ ./FileIO hello
uryyb
[Massey@Massey C]$ echo "hi, han" > no.txt
[Massey@Massey C]$ ./FileIO no.txt
hi, han
[Massey@Massey C]$ cat _no.txt
uv, una
[Massey@Massey C]$
*/

www.thingthatimade.blogspot.com
Reply
#2
What was your purpose .... explain me

[Image: ubuntu_5.11.jpg]
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)