【问题标题】:C creating function that calculates difference in value between first two non matching charactersC创建函数,计算前两个不匹配字符之间的值差
【发布时间】:2017-02-25 08:14:51
【问题描述】:

这是一个比较两个字符串以检查它们是否相等的函数,一切正常,直到它们不相等。我想计算前两个不匹配字符之间的值差异。 函数返回一个奇怪的数字。这个想法是创建一个指针来保存不匹配字符的第一个外观和另一个在下一次迭代中执行相同操作的指针,然后返回它们的减法。根据我在这个网站上的发现和谷歌研究,到目前为止我能够构建这个。

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


int str_eq(const char *s1,const char *s2)
{
int i = 0;
int d = 0;
char *c ,*cp;
while(*s1 != 0 && *s1 != 0)
{
    if(*s1 != *s2)
    {
        *c = s1[i];
        *cp = s2[i + d];
        d++;
        return ((int)(*cp) - (*c));
    }
    s1++;
    s2++;
    i++;
}
return (0);

}


main()
{
const char *s1 = "awerrf";
const char *s2 = "awedre";

if(!(str_eq(s1 , s2)))
{
    printf("strings are equal");

}
else
{
    printf("strings aren't equal %d" ,str_eq(s1 , s2));

}


}

@edit 我对函数代码进行了小幅更新,因此 c 指向发现第一个不匹配字符的数组索引,但现在我如何创建另一个与下一个不匹配字符相同的索引字符。

int str_eq(const char *s1,const char *s2)
{
int i = 0;

char *c ,*cp;
while(*s1 != 0 && *s2 != 0)
{
    if(*s1 != *s2)
    {
        c = &s1[i];
        return (*c);

    }

    s1++;
    s2++;
    i++;
}
return (0);

}

预期结果(测试用例)

    const char *s1 = "dogydf";
    const char *s2 = "dosydg";


    s1[0] = s2[0]
    s1[1] = s2[1]
    s1[2] != s2[2] pointer c = &s1[2];
    s1[3] = s2[3]
    s1[4] = s2[4]
    s1[5] != s2[5] pointer cp = %s1[5] , break loop;

    return (*cp) - (*c) //(5 - 2)

@edit_2 解决方案 (c) RoadRunner。

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

#define SIZE 2

int str_eq(char *s1, char *s2) {
    int indexes[SIZE];
    int count = 0, i = 0, j = 0;

    while (s1[i] != '\0' && s2[j] != '\0' && count < 2) {
        if (s1[i] != s2[j]) {
            indexes[count++] = i;
        }
        i++;
        j++;
    }

    return indexes[1] - indexes[0];
} 

int main(void) {
    char *s1 = "doedz";
    char *s2 = "dogdyy";

    printf("Differences = %d\n", str_eq(s1, s2));

    return 0;
}

【问题讨论】:

  • SO 不是调试服务。使用符号编译,在调试器中运行代码以逐行跟踪程序,检查相关变量的值以了解真正发生了什么。如果出现具体问题,请随时返回此处。
  • 当你做*c = s1[i];时,c指向哪里?由于它是一个指针,它实际上需要指向某个地方才能有效地取消引用它。也许您应该使用ccp 的指针?
  • 为什么不只是for (; *s1 &amp;&amp; *s2 &amp;&amp; *s1 == *s2; s1++, s2++) {} return *s1 - *s2;? (当然,在初步检查s1s2 都是有效指针之后)
  • 请打开编译器警告并修复它们!使用未初始化变量的代码寻求帮助是没有意义的,编译器已经指出了这一点!
  • ...或者您的问题应该是“编译器给我这个警告,我不明白,为什么我会得到它以及如何修复它?”...

标签: c string difference


【解决方案1】:
int str_eq(const char *s1,const char *s2){
    do{
        if(*s1 != *s2){
            return ((int)(*s1) - (*s2));
        }
    }while(*s1++ != 0 && *s2++ != 0);
    return (0);
}

【讨论】:

  • 如果字符串是“a”和“aa”,这会返回什么?它应该返回什么?
  • @hyde 添加了你的答案
  • 我猜这个问题更多是针对 OP...实际上您的编辑现在使代码的工作方式与 OPs 代码不同...现在考虑它为“a”和“bb”返回的内容。
【解决方案2】:

试试这个:

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

int str_eq(const char *s1, const char *s2, int *diff) {
    if (strlen(s1) != strlen(s2)) {
        return 0;
    }

    if (*s1 == '\0' || *s2 == '\0') {
        return 0;
    }

    while (*s1 != '\0' && *s2 != '\0') {
        if (*s1 != *s2) {
            *diff = *s1 - *s2;
            return 0;
        }
        s1++;
        s2++;
    }

    return 1;
}

int main(void) {
    const char *s1 = "aaa";
    const char *s2 = "aab";
    int diff = 0, result;

    result = str_eq(s1, s2, &diff);

    if (result) {
        printf("Strings are equal\n");
    } else if (!result && diff) {
        printf("strings aren't equal %d\n" , diff);
    } else {
        printf("Invalid string inputs\n");
    }

    return 0;
}

更新:

索引之间的差异:

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

#define SIZE 2

int str_eq(char *s1, char *s2) {
    int indexes[SIZE];
    int count = 0, i = 0, j = 0;

    while (s1[i] != '\0' && s2[j] != '\0' && count < 2) {
        if (s1[i] != s2[j]) {
            indexes[count++] = i;
        }
        i++;
        j++;
    }

    return indexes[1] - indexes[0];
} 

int main(void) {
    char *s1 = "doedz";
    char *s2 = "dogdyy";

    printf("Difference = %d\n", str_eq(s1, s2));

    return 0;
}

【讨论】:

    猜你喜欢
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    • 1970-01-01
    • 2018-11-07
    相关资源
    最近更新 更多