简体   繁体   English

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

[英]String compare without using standard library function

I am new to C programming. 我是C编程新手。 This is just a beginners question. 这只是一个初学者的问题。 I am trying to implement string compare without using standard function.Here i have used dynamic memory allocation and used fgets() . 我正在尝试不使用标准函数来实现字符串比较。这里我使用了动态内存分配和fgets() But the second string is not inputted. 但是没有输入第二个字符串。 Can anyone help me point out the problem? 谁能帮我指出问题所在? The code is given below. 代码如下。

#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;
    }
} 

The n parameter of fgets is the size of the buffer including null-terminator . fgetsn参数是缓冲区的大小, 包括null终止符 Thus, it reads up to at most n - 1 characters, and fills the last spot with a null-terminator. 因此,它最多读取n - 1字符,并用空终止符填充最后一个点。 Your second call to getchar (after the first fgets ) then reads that last character, not the newline, so the second call to fgets stops early because it immediately hits a newline. 您对getchar第二次调用(在第一个fgets )随后读取了最后一个字符, 而不是换行符,因此对fgets的第二次调用提早停止,因为它立即命中了换行符。

Instead, you'll want to allocate n + 1 characters for each string, and call fgets with n + 1 as well. 相反,您将需要为每个字符串分配n + 1字符,并同时调用n + 1 fgets

Also, you should check for malloc failure before trying to write to s1 or s2 . 另外, 尝试写入s1s2 之前 ,应检查malloc失败。

The code can be changed like this. 可以像这样更改代码。

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

Set n=n+1 after getting from user. 从用户处获取后设置n=n+1 This is to deal '\\0' character 这是为了处理'\\0'字符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法在不使用 strupr function 的情况下以大写字母打印所有字符串,因为它不是标准库 function? - Is there a way to print all string in capital letters without using strupr function as its not a standard library function? 在 C 中不使用标准库 function 复制文件 - Copy file without using standard library function in C 字符串串联而未在C中使用库函数? - string concatenation without using the library function in c? 在不使用库 function 的情况下查找字符串长度 - Finding string length without using library function 在不使用任何库 function 的情况下反转字符串 - Reversing a string without using any library function 在不使用库函数的情况下查找字符串中的子字符串 - Finding substring in string without using library function 递归函数比较没有库函数的字符串 - Recursive function to compare strings without library functions 如何在不使用库的情况下查找 c 中字符串 char[] 的长度。 (甚至不是 c 标准) - How to find the length of a string char[] in c without using a library. (Not even c standard) 反转没有库 function 的字符串 - Reversing the string without library function 为什么像 realloc() 这样的标准 C 库中没有 function 没有数据复制? - Why is there no function in standard C library like realloc() without data copying?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM