C 使用二进制搜索查找排序字符串中的第一个大写字母

C 使用二进制搜索查找排序字符串中的第一个大写字母,c,char,c-strings,uppercase,function-definition,C,Char,C Strings,Uppercase,Function Definition,我编写了以下代码以使用二进制搜索查找字符串中的第一个大写字母: char first_capital(const char str[], int n) { int begin = 0; int end = n - 1; int mid; while (begin <= end) { mid = (begin + end) / 2; if (mid == 0 && isupper(str[mid]))

我编写了以下代码以使用二进制搜索查找字符串中的第一个大写字母:

char first_capital(const char str[], int n)
{
    int begin = 0;
    int end = n - 1;
    int mid;
    while (begin <= end)
    {
        mid = (begin + end) / 2;
        if (mid == 0 && isupper(str[mid]))
        {
            return mid;
        }
        else if (mid > 0 && isupper(str[mid]) && islower(str[mid - 1]))
        {
            return mid;
        }
        if (islower(str[mid]))
        {
            begin = mid + 1;
        }
        else
        {
            end = mid - 1;
        }
    }
    return 0;
}
char首字母大写(const char str[],int n)
{
int begin=0;
int end=n-1;
int mid;
while(开始0&&isupper(str[mid])&&islower(str[mid-1]))
{
中途返回;
}
如果(岛下(str[mid]))
{
开始=中间+1;
}
其他的
{
结束=中间-1;
}
}
返回0;
}
目前,我的代码在测试时无法按预期工作。如果有人能告诉我哪里出了错,那会很有帮助

注意:输入字符串已排序(所有小写字母显示在大写字母之前)
const char str[]
是字符串,
int n
是字符串的长度

编辑:例如:
首字母大写(“abcBC”,5)
应返回
'B'

\include
#include <stdio.h>

/* This will find and return the first UPPERCASE character in txt
 * provided that txt is zero-or-more lowercase letters,
 * followed by zero-or-more uppercase letters.
 * If it is all lower-case letters, it will return \0 (end of string)
 * If it is all upper-case letters, it will return the first letter (txt[0])
 * If there are non-alpha characters in the string, all bets are off.
 */
char findFirstUpper(const char* txt)
{
    size_t lo = 0;
    size_t hi = strlen(txt);
    
    while(hi-lo > 1)
    {
        size_t mid = lo + (hi-lo)/2;
        *(isupper(txt[mid])? &hi : &lo) = mid;
    }
    
    return isupper(txt[lo])? txt[lo] : txt[hi];
}

int main(void)
{
    char answer = findFirstUpper("abcBC");
    printf("Found char %c\n", answer);
    return 0;
}
/*这将查找并返回txt中的第一个大写字符 *如果txt是零个或多个小写字母, *后跟零个或多个大写字母。 *如果全部为小写字母,则返回\0(字符串末尾) *如果全部为大写字母,则返回第一个字母(txt[0]) *如果字符串中有非字母字符,则所有下注都将取消。 */ char FINDFIRSTUPER(常量char*txt) { 尺寸=0; 尺寸=标准长度(txt); 同时(hi-lo>1) { 尺寸_tmid=lo+(hi-lo)/2; *(isupper(txt[mid])?&hi:&lo)=mid; } 返回isupper(txt[lo])?txt[lo]:txt[hi]; } 内部主(空) { char answer=findfirstuper(“abcBC”); printf(“找到字符%c\n”,答案); 返回0; }
您的逻辑完全正确,但返回了错误的值

char first_capital(const char str[], int n)
{
    int begin = 0;
    int end = n - 1;
    int mid;
    while (begin <= end)
    {
        mid = (begin + end) / 2;
        if(mid == 0 && isupper(str[mid]))
        {
            return mid;    // Here the index is returned not the character
        }
        else if (mid > 0 && isupper(str[mid]) && islower(str[mid-1]))
        {
            return mid;    // Same goes here
        }
        if(islower(str[mid]))
        {
            begin = mid+1;
        }
        else
        {
            end = mid - 1;
        }
    }
    return 0;
}
将给出15作为答案,这是字符Z的索引


如果要返回字符,请将
return mid
替换为
return str[mid]
,并返回“Z”。

如果函数处理字符串,则应删除第二个参数

函数应返回指向第一个大写字母的指针,如果字符串中不存在此类字母,则应返回空指针。也就是说,函数声明和行为应该类似于标准字符串函数的声明和行为
strchr
。唯一的区别是,函数不需要类型为
char
的第二个参数,因为搜索的字符由条件隐式定义为大写字符

另一方面,尽管函数的返回类型为char,但它返回一个整数,指定找到的字符的位置。此外,在未找到大写字符和字符串第一个位置包含大写字符的情况下,函数不会产生差异

此外,函数中的if-else语句太多

该函数可以按照下面的演示程序中所示的方式进行声明和定义

#include <stdio.h>
#include <string.h>
#include <ctype.h>

char * first_capital( const char s[] )
{
    const char *first = s;
    const char *last = s + strlen( s );
    
    while ( first < last )
    {
        const char *middle = first + ( last - first ) / 2;
        
        if ( islower( ( unsigned char )*middle ) )
        {
            first = middle + 1;
        }
        else
        {
            last = middle;
        }
    }
    
    return ( char * )( isupper( ( unsigned char )*first ) ? first : NULL );
}

int main(void) 
{
    const char *s = "";
    
    char *result = first_capital( s );
    
    if ( result )
    {
        printf( "%c at %zu\n", *result, ( size_t )( result - s ) );
    }
    else
    {
        printf( "The string \"%s\" does not contain an upper case letter.\n", s );
    }
    
    s = "a";
    
    result = first_capital( s );

    if ( result )
    {
        printf( "%c at %zu\n", *result, ( size_t )( result - s ) );
    }
    else
    {
        printf( "The string \"%s\" does not contain an upper case letter.\n", s );
    }
    
    s = "A";
    
    result = first_capital( s );

    if ( result )
    {
        printf( "%c at %zu\n", *result, ( size_t )( result - s ) );
    }
    else
    {
        printf( "The string \"%s\" does not contain an upper case letter.\n", s );
    }
    
    s = "abcdefA";
    
    result = first_capital( s );

    if ( result )
    {
        printf( "%c at %zu\n", *result, ( size_t )( result - s ) );
    }
    else
    {
        printf( "The string \"%s\" does not contain an upper case letter.\n", s );
    }
    
    s = "abAB";
    
    result = first_capital( s );

    if ( result )
    {
        printf( "%c at %zu\n", *result, ( size_t )( result - s ) );
    }
    else
    {
        printf( "The string \"%s\" does not contain an upper case letter.\n", s );
    }
    
    return 0;
}

你提议的那一类行不通;该返回语句返回0的内容是什么;意思?@ggorlen“abcABB”输出:A,不会是aAbBBc,会是abcabby你返回
mid
,而不是
str[mid]
@e2-e4我想这是因为习惯,不是故意的。感谢您指出:D@Jason你能接受其中一个答案吗?因为这个职位不需要更多的答案出于一种完美的感觉,我批准了ani1998ket的编辑。虽然我认为这会使代码对初学者的可读性稍差,但它确实防止了一种罕见的bug。这就是为什么我没有做相同的更正(Jason的代码:)
#include <stdio.h>
#include <string.h>
#include <ctype.h>

char * first_capital( const char s[] )
{
    const char *first = s;
    const char *last = s + strlen( s );
    
    while ( first < last )
    {
        const char *middle = first + ( last - first ) / 2;
        
        if ( islower( ( unsigned char )*middle ) )
        {
            first = middle + 1;
        }
        else
        {
            last = middle;
        }
    }
    
    return ( char * )( isupper( ( unsigned char )*first ) ? first : NULL );
}

int main(void) 
{
    const char *s = "";
    
    char *result = first_capital( s );
    
    if ( result )
    {
        printf( "%c at %zu\n", *result, ( size_t )( result - s ) );
    }
    else
    {
        printf( "The string \"%s\" does not contain an upper case letter.\n", s );
    }
    
    s = "a";
    
    result = first_capital( s );

    if ( result )
    {
        printf( "%c at %zu\n", *result, ( size_t )( result - s ) );
    }
    else
    {
        printf( "The string \"%s\" does not contain an upper case letter.\n", s );
    }
    
    s = "A";
    
    result = first_capital( s );

    if ( result )
    {
        printf( "%c at %zu\n", *result, ( size_t )( result - s ) );
    }
    else
    {
        printf( "The string \"%s\" does not contain an upper case letter.\n", s );
    }
    
    s = "abcdefA";
    
    result = first_capital( s );

    if ( result )
    {
        printf( "%c at %zu\n", *result, ( size_t )( result - s ) );
    }
    else
    {
        printf( "The string \"%s\" does not contain an upper case letter.\n", s );
    }
    
    s = "abAB";
    
    result = first_capital( s );

    if ( result )
    {
        printf( "%c at %zu\n", *result, ( size_t )( result - s ) );
    }
    else
    {
        printf( "The string \"%s\" does not contain an upper case letter.\n", s );
    }
    
    return 0;
}
The string "" does not contain an upper case letter.
The string "a" does not contain an upper case letter.
A at 0
A at 6
A at 2