half working translate

This commit is contained in:
Christoph J. Scherr 2024-05-16 13:45:36 +02:00
parent add1185efc
commit 9d148639a7
1 changed files with 46 additions and 15 deletions

View File

@ -2,9 +2,33 @@
* Read from stdin. replace all occurences of arg 1 with arg2.
*/
#include <string.h>
const int BUF_SIZE = 1024;
char translate_c(char src, char s1[], char s2[]) {
int n = strlen(s1);
for (int i = 0; i < n; i++) {
if (src == s1[i]) {
return s2[i];
}
}
return src;
}
void translate(char source[], char target[], char s1[], char s2[]) {
int n = strlen(source);
for (int i = 0; i < n; i++) {
target[i] = translate_c(source[i], s1, s2); // FIXME: this is buggy
}
target[n] = '\0';
}
#include <stdio.h>
int main(int argc, char *argv[])
{
int main(int argc, char *argv[]) {
char *translator[2];
if (argc != 3) {
printf("not enough arguments");
@ -15,7 +39,14 @@ int main(int argc, char *argv[])
printf("translator0: %s\n", translator[0]);
printf("translator1: %s\n", translator[1]);
// TODO: translate from stdin and print to stdout
char source[BUF_SIZE];
char target[BUF_SIZE];
while (scanf("%s", source) != EOF) {
translate(source, target, translator[0], translator[1]);
printf("%s\n", target);
}
return 0;
}