Hello.

I would very much appreciate if someone would aid me with a hint.
My problem is as follows :
read two arrays of chars and merge them into a third array like so :
ARR1 :123abc
ARR2: rt678iogl
ARR3:1r2t36a7b8ciogl

The problem must be solved without pointers and string.h

Basically Im stuck at the the above merging procedure. I myself am not a very experienced programmer as you can notice, but a hint in the right direction would be very much appreciated. No code please .
Thank you very much !

>>read two arrays of chars
Read them from where? the keyboard? a data file? sockets? serial rs232 port?

I know of no way to do that assignment without using either pointers or fucntions in string.h. Maybe you misunderstood the assignment. If not, then you should get a different teacher.

>>read two arrays of chars
Read them from where? the keyboard? a data file? sockets? serial rs232 port?

Oh my heavens, no :) Just from a simple ps2 keyboard. Ive used a getchar for that. The main prob is that I have index 0 at array A that has to become index 0 at the merged array. And then I also have index 0 at array B that has to become index 1 at the merged array. And this is where I need that bit of a push.

Hello.

I would very much appreciate if someone would aid me with a hint.
My problem is as follows :
read two arrays of chars and merge them into a third array like so :
ARR1 :123abc
ARR2: rt678iogl
ARR3:1r2t36a7b8ciogl

The problem must be solved without pointers and string.h

Basically Im stuck at the the above merging procedure. I myself am not a very experienced programmer as you can notice, but a hint in the right direction would be very much appreciated. No code please .
Thank you very much !

Three states have to be handled:

1) array1 has more letters than array2
2) array2 has more letters than array1
3) both arrays have the same quantity of letters.

It's very useful to use one int variable as the iterator and index for the smaller arrays, and use another int variable as the indexer for the merged array3.

i and j set to 0 before the loop starts is good
then a
start your while loop
testing first that both array's have not reached the end of string char: '\0'

if either array has reached it's end, break out of the while loop, and remember which one still has data, by using a flag value on a variable: go1 or go2, maybe.

then set the values from array1 into array[3], increment j, and do the same with values from array2, and increment j again.

They both have data, or the program wouldn't have reached down this far.
increment i once, and loop back for the next pair of letter to be tested and assigned.

Then test if go1 equals one, then forget array2 (it's empty), and copy array1 remaining char's, into array3[j]

if go2 equals one then do the same, but use array2 as your source.

If you aren't allowed to even test for the end of string char, then use the sizeof(array1) and sizeof(array2), but it will copy EVERYTHING in them, even if it's after the end of string char.

I know of no way to do that assignment without using either pointers or fucntions in string.h. Maybe you misunderstood the assignment. If not, then you should get a different teacher.

Hm, I was left under the impression that it could be done that way. Nevertheless, I would be thankful for a hint concerning pointers then.

@Joey: I was thinking indexes as Adak suggest would not be allowed either. If its ok, then that is the best way to go.

@Adak: I think you've overcomplicated the program. Just copy the contents of arr1 into arr3, then the contents of arr2 into arr3 where arr1 left off. Two integers are needed, one for arr1/arr2 and the other for arr3. No need to check of the length if arr1 and arr2 are the same or different.

Thank you for your reply. I shall implement the given algorithm and post back with results.
Cheers !

Depends (and I hate to go "Clinton" on ya) ;) what your definition of "merge" is.

Joey's post showed an interleaving of char's, so if your arrays were thus:

char a[] = {"acegikmoqsuwy"};
  char b[] = {"bdfhjlnprtvxz -- It's working"};
  char c[80] = {'\0'};

You would wind up with the correct alphabet in c[] + " --It's working"

And of course, you can do this with just integers for indeces, and without using string h.

The whole program I described is 29 lines of code, including the #include line for stdio.h, and explicit lines for ++i and ++j, and the printout at the end. I just wrote it to clear up a nagging thought I had.

Works fine.

commented: Yes you are right. I had not noticed that. +28
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.