我如何用C中的数字替换字母?
How can I replace letters with numbers in C?
所以我想为学校项目制作一个加密程序,例如我想用 C 中的 54321 替换字母 'a' with:12345 'b',如何我能做到吗?我不是最好的,到目前为止我的代码:
eFile = fopen("Message.txt", "ab+");
while(!feof(eFile))
{
fscanf(eFile,"%c",&message);
}
例如,如果我将 apple 这个词写入文本文件,让程序逐个字母地扫描它,并用 5 位数字替换每个字母(我已经预定义了它们)示例:apple = 12342 69865 69865 31238 43297
我不确定你的策略是否可以称为加密,但它很容易实现
通过查找 table.
只需将替换内容放入 int table 中,如下所示:
int map[]={ //size will be autoinferred to fit the indices
['a']=12342,
['p']=69865,
['l']=31238,
['e']=43297,
//you should preferrably have an entry for each value of char
};
并用它来打印替换。
int c;
while(EOF!=(c=fgetc(inputFile)))
if(0>(outPutfile,"%d \n", map[c]))
return -1;
由于新文件的大小会发生不可预测的变化,因此可能
输出到一个临时文件然后将它移动到
成功完成后原件的位置。
一个更好的主意可能是简单地忘记就地文件重写
并简单地读取 stdin
并写入 stdout
– 这将使程序也能很好地处理流,并且可能的包装脚本可以在事后将其转换为就地翻译器(通过临时文件)如果需要。
- 从输入中逐个字符读取
- 使用简单数组在字符与数字或字符串之间进行转换,或者为此使用处理函数
- 打印那个数字
- 替换并不容易,最简单的方法是创建一个临时文件,将数字写入该临时文件,而不是将临时文件复制到原始文件并删除临时文件。
_
#include <stdio.h>
#include <assert.h>
int cipher_table[255] = {
['a'] = 12342,
['p'] = 69865,
['l'] = 31238,
['e'] = 43297,
// so on...
};
int main()
{
FILE *fin = stdin; // fopen(..., "r");
assert(fin != NULL);
FILE *fout = stdout; // tmpfile();
assert(fout != NULL);
for (int c; (c = getc(fin)) != EOF;) {
if (c == '\n') {
if (fputc('\n', fout) == EOF) {
fprintf(stderr, "Error writing to file fout with fputc\n");
return -1;
}
continue;
}
if (fprintf(fout, "%5d ", cipher_table[c]) < 0) {
fprintf(stderr, "Error writing to file fout with fprintf\n");
return -1;
}
}
close(fin);
close(fout);
return 0;
}
所以我想为学校项目制作一个加密程序,例如我想用 C 中的 54321 替换字母 'a' with:12345 'b',如何我能做到吗?我不是最好的,到目前为止我的代码:
eFile = fopen("Message.txt", "ab+");
while(!feof(eFile))
{
fscanf(eFile,"%c",&message);
}
例如,如果我将 apple 这个词写入文本文件,让程序逐个字母地扫描它,并用 5 位数字替换每个字母(我已经预定义了它们)示例:apple = 12342 69865 69865 31238 43297
我不确定你的策略是否可以称为加密,但它很容易实现 通过查找 table.
只需将替换内容放入 int table 中,如下所示:
int map[]={ //size will be autoinferred to fit the indices
['a']=12342,
['p']=69865,
['l']=31238,
['e']=43297,
//you should preferrably have an entry for each value of char
};
并用它来打印替换。
int c;
while(EOF!=(c=fgetc(inputFile)))
if(0>(outPutfile,"%d \n", map[c]))
return -1;
由于新文件的大小会发生不可预测的变化,因此可能 输出到一个临时文件然后将它移动到 成功完成后原件的位置。
一个更好的主意可能是简单地忘记就地文件重写
并简单地读取 stdin
并写入 stdout
– 这将使程序也能很好地处理流,并且可能的包装脚本可以在事后将其转换为就地翻译器(通过临时文件)如果需要。
- 从输入中逐个字符读取
- 使用简单数组在字符与数字或字符串之间进行转换,或者为此使用处理函数
- 打印那个数字
- 替换并不容易,最简单的方法是创建一个临时文件,将数字写入该临时文件,而不是将临时文件复制到原始文件并删除临时文件。
_
#include <stdio.h>
#include <assert.h>
int cipher_table[255] = {
['a'] = 12342,
['p'] = 69865,
['l'] = 31238,
['e'] = 43297,
// so on...
};
int main()
{
FILE *fin = stdin; // fopen(..., "r");
assert(fin != NULL);
FILE *fout = stdout; // tmpfile();
assert(fout != NULL);
for (int c; (c = getc(fin)) != EOF;) {
if (c == '\n') {
if (fputc('\n', fout) == EOF) {
fprintf(stderr, "Error writing to file fout with fputc\n");
return -1;
}
continue;
}
if (fprintf(fout, "%5d ", cipher_table[c]) < 0) {
fprintf(stderr, "Error writing to file fout with fprintf\n");
return -1;
}
}
close(fin);
close(fout);
return 0;
}