English 中文(简体)
Using Recursion To Compare Strings To Determine Which Comes First Alphabetically Java
原标题:

I am trying to write a method that uses recursion to compare the strings str1 and str2 and determine which of them comes first alphabetically (i.e., according to the ordering used for words in a dictionary).

If str1 comes first alphabetically, the method should return int 1.

If str2 comes first alphabetically, the method should return the int 2.

If the two strings are the same, the method should return the int 0.

I know that there is a compareTo method in the Java API but i would like to know how to do this without this

This is what i have so far but i m not entirely sure how to proceeded

 } if (str1.length().equals(str2.length()))

         return 0;
    } else {
        (str.substring(1, str.length()));

Any ideas would be greatly appreciated

最佳回答

Make a method int recursiveCompare(String string1, String string2, int index). Initially call it with index = 0. Compare string1.charAt(index) and string2.charAt(index), and if they re different, return 1 or 2. If they re the same, return recursiveCompare(string1, string2, index + 1).

Of course, you ll have to check the lengths of string1 and string2 before calling charAt(index). If they both reach the end at the same time, they re equal, so return 0. Otherwise, return the number of the one that has ended.

And yeah, recursion is pretty much the worst way to do this, LOL.

问题回答

No recursion required... (Unless this is specifically required in the homework(?) assignement...)

As this looks a lot like homework, I ll just give a few hints

Use a integer variable, say i, to index from 0 to the length of shorter string. As long as str1[i] == str2[i], and the last index value hasn t been reached, increment i. If you do reach the last possible value for the index, then the shorter string comes first (or they are deemed equal if same length...)

Otherwise, compare this first character that differs, and decide which string is first accordingly... Could be as simple as:
return (str1[i] < str2[i]);

If recursion you must... (and it was readily said in other comments, this kind of problem is truly not a logical/valid candidate for recursion...)

The idea is to have a function with this kind of interface:

int  RecursCompare(string str1, string str2, int i)

and which calls itself, passing the same values for str1 an str2 and passing the next value for i (i+1), as long as str1[i] == str2[i] AND neither str1 or str2 is at its end. When this condidtion becomes false, recursion ends, and instead the function returns the appropriate value to signify tha Str1 is alphabetically before or after Str2.

#include<stdio.h>
main()
{
            char str1[100],str2[100];
            int i=0,k=0; 
            puts("Enter string 1");
            gets(str1);
            puts("Enter string 2");
            gets(str2);
i=comp(str1,str2,0);
           printf(" count is %d %d ",i,strlen(str1));
         (strlen(str1)==strlen(str2)) ?( (strlen(str1)==i) ? printf("Both are equal"):printf("Both are Not               equal")):printf("Both are Not equal");

}
int comp(char s1[], char s2[],int i)
{
printf(" %c %c",s1[i],s2[i]);
int sum=0,count =1;
if((s1[i] != )|| (s2[i]!= ))
{
if (s1[i] == s2[i])
{
return (count += comp(s1,s2,++i));
}
else
{
return 0;
}
}
else
{
return 0;
}
return count;

}





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签