【问题标题】:Finding first string in second string without pointers in C在C中没有指针的情况下在第二个字符串中查找第一个字符串
【发布时间】:2019-03-15 12:26:56
【问题描述】:

我是一名 C 语言新手。 我的老师说我们必须写一个项目来: 在没有任何指针(*)的第一个字符串中查找第二个字符串。到目前为止,我已经学习了循环、条件、函数和数组,它们是我唯一的选择。 该项目必须从两个级别的用户那里获取字符串。检查它们并打印结果。

现在我写了一些废话:

int main()
{

        char source[MAX_STR_LEN];
        char target[MAX_STR_LEN];
        int len = 50;
        int a;
        scanf("%s", &source);
        scanf("%s", &target);

        for (int i = 0; i <= len; i++)
        {

            if (strncasecmp(source[i], target[i], strlen(target)) == 0)
            {
                int a = 1;
                if (a == 1)
                {
                    printf("%s is inner of %s", target, source);
                }
                else
                {
                    printf("%s is NOT inner of %s", target, source);
                }
            }
        }


      return 0;
}

但是当我输入两个字符串时,我的项目什么也不打印并自动关闭。我确定我的代码不正确有什么简单的方法吗? 谢谢

【问题讨论】:

  • 一个主要问题是source[i](以及target[i])是一个单个字符,而strncasecmp函数比较字符串.
  • 即使这个scanf("%s", &amp;source); 也是指针。
  • 首先,scanf("%s", &amp;source); -> scanf("%s", source);

标签: c string pointers


【解决方案1】:

首先你必须改进如何在原始字符串中搜索子字符串的逻辑,或者如果你的老师允许你可以离开C语言来搜索它。

strstr 做这项工作。

下面是我的代码,我在你的代码中添加了注释

#include <stdio.h>
#include <strings.h>

#define MAX_STR_LEN 50

int main(void)
{

     char source[MAX_STR_LEN];
     char target[MAX_STR_LEN];
    //int len = 50;
    //int a;
    scanf(" %s", source);   //char array name is used like pointer to the first element of array
    scanf(" %s", target);

    char* ret = NULL;

    ret = strstr(source, target);

    if(ret == NULL)
        printf("%s is NOT inner of %s", target, source);
    else
        printf("%s is inner of %s", target, source);

  return 0;
}

【讨论】:

  • OP 要求提供不使用(使用)指针的解决方案。由于strstr 返回一个指针,我不确定这是否能回答问题。
  • 对!非常感谢你能把它写成一个函数吗?
  • 操作!这个答案使用 * 我不能使用它。
【解决方案2】:

一个非常简单的方法是简单地逐个字符地检查源字符串以查看是否找到目标字符串。换句话说:

  • 检查目标字符串是否从源[0]开始。
  • 如果不是:检查目标字符串是否从源 [1] 开始。
  • 如果不是:检查目标字符串是否从源 [2] 开始。
  • 以此类推,直到到达源字符串的末尾。

这可以使用两个 for 循环来完成,其中外循环迭代源字符串中的所有字符,内循环迭代目标字符串,同时比较两个字符串中的字符。

你可以像这样可视化它:

source: Hello World
target: lo

Hello World
lo           // No match: He != lo

Hello World
 lo          // No match: el != lo

Hello World
  lo         // No match: ll != lo

Hello World
   lo        // Match: lo != lo

一个简单的实现可能如下所示:

int main()
{

    char source[MAX_STR_LEN] = "Hello World";
    char target[MAX_STR_LEN] = "lo";

    int source_index = 0;
    int match = 0;
    while (source[source_index] != '\0')
    {
        int target_index = 0;
        while (target[target_index] != '\0' &&
               source[source_index + target_index] != '\0' && 
               source[source_index + target_index] == target[target_index])
        {
            ++target_index;
            if (target[target_index] == '\0')
            {
                match = 1;
                break;
            }
        }
        if (match) break;
        ++ source_index;
    }
    if (match)
    {
        printf("found\n");
    }
    else
    {
        printf("not found\n");
    }

  return 0;
}

【讨论】:

  • 非常感谢
  • 你能帮我写一个 CheckString 函数吗?
  • @MeT 好吧,如果代码更改为使用接收字符串并进行搜索的函数,那么将涉及指针,因为字符串作为 char 指针传递......
【解决方案3】:

兄弟,您在检查if(a == 1)之后立即分配了int a = 1;的值,这没有任何意义,因为else{printf("%s is NOT inner of %s", target, source);}以上部分代码将永远不会在这种情况下使用这是解决方案https://www.geeksforgeeks.org/given-two-strings-find-first-string-subsequence-second/be小心:)

【讨论】:

  • 找不到页面?
  • 嗨,欢迎......关于 SO 的答案需要结构化并包含足够的信息,不仅适用于 OP,也适用于未来的访问者。把它放在一边......您提供的链接已损坏。
  • geeksforgeeks.org/… 抱歉链接断开了,您可以用谷歌搜索给定两个字符串,查找第一个字符串是否是第二个字符串的子序列。如果没有指针,这不是你做的最大情况,如果它是一个简单的硬件,你可能不会认为它是数据结构和算法中的大 BC 指针。
猜你喜欢
  • 2020-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-04
  • 2012-12-07
  • 2019-09-22
  • 2018-01-19
  • 2017-09-22
相关资源
最近更新 更多