Question: #include #include #include char *reverse_string(char *line) { // initialize a new string char *reverse_string = (char *)malloc(sizeof(char *)); // if malloc failed print error and

#include #include #include

char *reverse_string(char *line) { // initialize a new string char *reverse_string = (char *)malloc(sizeof(char *)); // if malloc failed print error and exit(1) if (reverse_string == NULL) { fprintf(stderr, "malloc failed "); exit(1); } // start from the end int end_point_of_last_word_read = strlen(line) - 1; // reverse string index start int reverse_string_index = 0; // loop from end of the input string to the start for (int i = strlen(line) - 1; i >= 0; i--) { // if a complete word is passed if (line[i] == ' ') {

// start from the current index +1 to end of the last word index for (int j = i + 1; j

// add a space after word is copied reverse_string[reverse_string_index++] = ' '; // new index of last word read= present index-1 end_point_of_last_word_read = i - 1; } }

// Finally add the last word for (int i = 0; i

int reverse(int argv, char **argc) {

// more than 3 arguments show help and exit if (argv > 3) {

fprintf(stdout, "usage: reverse "); exit(1); }

// if 3 arguments passed else if (argv == 3) { // check if input file and output file are same if (strcmp(argc[1], argc[2]) == 0) { // if same then show error and exit fprintf(stderr, "%s", "Input and output file must differ "); exit(1); }

// open input and output file FILE *inputFile; inputFile = fopen(argc[1], "r"); FILE *outputFile; outputFile = fopen(argc[2], "w");

// if opening of files failed show error and exit if (inputFile == NULL) { fprintf(stderr, "error: cannot open file \'%s\' ", argc[1]); exit(1); }

if (outputFile == NULL) { fprintf(stderr, "error: cannot open file \'%s\' ", argc[2]); exit(1); }

// read lines from input file char *line = NULL; size_t len = 0; ssize_t line_size = 0; while ((line_size = getline(&line, &len, inputFile)) != -1)

{ // remove the newline character line[line_size - 1] = '\0'; // call reverse string function on the line read char *reverse_str = reverse_string(line);

// write the reversed line to output file fprintf(outputFile, "%s ", reverse_str); } }

// if 2 arguments are passed else if (argv == 2) { // open input file FILE *inputFile; inputFile = fopen(argc[1], "r");

// if input file opening failed show error and exit if (inputFile == NULL) { fprintf(stderr, "error: cannot open file \'%s\' ", argc[1]); exit(1); } // read line from input file char *line = NULL; size_t len = 0; ssize_t line_size = 0;

///////////////////////////////////////////////// while running , it got error on the while line below while ((line_size = getline(&line, &len, inputFile)) != -1) { // remove newline character from line line[line_size - 1] = '\0'; // print to standard output the reverse of the line read fprintf(stdout, "%s ", reverse_string(line)); } }

// if 1 argument else if (argv == 1) { // get input string from standard input fprintf(stdout, "Enter a string:"); // initialize string to store user input char *str = (char *)malloc(sizeof(char *)); // if mallac failed show error and exit if (str == NULL) { fprintf(stderr, "malloc failed "); exit(1); } // get the user input gets(str); // print the reverse of the string fprintf(stdout, "%s ", reverse_string(str)); } return 0;

}

while running, it got an error as a picture. can anyone help in c

#include #include #include char *reverse_string(char *line) { // initialize a new string

char *reverse_string = (char *)malloc(sizeof(char *)); // if malloc failed print error

Logs & others Code::Blocks X Search results X ccc x Build log x Build messages X CppCheck/Vera++ X CppCheck/Vera++ messages X Cscope X Debugger File Line 107 107 107 C:\Users\Vinh... C:\Users\Vinh... C:\Users\Vinh, C:\Users\Vinh.. C:\Users\ Vinh, C:\Users\Vnh... C:\Users\Vinh... C:\Users\Vinh. Message === Build: Debug in reverse (compiler: GNU GCC Compiler) === In function 'reverse': error: 'read' undeclared (first use in this function); did you mean 'fread'? note: each undeclared identifier is reported only once for each function it appears in warning left-hand operand of comma expression has no effect [-Wunused-value] error: expected expression before warning left-hand operand of comma expression has no effect [-Wunused-value] error: expected '}' before 'else' warning: implicit declaration of function 'getline'; did you mean 'getenv'? [-Wimplici... === Build failed: 3 error(s), 3 warning(s) (0 minute(s), 0 second(s) ) === 'FILE 107 107 121 137 reverse.c:161:1: warning: implicit declaration of function 'gets' [-Wimplicit-fu nction-declaration] gets (str); /tmp/ccWmmLhm.o: In function 'reverse': ceverse.c: (.text+0x445): warning: the gets' function is dangerous and should no t be used. /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crti.o: In function start': (.text+0x20): undefined reference to 'main' collect2: error: ld returned i exit status

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!