C 不使用标准库函数进行字符串比较

C 不使用标准库函数进行字符串比较,c,string,pointers,malloc,C,String,Pointers,Malloc,我是C编程新手。这只是一个初学者的问题。我试图在不使用标准函数的情况下实现字符串比较。这里我使用了动态内存分配和fgets()。但是没有输入第二个字符串。谁能帮我指出这个问题吗?代码如下所示 #include <stdio.h> #include <stdlib.h> int my_strcmp(char*, char*); int main() { int a, n; printf("enter the length of strings\n");

我是C编程新手。这只是一个初学者的问题。我试图在不使用标准函数的情况下实现字符串比较。这里我使用了动态内存分配和
fgets()
。但是没有输入第二个字符串。谁能帮我指出这个问题吗?代码如下所示

#include <stdio.h>
#include <stdlib.h>

int my_strcmp(char*, char*);

int main()
{
    int a, n;
    printf("enter the length of strings\n");
    scanf("%d",&n);
    getchar();

    char *s1 = (char *)malloc(n * sizeof(char));
    printf("enter the string1\n");
    fgets(s1, n, stdin);
    getchar();

    char *s2 = (char *)malloc(n * sizeof(char));
    printf("enter the string2\n");
    fgets(s2, n , stdin);

    if (s1 == NULL)
    {
        printf("malloc error!!");
    }

    if (s2 == NULL)
    {
        printf("malloc error!!");
    }

    a = my_strcmp( s1, s2);

    if (a == 0)
    {
        printf("the strings are equal");
    }
    else
    {
        printf("the strings are not equal");
    }
    free (s1);
    free (s2);
    return 0;
}

int my_strcmp( char *s1, char*s2)
{
    while (*s1)
    {
        if (*s1 == *s2)
        {
            s1++;
            s2++;
        }
        else
            break;
    }

    if ( *s1 == *s2)
    {
        return 0;
    }
    else if (*s1 > *s2)
    {
        return 1;
    }
    else
    {
        return -1;
    }
} 
#包括
#包括
int my_strcmp(char*,char*);
int main()
{
int a,n;
printf(“输入字符串的长度\n”);
scanf(“%d”和“&n”);
getchar();
char*s1=(char*)malloc(n*sizeof(char));
printf(“输入字符串1\n”);
fgets(s1,n,stdin);
getchar();
char*s2=(char*)malloc(n*sizeof(char));
printf(“输入字符串2\n”);
fgets(s2,n,stdin);
如果(s1==NULL)
{
printf(“malloc error!!”);
}
if(s2==NULL)
{
printf(“malloc error!!”);
}
a=我的strcmp(s1,s2);
如果(a==0)
{
printf(“字符串相等”);
}
其他的
{
printf(“字符串不相等”);
}
免费(s1);
免费(s2);
返回0;
}
int my_strcmp(字符*s1,字符*s2)
{
while(*s1)
{
如果(*s1==*s2)
{
s1++;
s2++;
}
其他的
打破
}
如果(*s1==*s2)
{
返回0;
}
否则,如果(*s1>*s2)
{
返回1;
}
其他的
{
返回-1;
}
} 

fgets的
n
参数是包含空终止符的缓冲区大小。因此,它最多读取n-1个字符,并用空终止符填充最后一个位置。您对
getchar
(在第一个
fgets
之后)的第二次调用会读取最后一个字符,而不是换行符,因此对
fgets
的第二次调用会提前停止,因为它会立即命中一个换行符

相反,您需要为每个字符串分配
n+1
字符,并使用
n+1
调用
fgets


此外,在尝试写入
s1
s2
之前,应检查
malloc
故障。代码可以这样更改

getchar(); //Fix
fgets(s1, n, stdin);
getchar();
char *s2 = (char *)malloc(n * sizeof(char));

从用户处获取后设置
n=n+1
。这是为了处理
'\0'
字符

对代码的次要注释
sizeof(char)
将是
1
,所以不要让事情变得不必要的复杂。我不知道为什么在
fgets
之后有
getchar
。特别是因为你不喜欢最后一个。在
malloc
停止程序失败期间,您应该以非零返回值退出
。在最终输出的末尾缺少
\n
。其他人用字符串解释了一个错误。这是使用字符串的C编程中的几个常见错误之一。您所说的“字符串长度”以及代码如何使用
n
是一个问题。说最长的“字符串”是“abc”。这需要一个3+1+1长的缓冲区。(3个代表“abc”
,1个代表
'\n'
,1个代表
'\0'
)。Do
char*s1=malloc(n+2)检查失败之前使用内存的好方法(
malloc
)。谢谢你的回答。如果你不介意的话,请解释一下细节:)“首先调用getchar,然后读取最后一个字符,而不是换行符”很可能读取换行符。第一个
getchar()
位于
scanf()
之后,当OP输入长度值时,肯定会消耗
'\n'
。第二个
getchar()
就是问题所在。它在两个
fgets()
之间的存在应该被消除。OP应该为每个字符串分配
n+2
,因为OP可能会查看输入的字符串的长度,而不包括
'\n'
@chux。通过使用
fgets
n+1
,您将不会得到换行。这就是
getchar
调用的目的(我猜)。