繁体   English   中英

C中两个字符串的字母比较

[英]Alphabetical comparison of two strings in C

我的编程经验非常分散,我似乎无法找到我希望是一个简单问题的答案。 上周我有一个任务,我没有正确完成任务,这使我烦死了。

我应该在不使用strcmp的情况下按字母顺序比较两个字符串,然后通过使用指针的函数找出哪个字符串首先按字母顺序排序。

int strcmp373(char *str1, char *str2) {
  while(*str1 != '\0')
  {
    str1++;
  }
  while(*str2 != '\0')
  {
    str2++;
  }
if(*str1 == *str2)

}

这是我的可怕尝试,我的思维过程是使用空终止值。 我希望我能获得一些见识并解释其工作原理?

这是作业规范的副本,以供参考。

编写一个名为strcmp373的函数,该函数以与strcmp在C库中完全相同的方式比较两个字符串。 这次,请在编写此函数时使用“指针语法”。 也就是说,当引用string1和string2中的特定字符时,根本不应该使用[]运算符; 而是应将所有参数和局部变量声明为指针(使用*符号)。 请确保您模拟了strcmp C函数。 请注意,即使两个字符串相等,strcmp也会返回0,即使0在C语言中通常意味着false。其他返回值的符号也很重要,用于指示字符串的不同方式,但是精确的返回值并不重要。 您可能无法使用任何内置的C字符串库函数来完成此代码。

这是此函数的原型:

int strcmp373(char *, char *);

这是您可以用来测试strcmp373的主要功能。

#include <stdio.h>
#include "hw3.h" // to be discussed

int main() {
char str1[81], str2[81];
char again = 'y', newline;
while (again == 'y') {
    printf("Enter a string\n");
    scanf("%s", str1);
    printf("Enter another string\n");
    scanf("%s", str2);
    int comp = strcmp373(str1, str2);
    if (comp < 0)
        printf("%s is alphabetically before %s\n", str1, str2);
    else if (comp > 0)
        printf("%s is alphabetically after %s\n", str1, str2);
    else printf("%s and %s are the same\n", str1, str2);
    printf("Again? (y/n)\n");
    scanf("%c%c", &newline, &again);
}
}

假设str1指向持有“ abcd”的东西,而str2指向持有“ abcd”的东西。

str1
|
v
+---+---+---+---+----+
| a | b | c | d | \0 |
+---+---+---+---+----+

str2
|
v
+---+---+---+----+
| a | b | c | \0 |
+---+---+---+----+

执行时

while(*str1 != '\0')
{
  str1++;
}

您移动str1直到它指向空字符。

str1
|
v
+---+---+---+---+----+
| a | b | c | d | \0 |
+---+---+---+---+----+

    str1
    |
    v
+---+---+---+---+----+
| a | b | c | d | \0 |
+---+---+---+---+----+

        str1
        |
        v
+---+---+---+---+----+
| a | b | c | d | \0 |
+---+---+---+---+----+

            str1
            |
            v
+---+---+---+---+----+
| a | b | c | d | \0 |
+---+---+---+---+----+

                str1
                |
                v
+---+---+---+---+----+
| a | b | c | d | \0 |
+---+---+---+---+----+

同样,当您执行

while(*str2 != '\0')
{
  str2++;
}

您移动str2直到它指向空字符。

str2
|
v
+---+---+---+----+
| a | b | c | \0 |
+---+---+---+----+

    str2
    |
    v
+---+---+---+----+
| a | b | c | \0 |
+---+---+---+----+

        str2
        |
        v
+---+---+---+----+
| a | b | c | \0 |
+---+---+---+----+

            str2
            |
            v
+---+---+---+----+
| a | b | c | \0 |
+---+---+---+----+

while循环完成时, str1str2指向一个空字符。 因此, *str1 == *str2始终计算为true

您需要比较*str1*str2 ,如果它们相等,则将它们加在一起,直到它们不相等或到达字符串的末尾。

str1
|
v
+---+---+---+---+----+
| a | b | c | d | \0 |
+---+---+---+---+----+

str2
|
v
+---+---+---+----+
| a | b | c | \0 |
+---+---+---+----+

    str1
    |
    v
+---+---+---+---+----+
| a | b | c | d | \0 |
+---+---+---+---+----+

    str2
    |
    v
+---+---+---+----+
| a | b | c | \0 |
+---+---+---+----+

        str1
        |
        v
+---+---+---+---+----+
| a | b | c | d | \0 |
+---+---+---+---+----+

        str2
        |
        v
+---+---+---+----+
| a | b | c | \0 |
+---+---+---+----+

            str1
            |
            v
+---+---+---+---+----+
| a | b | c | d | \0 |
+---+---+---+---+----+

            str2
            |
            v
+---+---+---+----+
| a | b | c | \0 |
+---+---+---+----+

现在您知道它们不相等,并且'd'大于'\\0'返回一个正值,表示LHS按字母顺序大于RHS。

该逻辑可以使用以下方法实现:

while ( *str1 != '\0' && *str1 == *str2 )
{
   ++str1;
   ++str2;
}

return (*str1 - *str2);
if(*str1 == *str2)   

您的代码摘录中有一个if语句,其中没有任何正文。

除此之外,您的程序还要计算传递给该函数的字符数组中的字符数。 这不会真正实现您的目标。

让我们将每个char数组称为一个词。 您要做的是找到字母表中第一个“单词”。 我建议看一个ASCII表。

每个单词中的每个字母都有一个对应的ASCII数字值。 如果可以弄清楚如何访问这些字母中的每个字母的数字值,则可以比较字符串,字母中的较低值将较高。

除非它们大写。 这将需要更多的工作。 这是一个ASCII表,您可以在其中看到区别。 在此处输入图片说明

这将使您开始弄清楚如何获取这些字母的ASCII值

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM