Skip to content
Advertisement

Need help Combining two C codes

My goal is to write a program that reads up to 100 characters from an input sentence, then from that stored data it will output 3 things. The reverse of that input sentence, the odd-numbered elements of the array, and even-numbered elements of the array. Therefore I have 3 printf functions. I have 2 separate codes to do this and can not figure out how to combine the two.

JavaScript

My second Code is:

JavaScript

Any input will be greatly appreciated!

So I have been messing around with it and taking all of your input. The code I have so far is below. I am getting the Odd and Even to work but when I input ‘Hello’ the String in Reverse Order says : ‘eH lleH’.. I’be been trying to break it down to see where that issue is arising but I think I am missing something.

JavaScript

Advertisement

Answer

As suggested, transferring a comment into an answer, and extending it (no 600-character limit in an answer).

You have code to read a line and chop off the newline; package that as function 1. Pass the array and array size into the function from the main program; return the length of the string that was read.

You have code to print the string in reverse; that can be simpler because you don’t need to worry about the newline any more because function 1 removed it. Package that as function 2.

You have code to find and print the odd-elements and even-elements of the string; package that as function 3. Again, you can pass the array and the string length into the function, which makes it easier to write.

Then your main program declares the variables and calls the functions. You should use more meaningful names for the functions that I specified — but in the code below, when I used functions, I stuck with the numeric names so you can see what corresponds to what clearly.

This is more or less the way to work with any code. You write functions that do separate identifiable tasks, and then you arrange for other code to call those functions in an appropriate sequence. You learn how to pass arguments to functions and return suitable values from functions and use those to guide the code. To some extent you’re already familiar with the process of using functions to do tasks. You use fgets() and strlen(), for example: they each have a different job to do, but the code is reusable, so it is provided by the standard C library. Your code won’t be quite as reusable, so it is unlikely to be part of the standard C library, but sometimes you’ll write code that is reusable enough that it should go in your own personal library.

Look at function 3. It does two jobs; maybe it should be split into two separate functions, one for the odd elements and one for the even elements. Or maybe there’s a general function lurking under there: print every Nth element of the string starting at the Mth element. Or maybe the function should simply copy N/M elements into a new array (passed to the function) and a coordinating function should call the extraction code and then print the code. It is often good to separate the I/O from the rest of the algorithm. Functions that mix the two are frequently less reusable. (Function 2 might also benefit from the separation of algorithm and I/O. A general purpose string reverser could be useful.)

Note, too, that there isn’t a single right result. A lot depends on context. But given the sample code, I think that three functions plus main is probably a suitable balance between competing interests. But it is important not to be scared of creating — and using — functions.


With all due respect, your proposed solution mixes up the processing and makes the code unnecessarily complex. This is what I meant by the three functions:

combo1.c

JavaScript

Sample run

JavaScript

On my Mac, the terminal is set to echo control characters, which is why the ^D appears where I typed Control-D to indicate the end of input. The final putchar('n'); is there to place the prompt on the next line.

combo2.c

Doing the combination as you did, you should still simplify the code (but making the code loop over multiple inputs is harder than in the code with functions, primarily because you have to get the prompt as well as the validation into the loop conditions — it is a primary reason for using functions). This code only does one cycle:

JavaScript

Sample run

JavaScript

combo3.c

And if you are adamant that you must do just one loop of the string once read, you can combine the two loops into one more cleanly like this:

JavaScript

Sample run

JavaScript

combo4.c

Going back to the original function-based version, I can rename the functions and provide some better separation of duty by controlling the output in the main() program and then reusing the functions. Note that I can use the print_reversed_string() three times, and for two of the uses, the string is not at the end of the line, even.

JavaScript

Example run

JavaScript

This is how functions make life easier. There is room to separate reverse_string() into a function that doesn’t print:

JavaScript

And then you can reverse a string and use the reversed value multiple times. You might even use:

JavaScript

so you could embed the reversal in a call to printf():

JavaScript

All these are valid variant designs on your original problem.

Advertisement