Using Recursion To Compare Strings To Determine Which Comes First Alphabetically Java

后端 未结 3 1574
旧巷少年郎
旧巷少年郎 2021-01-27 02:25

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 us

3条回答
  •  伪装坚强ぢ
    2021-01-27 02:50

    #include
    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("\ncount is %d %d \n",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("\n%c %c",s1[i],s2[i]);
    int sum=0,count =1;
    if((s1[i] != '\0')|| (s2[i]!='\0'))
    {
    if (s1[i] == s2[i])
    {
    return (count += comp(s1,s2,++i));
    }
    else
    {
    return 0;
    }
    }
    else
    {
    return 0;
    }
    return count;

    }

提交回复
热议问题