【问题标题】:Strlwr function - getting an error in xcode 9.2Strlwr 函数 - 在 xcode 9.2 中出现错误
【发布时间】:2018-07-07 11:45:59
【问题描述】:

我正在尝试将字符串从大写转换为小写以检查它是否是回文,但是我不断收到错误:

“函数声明不是原型”

我已经在标题中添加了#include <string.h>,但它仍然不起作用。我该如何解决这个问题?

这是代码:

int main (void)
{
    char *user_string, *user_string_rev;
    
    /* the malloc function is used to make sure that enough memory is allocated for the string and that it does not overwrite memory boxes of other variables. */
    user_string= (char*)malloc(BUFF_SIZE*sizeof(char));
    user_string_rev= (char*)malloc(BUFF_SIZE*sizeof(char));
    printf("Please enter a string:");
    fgets(user_string,BUFF_SIZE, stdin); /* fgets function take the string the user inputs and stores it into user_string. */
    user_string_rev=strcpy(user_string_rev, user_string); /*the strcpy takes the string the user inputs and copies it to user_string_rev. */
    strlwr(user_string_rev);
    
    palindrome_check(user_string,user_string_rev); /*this is the palindrome function used to check if the two strings are palindromes, it intakes two arguments, the two strings and does not return anything. */
    
    return 0;
    
}

【问题讨论】:

  • strlwr 不是标准函数。据我所知,这是微软独有的东西。 see this question for a replacement
  • 它说要使用 tolower 功能,但是这不起作用您对如何解决这个问题有任何见解吗?
  • 在调用任何堆分配函数时:(malloc, calloc, realloc) 1) 始终检查 (!=NULL) 返回值以确保操作成功。操作不成功时,使用perror()输出附上的文字和系统认为错误发生的原因stderr。 2)在C语言中,返回类型为void*,可以赋值给任意指针。强制转换只会使代码混乱,使其更难以理解、调试等。
  • 表达式:sizeof(char) 在 C 标准中定义为 1。将任何内容乘以 1 无效。建议将该表达式从参数中删除到函数中:malloc()
  • 关于:fgets(user_string,BUFF_SIZE, stdin); 函数 fgets() 将最后的 '\n'(换行符)放在输入缓冲区中。 (通常)最好删除换行符。一种方法是:char *newline = strchr( user_string, '\n' ); if( newline ) { *newline = '\0'; }

标签: c string function


【解决方案1】:

替换:

strlwr(user_string_rev);

这不是标准功能:

int i = 0;
while (user_string_rev[i])
{
    if (isalpha(user_string_rev[i]))
        user_string_rev[i] |= 32;
    ++i;
}

不要忘记在 .c 文件的顶部添加 ctype 标头以使用 isalpha:

#include <ctype.h>

【讨论】:

  • 我最终做到了,并且成功了:*user_string_rev= tolower((unsigned char)*user_string_rev); *user_string=tolower((unsigned char)*user_string);
  • |= 32 不好。使用tolower(),可能没有检查,因为它不翻译非字母字符。
  • @MatteoCiccozzi:考虑将其添加为答案。
  • @MatteoCiccozzi,tolower() 一次只能处理一个字符,而不是整个字符串。
  • 好吧,它已经在这里发布了stackoverflow.com/questions/16909302/…。您确实不需要检查,因为 libc 会做同样的事情。两者都同样快。
【解决方案2】:

以下建议的代码:

  1. 将 cmets 合并到问题中
  2. 干净编译
  3. 正确检查错误
  4. 将仅是换行符的字符串视为不是回文

现在建议的代码:

#include <stdio.h>    // getline(), printf()
#include <stdlib.h>   // free()
#include <ctype.h>    // tolower()
#include <string.h>   // strlen(), strchr()

// prototypes
void palindrome( char *, size_t length );


int main( void )
{
    char   *inputStr = NULL;
    size_t  lengthStr = 0;

    printf("Please enter a string:");
    if( -1 != getline( &inputStr, &lengthStr, stdin ) )
    {
        size_t length  = strlen( inputStr );
        for( size_t i = 0; i < length; i++ )
        {
            inputStr[i] = (char)tolower( inputStr[i] );
        }


        char *newline = strchr( inputStr, '\n' );
        if( newline )
        {
            *newline = '\0';
            length--;
        }

        palindrome( inputStr, length );
    }

    free( inputStr );

    return 0;
}


void palindrome( char stringToCheck[], size_t length )
{
    size_t index = length - 1;  // don't check NUL terminator byte
    size_t i;

    for( i = 0; i < index; i++ )
    {
        if( stringToCheck[i] != stringToCheck[ index ] )
        {
            break;
        }

        index--;
    }

    if( i < index )
    {
        printf( "%s is not a palindrome\n", stringToCheck );
    }

    else
    {
        printf( "%s is a palindrome\n", stringToCheck );
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多