【发布时间】: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'; }