重新创建 strstr() 函数 [英] Recreate the strstr() function

查看:60
本文介绍了重新创建 strstr() 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试创建自己的 strstr() 函数,但我无法弄清楚它为什么返回分段错误.我正在尝试在另一个字符串中搜索一个字符串,然后返回指向第一个相同"字母的指针.任何帮助,将不胜感激.这是我的代码:

Hello i am trying to make my own strstr() function and i can't figure out why it is returning a segmentation fault.I am trying to search a string within another string and then return a pointer to the first 'same' letter. Any help would be appreciated. This is my code:

char* ms_search(char *Str1,char* Str2){
    char* p = NULL;
    int i,k=0,j = 0;
    for(i = 0;i < ms_length(Str1); i++){
        if(Str1[i] == Str2[k]){
            if(k == 0){
                p = &Str1[i];
                j= i;
            }
            if(k == ms_length(Str2)){
                break;
            }
            k++;
        }
        else{
            if(Str1[i] == Str2[0]){
                p = &Str1[i];
                k=1;
                j= i;
            }   
            else{
                j=0;
                k = 0;
                p = NULL;
            }
        }
    }
    if(p != NULL){
        Str1[ms_length(Str2)+1] = '\0';
    }
    return &Str1[j];
} 
int main(){
    int i;
    char* p2;
    char* p="lolaaa";
    char* p1= "aaa";
    //char ar2[] = "aaa4";
    //ms_copy(p,p1);
    //printf("%s",p);
    //ms_nconcat(p,p1,3);
    //if(ms_ncompare(p,p1,3) == 1) printf("einai idia");
    p2 = ms_search(p,p1);
    printf("%s",p2);
    return 0;
}

推荐答案

你好,我正在尝试制作自己的 strstr()

Hello i am trying to make my own strstr()

首先你要遵循C标准.

C89/C99 原型是:

The C89/C99 prototype is:

char *strstr(const char *s1, const char *s2);

标准 strstr() 函数不会改变传递的缓冲区.

Standard strstr() function will NOT change the passed buffers.

功能描述为:

strstr() 函数在 s1 所指向的字符串中定位第一个出现在 s1 指向的字符串中的字符序列(不包括终止空字符)s2.

strstr() function locates the first occurrence in the string pointed to by s1 of the sequence of characters (excluding the terminating null character) in the string pointed to by s2.

strstr 函数返回指向所定位字符串的指针,如果未找到该字符串,则返回空指针.如果 s2 指向长度为零的字符串,则函数返回 s1.

The strstr function returns a pointer to the located string, or a null pointer if the string is not found. If s2 points to a string with zero length, the function returns s1.

在标准 C 中,这可以实现为:

In standard C, this can be implemented as:

#include <string.h> /* size_t memcmp() strlen() */
char *strstr(const char *s1, const char *s2)
{
    size_t n = strlen(s2);
    while(*s1)
        if(!memcmp(s1++,s2,n))
            return (char *) (s1-1);
    return 0;
}

独立实现如下:

#include <stdio.h>

char *strstr1(const char *str, const char *substring)
{
    const char *a;
    const char *b;

    b = substring;

    if (*b == 0) {
        return (char *) str;
    }

    for ( ; *str != 0; str += 1) {
        if (*str != *b) {
            continue;
        }

        a = str;
        while (1) {
            if (*b == 0) {
                return (char *) str;
            }
            if (*a++ != *b++) {
                break;
            }
        }
        b = substring;
    }

    return NULL;
}

int main (void)
{
  char string[64] ="This is a test string for testing strstr";
  char *p;

  p = strstr1 (string,"test");

  if(p)
  {
    printf("String found:\n" );

    printf ("First occurrence of string \"test\" in \"%s\" is:\n%s", string, p);
  }
  else
  {
    printf("String not found!\n" );
  }

  return 0; 
}

输出:

String found:
First occurrence of string "test" in "This is a test string for testing strstr" is:
test string for testing strstr

这篇关于重新创建 strstr() 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆