# sort string with recursion

Page 1 of 1

## 11 Replies - 2311 Views - Last Post: 30 May 2012 - 11:36 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=281202&amp;s=03a3bf521fa5d877d7edc0e4fed655bf&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 tomas7470

Reputation: 1
• 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

• D.I.C Lover

Reputation: 1833
• 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).

### #3 tomas7470

Reputation: 1
• 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...

### #4 r.stiltskin

• D.I.C Lover

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

## Re: sort string with recursion

Posted 30 May 2012 - 08:32 AM

```        if(str2[index2-1] <= str2[index2])

```

If index 2 is 0 or less, you are comparing values that aren't in the array.

### #5 tomas7470

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

## Re: sort string with recursion

Posted 30 May 2012 - 09:06 AM

Yes I see...
so u think that this code isnt right at all??

### #6 r.stiltskin

• D.I.C Lover

Reputation: 1833
• 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.

### #7 baavgai

• Dreaming Coder

Reputation: 6608
• Posts: 13,948
• Joined: 16-October 07

## Re: sort string with recursion

Posted 30 May 2012 - 10:07 AM

tomas7470, 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

### #8 tomas7470

Reputation: 1
• 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...

### #9 baavgai

• Dreaming Coder

Reputation: 6608
• Posts: 13,948
• 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.

### #10 tomas7470

Reputation: 1
• 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

### #11 baavgai

• Dreaming Coder

Reputation: 6608
• Posts: 13,948
• 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.

### #12 r.stiltskin

• D.I.C Lover

Reputation: 1833
• 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