problem to use srtcat() to add string to string

54 Views Asked by At

~~ z_goggamal-input.txt ~~

Make me a sandwich.

What? Make it yourself.

Sudo make me a sandwich.

Okay.

this is the original text file. i need to change this file

~~like this ~~
Make me a sosandwich.

Whatot? Make itot yourorsoself.

SoSudo make me a sosandwich.

Okay.

it means consonants are changed to '<consonant> o <consonant>', only required to transform 3 consonants, specifically R, S and T (both lower case, and upper case).

so i add the transform_char() on the given code.

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>

#define D_BUFSIZE 4096
#define NUM_ARGS_3 3
#define NUM_ARGS_4 4

#define ERROR_ARGS 1
#define ERROR_OPEN_R 2
#define ERROR_OPEN_W 3
#define ERROR_WRITE 4
#define ERROR_CALLOC 5
#define SUCCESS 0
#define ERROR -1

int BUFSIZE = D_BUFSIZE;

// Function to transform a character to Goggamal language
char transform_char(char c) {
        if (c == 'r'){
        char or[10] = "or";
        char plusr[10] = "r";
        strcat (plusr,or);
        return *plusr;
    }
 }

int main(int argc, char *argv[]) {
    char *prog_name = strdup(argv[0]);
    int input_fd, output_fd;
    ssize_t return_num_bytes_write, return_num_bytes_read;
    char *buffer;
    char *fn = strdup("makegoggamalA");

    if (argc != NUM_ARGS_3 && argc != NUM_ARGS_4) {
        printf("%s: usage: makegoggamalA source_file target_file [BUFSIZE]\n", prog_name);
        return ERROR_ARGS;
    }

    if (argc == NUM_ARGS_4) {
        BUFSIZE = atoi(argv[3]);
    }

    if (argc == NUM_ARGS_3) {
        BUFSIZE = 1;
    }

    buffer = (char *) calloc(BUFSIZE, sizeof(char));
    if (buffer == NULL) {
        perror("calloc");
        return ERROR_CALLOC;
    }

    input_fd = open(argv[1], O_RDONLY);
    if (input_fd == ERROR) {
        perror("open");
        return ERROR_OPEN_R;
    }

    output_fd = open(argv[2], O_WRONLY | O_CREAT, 0644);
    if (output_fd == ERROR) {
        perror("open");
        return ERROR_OPEN_W;
    }

    int total_read = 0;
    int total_write = 0;

    while ((return_num_bytes_read = read(input_fd, buffer, BUFSIZE)) > 0) {
        for (int i = 0; i < return_num_bytes_read; i++) {
            buffer[i] = transform_char(buffer[i]);
        }
        return_num_bytes_write = write(output_fd, buffer, (ssize_t) return_num_bytes_read);
        if (return_num_bytes_read != return_num_bytes_write) {
            perror("write");
            return ERROR_WRITE;
        }
        total_read += return_num_bytes_read;
        total_write += return_num_bytes_write;
    }

    close(input_fd);
    close(output_fd);
    free(buffer);
    free(prog_name);
    printf("read %'d bytes and wrote %'d bytes done...\n", total_read, total_write);
    return SUCCESS;
}

I just did 'r' character only so far.

I thought i could use strcat function to put "or" after "r"

but when i check the txt file after compiling, there was no error but nothing changed.

output is just the same as original file.

what should i do to edit the code to make <consonant> o <consonant> ?

or is there any other way that I can use instead srtcat function?

1

There are 1 best solutions below

0
Fe2O3 On

"or is there any other way that I can use instead strcat function?"
Yes there is another way...

The code below performs the operation you describe. For simplicity, the original text is hard coded instead of being read from a file. (Replace this with fgetc() to read in one character at a time.) The output is being printed to stdout. (Replace these with fprintf().)

#include <stdio.h>

int main( void ) {
    char *src =
        "Make me a sandwich.\n"
        "What? Make it yourself.\n"
        "Sudo make me a sandwich.\n"
        "Okay.\n";

    for( ; *src; src++ )
        switch( *src ) {
            case 'r':
            case 'R':
            case 's':
            case 'S':
            case 't':
            case 'T':
                putchar( *src );
                putchar( 'o' );
            /* FALLTHROUGH */
            default:
                putchar( *src );
                break;
        }

    return 0;
}

Adding counters of source characters and output characters then follows.

Make me a sosandwich.
Whatot? Make itot yourorsoself.
SoSudo make me a sosandwich.
Okay.

EDIT Noticed that OP example reuses the same case for both 'ends' of the target character being expanded. Makes code even simpler...