简体   繁体   English

C-子字符串在字符串中的位置递归

[英]C - position of substring in string recursively

Task asks to determine first if a string str1 is a substring of str2. 任务要求首先确定字符串str1是否为str2的子字符串。 If so, the starting position of str1 in str2 is to be returned. 如果是这样,将返回str2中str1的起始位置。

int instring (char *str1, char *str2);
void main () {
     char *str1 = (char*) malloc (strlen(str1)*sizeof(char));
     char *str2 = (char*) malloc (strlen(str2)*sizeof(char));
     char ch;
     int i = 0;
     while (ch != '\n'){
         ch = getchar();
         str1[i] = ch;
         i++;
     }
     str1[i] = '\0';
     i = 0;
     char ch1;
     while (ch1 != '\n'){
     ch1 = getchar();
     str2[i] = ch1;
     i++;
     }
     str2[i] = '\0';
     printf("%d", instring(str1, str2));
     return 0;
 }
 int instring(char *str1, char *str2){
     int r;
     if(*str1==0) return(0);
     if(*str2==0) return -1;
     if(*str1==*str2 && instring(str1+1,str2+1)==0) return(0);
     r=instring(str1,str2+1);
     if(r!=-1) return(r+1);
     }

Case 1: The implementation works for: str1 = "Mediolan" str2 = "MMediolan" 情况1:该实现适用于:str1 =“ Mediolan” str2 =“ MMediolan”

It returns 1, which is correct. 它返回1,这是正确的。

Case 2: However it does not for: str1 = "Mediolan" str2 = "Mediolana" 情况2:但是不适用于:str1 =“ Mediolan” str2 =“ Mediolana”

It returns -1. 返回-1。

I got stuck here and do not know how to rewrite the code, so the Case 2 will be taken care of correctly. 我被困在这里,不知道如何重写代码,因此案例2将得到正确处理。

For the start this is wrong 首先,这是错误的

 char *str1 = (char*) malloc (strlen(str1)*sizeof(char));
 char *str2 = (char*) malloc (strlen(str2)*sizeof(char));

whish str1 do you refer to in strlen ? 您在strlen中指的是str1吗?

Also, you never initialized ch1 below 另外,您从未在下面初始化ch1

char ch1;
while (ch1 != '\n'){

You should not read value of variable you didn't initialize. 您不应该读取未初始化的变量的值。 Same for ch . ch相同。


Maybe you want to try this version (seems to work at my side): 也许您想尝试这个版本(似乎可以在我这边工作):

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

#define MAXLEN 200

int instring (char *str1, char *str2);

int main (void) 
{

    char *str1, *str2;
    int ch = 0;
    int i = 0;

    str1 =  malloc (MAXLEN);
    if(str1 == NULL) return 1;
    str2 =  malloc (MAXLEN);
    if(str2 == NULL) {free(str1); return 1;}

    while((ch = getchar()) != EOF && ch != '\n')
    {
        str1[i] = (char) ch;
        i++;
    }
    str1[i] = '\0';

    i = 0;
    while((ch = getchar()) != EOF && ch != '\n')
    {
        str2[i] = (char) ch;
        i++;
    }
    str2[i] = '\0';

    printf("%d", instring(str1, str2));
    free(str1); free(str2);
    return 0;
}

int instring(char *str1, char *str2)
{
    int r = 0;
    if(*str1 == 0) return 0; 
    if(*str2 == 0) return -1;
    if(*str1 == *str2 && instring(str1 + 1, str2 + 1) == 0) return 0;
    r = instring(str1, str2 + 1);
    if(r != -1) return (r + 1);
}

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

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