C language character replacement

Function ReadDat () to achieve reading an article in English from the file ENG.IN, deposited into an array of strings in xx; please prepare function encryptChar (), according to the substitutes given all the characters in the array xx be substituted, into an array corresponding to the positions xx still on, after the function call WriteDat () to result in PS1.DAT xx output to a file.

Alternatively the relationship: f (p) = p * 11 mod 256 (p is the ASCII value of a single character array, f (p) is the ASCII value of the new character after calculation), if calculated f (p) value of less than or equal to 32 or greater than 130, then the character unchanged, otherwise f (p) corresponding to the character instead. (Note that the intermediate variable with the unsigned integer), the format of the original data files are: the width of each row is less than 80 characters.
#include <stdio.h>

#include <string.h>

#include <conio.h>

#include <ctype.h>

unsigned char xx[50][80];

int maxline = 0; / total number of rows of articles /

int ReadDat(void);

void WriteDat(void);

void encryptChar()

{

int i,j;

for(i=0;i<maxline;i++)

for(j=0;j<strlen(xx[i]);j++)

if(xx[i][j]*11%256<=32||xx[i][j]*11%256>130) continue;

else xx[i][j]=xx[i][j]*11%256;

}

void main()

{

clrscr();

if (ReadDat ()) {printf ( "Can not open ENG.IN data file \ n \ 007!");

return;

}

encryptChar();

WriteDat();

}

int ReadDat(void)

{

FILE *fp;

int i=0;

unsigned char *p;

if((fp=fopen(“eng.in”,“r”))==NULL) return 1;

while(fgets(xx[i],80,fp)!=NULL){

p=strchr(xx[i],’\n’);

if§*p=0; i++; } maxline=i;

fclose(fp); return 0;

}

void WriteDat(void)

{

FILE *fp;

int i;

fp = fopen ( "ps1.dat", "w");

for(i=0;i<maxline;i++){

printf("%s\n",xx[i]);

fprintf(fp,"%s\n",xx[i]);

}

fclose(fp);

}

Published 239 original articles · won praise 3 · Views 3148

Guess you like

Origin blog.csdn.net/it_xiangqiang/article/details/105176926