如何在不使用 strcmp 和方括号的情况下比较字符串

How to compare string without using strcmp and square brackets?

本文关键字:方括号 情况下 比较 字符串 strcmp      更新时间:2023-10-16

例如,我有一个结构数组,里面有很多名字,我有一个指向这个结构的指针。我有一个输入,让用户输入名称以匹配结构中的名称,我应该在不使用字符串的情况下做什么,只使用指针?

struct people{
   char name[20];
}
people list[10];
people *ptr;
ptr = list;
char lists[20];
char *inpu;
inpu = lists;
cout << "Input name";
cin >> inpu;

我尝试使用它,但它效果不佳。

If ( inpu == (*ptr).name){
       cout << "1";
}
else cout << "2";

如果你不想使用 strcmp,请从这里使用 totallynotstrcmp:

int totallynotstrcmp(const char* s1, const char* s2)
{
    while(*s1 && (*s1==*s2))
        s1++,s2++;
    return *(const unsigned char*)s1-*(const unsigned char*)s2;
}