[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1455
  • Last Modified:

how to use recursion to implement a function just like strcmp()

int compare(char str1[], char str2[]) {
 //int i=0;
 if(str1 < str2){
 return -1;
 }
 else if (str1 > str2){
 return 1;
 }
 else
 return compareString(str1+1, str2+1);
}
0
tim9232
Asked:
tim9232
1 Solution
 
andrewjbCommented:
What's the question?

(And you need to check if str1 and/or str2 are null else it'll never finish...)
0
 
Kent OlsenData Warehouse Architect / DBACommented:


Hi Paul,

Doing homework now?  :)

0
 
tim9232Author Commented:

int compare(char str1[], char str2[]) {
 int equal;
 if(*str1 < *str2){
 return -1;
 }
 else if (*str1 > *str2){
 return 1;
 }
 else{  
      equal= compare(str1+1, str2+1);
      if str1[]==str2[] ; //i don't know how to implement  
      return 0;
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Kent OlsenData Warehouse Architect / DBACommented:

Hi Tim,

The function looks complete without the if statement near the bottom.  Simply return 0.


Kent
0
 
Kent OlsenData Warehouse Architect / DBACommented:

Er... Sorry.

return 0 if *str1 is zero.  Otherwise, call comare() again.


Kent
0
 
aib_42Commented:
#1 sign that you should start looking for a cover: When the page editor judges and finds himself guilty.

Anyway, here's a tip for tim:

If the first characters of the two strings are different, you know right away that they are not equal and one comes after the other, correct?

What if the first characters are the same?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now