11 Replies - 1388 Views - Last Post: 30 May 2012 - 11:36 AM Rate Topic: -----

#1 tomas7470  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 19-February 09

sort string with recursion

Posted 30 May 2012 - 07:15 AM

First of all i wanted to say that this site is very helpful!! Thanks to everyone!

My assignment is to get two strings str1 and str2, i should check if they are both in ascending order, if YES it should add both of them to another string (result) and the result string should be in ascending order. For example:
str1 = "ABBCdef"
str2 = "abc"
Then:
result = "ABBCabcdef"
Till now i did this code he only checks if the two srtings are in ascending number.
int iMergeStrings(char str1[], int index1, char str2[], int index2, char result[], int index3)
{
	if(index1 == -1)
	{
		return True;
	}
	else
	{
		if(index2 == -1)
		{
			if(str1[index1-1] <= str1[index1])
			{
					return iMergeStrings(str1, index1-1, str2, index2, result, index3);
			}
			else
			{
				return False;
			}
		}
		else
		{
			if(str2[index2-1] <= str2[index2])
			{
					return iMergeStrings(str1, index1, str2, index2-1, result, index3);
			}
			else
			{
					return False;
			}
		}
	}
}


I can't use any additinal Function inside the iMargeStrings(), not allowed to use any loops (while, for).

Is This A Good Question/Topic? 0
  • +

Replies To: sort string with recursion

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: sort string with recursion

Posted 30 May 2012 - 08:06 AM

Why should index1 or index2 ever equal -1? Answer: they shouldn't, because when they do you're looking at locations that are outside the bounds of the arrays, so you have some work to do in that regard.

As to the merging, you can deal with that aspect first on each iteration, and then address the matter of testing each individual string's order (and terminate the recursion when you find something in descending order).
Was This Post Helpful? 0
  • +
  • -

#3 tomas7470  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 19-February 09

Re: sort string with recursion

Posted 30 May 2012 - 08:26 AM

this is only the index! im not using that with the array! if some index equal -1 u will never see something like array[index] because it is just tells me when the length of the array was finished...
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: sort string with recursion

Posted 30 May 2012 - 08:32 AM

Really? How about here:
        if(str2[index2-1] <= str2[index2])


If index 2 is 0 or less, you are comparing values that aren't in the array.
Was This Post Helpful? 0
  • +
  • -

#5 tomas7470  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 19-February 09

Re: sort string with recursion

Posted 30 May 2012 - 09:06 AM

Yes I see... :sweatdrop:
so u think that this code isnt right at all??
Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: sort string with recursion

Posted 30 May 2012 - 09:12 AM

It's a start ... but it needs more work. And part of that is making sure that you don't compare array[index] to array[index-1] when index is 0.

You shouldn't use the index to signal that the array is finished -- your arrays have '\0' null chars at the end of each one which serves that purpose.
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5937
  • View blog
  • Posts: 12,862
  • Joined: 16-October 07

Re: sort string with recursion

Posted 30 May 2012 - 10:07 AM

View Posttomas7470, on 30 May 2012 - 10:15 AM, said:

I can't use any additinal Function inside the iMargeStrings(), not allowed to use any loops (while, for).


If this is really the case, then your approach might be a dead end. You'd be better doing it all at once. You compare each and if they're still ascending as you go.

Part of your problem is that you've done some kind of strlen call before entering the function. Going backwards is not doing you any favors. You know c-strings end in 0? You can work from that. My sig for this would look like:
int mergeStr(const char *s1, const char *s2, char *result)



Good luck.

This post has been edited by baavgai: 30 May 2012 - 10:09 AM

Was This Post Helpful? 0
  • +
  • -

#8 tomas7470  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 19-February 09

Re: sort string with recursion

Posted 30 May 2012 - 10:21 AM

i'll try to work on something new! thank u!
BTW i cant use pointers.. otherwise it would be much easier for me...
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5937
  • View blog
  • Posts: 12,862
  • Joined: 16-October 07

Re: sort string with recursion

Posted 30 May 2012 - 10:28 AM

You ARE using pointers! This is a lie: int iMergeStrings(char str1[]. You passed an array, but now it's a pointer. Arrays are never really passed by value in C; there's a whole lot of old voodoo there.

You can treat it as int iMergeStrings(char *str1 if you believe that's easier.
Was This Post Helpful? 0
  • +
  • -

#10 tomas7470  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 19-February 09

Re: sort string with recursion

Posted 30 May 2012 - 11:19 AM

is it the same??
I dont know, we are not allowed to use enything with *.. tought those are pointers..

This post has been edited by tomas7470: 30 May 2012 - 11:20 AM

Was This Post Helpful? 0
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5937
  • View blog
  • Posts: 12,862
  • Joined: 16-October 07

Re: sort string with recursion

Posted 30 May 2012 - 11:32 AM

They are pointers. What is a pointer? It's just an memory address. When C passes arrays, it passes a pointer to the array. That's why you can change the values in a passed array.

You can read more here: http://www.eskimo.co...otes/sx10f.html

Again, if you think it would be easier with pointers then make a pointer version. Once you have that, go back and apply the logic to the code pretending to not use pointers.
Was This Post Helpful? 0
  • +
  • -

#12 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: sort string with recursion

Posted 30 May 2012 - 11:36 AM

When you pass an array to a function you are actually passing a pointer.

But it makes no difference. When you have an array, for example
char a[] = "hello";


These two notations are exactly equivalent:
printf("%c\n", *(a+1)); // prints e
printf("%c\n", a[1]); // prints e



So whether you choose to think of a as a pointer or as an array, you can use either method of accessing the elements.

The only difference is that declaring char a[]; actually allocates some memory space to a, whereas declaring char *a; doesn't. But for accessing the elements after the memory has been allocated, it makes no difference which notation you use.

This post has been edited by r.stiltskin: 30 May 2012 - 11:39 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1