Author Topic: Check if a string is inside a string  (Read 2286 times)

0 Members and 3 Guests are viewing this topic.

Offline sh4d0w_w4tch

  • Peasant
  • *
  • Posts: 73
  • Cookies: -1
  • Please do not feed the skids.
    • View Profile
    • 6c.nz
Check if a string is inside a string
« on: July 21, 2015, 08:44:36 am »
Code: (c) [Select]
int isStrInStr(char *substr, char *compstr) {
  int s = 0;
  for (int i = 0; i <= strlen(compstr); i++) {
    if (compstr[i] == substr[s]) {
      s++;
    } else if (substr[s] == 0x00) {
      return 0;
    } else {
      i = i - s;//I should add something like "i = i - s;" here.  Edit: That worked.
      s = 0;
    }
  }
  return 1;
}

I wrote this function to evaluated whether a given string contains a certain string.

Here's how it works:

The function takes two arguments, the string to check for and the string to evaluate.

A for loop runs through each character of the evaluated string and looks for the first character of the string that we are looking for.

If the characters match, then it evaluates the next two characters in both strings.  It continues comparing characters in both strings until they don't match or it gets a null terminator.

If the sub string contains a null terminator then it returns 0 for true.

if the characters do not match then the substring counter is reset to 0.  I need to make it reset the i counter the last position before the character comparison started returning true. Done.
« Last Edit: July 21, 2015, 09:07:29 am by sh4d0w_w4tch »
DeepCopy | Can you name a VPN provider that's like "hey use our services to hack government sites and spam the internet. Please Abuse our services"

+Polyphony | paging master hackers of evilzone: i am here to learn about your black hatted tools to hack different viruses like facebook, sql, php, and other ring zero exploits


Offline BurnTheWicked

  • Serf
  • *
  • Posts: 25
  • Cookies: -30
    • View Profile
Re: Check if a string is inside a string
« Reply #1 on: July 21, 2015, 03:33:52 pm »
Actually that won't work, unless the string you're searching, is the string you're searching for. If you really want to know if a string is within a string... Open first with checking your String is not null, or your SearchString is not null; from there, if you actually want to know the SearchString is inside String, you should check each value of SearchString, to each value of String, before incrementing SearchString value... Otherwise, you're only checking for something that is identical... Also, you don't need so many brackets; it makes it look sloppy...
Code: [Select]
for (int i=0, s=0, e=strlen(Comp); i <= e; i++) {
 if (Str[i] == 0x00 || Comp[i] == 0x00) return 0;
  for (;s <= e;s++) {
   if (Str[s] != Comp[i]) continue;
   else break;
  }
  if (s != e && i != e) continue;
  elseif ((int D = s - i) == e) return 1;
  elseif (s == e || i == e) return 0;
}

Ok, so very quick, and it will not work entirely; it does not verify that each character is found in exact succession... However, it will probably do better then what you provided, and it does show a little bit more of the consolidation power of C... That and I just used spaces to indent... Keep in mind, C/C++ and php have the ability to inline; though it's not a wise choice to over use inlining, but it can make a lot of things easier...

Offline sh4d0w_w4tch

  • Peasant
  • *
  • Posts: 73
  • Cookies: -1
  • Please do not feed the skids.
    • View Profile
    • 6c.nz
Re: Check if a string is inside a string
« Reply #2 on: July 22, 2015, 02:17:51 am »
Interesting.  I'll take a look.  Inlining improved the syntax.

I tested it with this:

Code: [Select]
int strInStr = isStrInStr("come tw", "this come welcome to c");
And it returned false.

This:

Code: [Select]
int strInStr = isStrInStr("come to", "this come welcome to c");
Returned true.
DeepCopy | Can you name a VPN provider that's like "hey use our services to hack government sites and spam the internet. Please Abuse our services"

+Polyphony | paging master hackers of evilzone: i am here to learn about your black hatted tools to hack different viruses like facebook, sql, php, and other ring zero exploits


Offline BurnTheWicked

  • Serf
  • *
  • Posts: 25
  • Cookies: -30
    • View Profile
Re: Check if a string is inside a string
« Reply #3 on: July 22, 2015, 03:14:04 am »
See as I said, it won't test for exact succession lol. But it does test to see that the string is there... But just to make sure I'm not being egotistical; did you try both mine and yours?

Offline sh4d0w_w4tch

  • Peasant
  • *
  • Posts: 73
  • Cookies: -1
  • Please do not feed the skids.
    • View Profile
    • 6c.nz
Re: Check if a string is inside a string
« Reply #4 on: July 22, 2015, 05:12:48 am »
Not yet but I'll believe you.
DeepCopy | Can you name a VPN provider that's like "hey use our services to hack government sites and spam the internet. Please Abuse our services"

+Polyphony | paging master hackers of evilzone: i am here to learn about your black hatted tools to hack different viruses like facebook, sql, php, and other ring zero exploits


Offline ArkPhaze

  • Peasant
  • *
  • Posts: 136
  • Cookies: 20
  • null terminated
    • View Profile
Re: Check if a string is inside a string
« Reply #5 on: July 27, 2015, 07:09:19 am »
Why not just use the strstr() function?

Otherwise, I'd have something like this:
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *xstrstr(char *src, const char *find)
{
  size_t i = 0;
  size_t srclen = strlen(find);
  char *p = NULL;
  for (p = src; *p; ++p)
  {
    for (i = 0; i < srclen; ++i)
    {
      if (!*(p + i)
          || *(p + i) != *(find + i))
      {
        break;
      }
    }
    if (i == srclen)
      return p;
  }
  return NULL;
}

int main()
{
  char src[] = "this is a simple test";
  const char find[] = "simple";

  char *p = xstrstr(src, find);
  if (!p)
  {
    puts("Not found...\n");
    exit(0);
  }

  printf("Found @ src[%d]: %s\n", p - src, p);
  return 0;
}

Obvious optimizations still can be implemented, for example, here:
Code: [Select]
for (p = src; *p; ++p)
« Last Edit: July 28, 2015, 12:34:08 am by ArkPhaze »
sig=: ArkPhaze

[ J/ASM/.NET/C/C++ - Software Engineer ]

Offline fuicious

  • Serf
  • *
  • Posts: 47
  • Cookies: 0
    • View Profile
Re: Check if a string is inside a string
« Reply #6 on: August 15, 2015, 09:21:19 am »
This is how I would do it.
Code: [Select]
int LetsCheckIt(char *TheBigString, char *TheSubString)
{
    int i = 0;
    while(TheBigString != '\0')
    {
        if(TheBigString == TheSubString[0])
        {
            int m = i;
            int n = 0;
            while(TheBigString[m] == TheSubString[n] && TheBigString[m] != '\0' && TheSubString[n] != '\0')
            {
                m++;
                n++;
            }
            if(TheSubString[n] == '\0')
                return 1;
        }
        i++;
    }
    return 0;
}
« Last Edit: August 15, 2015, 09:22:00 am by fuicious »

Offline Mytosus

  • /dev/null
  • *
  • Posts: 6
  • Cookies: 2
    • View Profile
Re: Check if a string is inside a string
« Reply #7 on: October 05, 2015, 07:20:48 pm »
Code: [Select]
int string_in_string(char* string1, char* string2){
  int i, c = 0;
  if(strlen(string2) > strlen(string1)){
    return 0;
  }
  for(i = 0; i < strlen(string1); ++i){
    if(string2[c] == string1[i]){
      printf("in here bro!\n");
      if(c == strlen(string2)-2){
printf("in here!\n");
return 1;
      }
      ++c;
    }
    else{
      c = 0;
    }
  }
  return 0;
}
returns 1 if the string2 is in string1.

Offline xor

  • Peasant
  • *
  • Posts: 59
  • Cookies: 32
    • View Profile
Re: Check if a string is inside a string
« Reply #8 on: October 06, 2015, 03:34:48 am »
But seriously. As ArkPhase said, why not use strstr?
Don't reinvent the wheel if there's no need to.

http://www.tutorialspoint.com/c_standard_library/c_function_strstr.htm

Offline fuicious

  • Serf
  • *
  • Posts: 47
  • Cookies: 0
    • View Profile
Re: Check if a string is inside a string
« Reply #9 on: October 06, 2015, 03:00:05 pm »
But seriously. As ArkPhase said, why not use strstr?
Don't reinvent the wheel if there's no need to.

http://www.tutorialspoint.com/c_standard_library/c_function_strstr.htm
Yeah, but you need to include a whole library to do so. Regardless of that, I don't see anything wrong in trying to understand the functions we usually take for granted.

Offline novaccainne

  • Serf
  • *
  • Posts: 29
  • Cookies: 2
    • View Profile
Re: Check if a string is inside a string
« Reply #10 on: October 06, 2015, 03:55:58 pm »
Yeah, but you need to include a whole library to do so. Regardless of that, I don't see anything wrong in trying to understand the functions we usually take for granted.

But, why don't you look for it on goggle if you would like to implement strstr ?

https://mischasan.wordpress.com/2011/03/19/building-a-better-strstr/
http://articles.leetcode.com/2010/10/implement-strstr-to-find-substring-in.html
https://fossies.org/dox/glibc-2.22/string_2strstr_8c_source.html


Offline BurnTheWicked

  • Serf
  • *
  • Posts: 25
  • Cookies: -30
    • View Profile
Re: Check if a string is inside a string
« Reply #11 on: November 01, 2015, 05:09:26 am »
novaccaine - You would actually listen to someone cheap and stupid enough to use wordpress websites?? Fuicious is correct, it is a proper mind set, to both question, and understand the simple things we take for granted. You may see it as "re-inventing the wheel", but real hackers see it as "Oh, that's how it works", or "Hey, if we try this, we can increase our overall throughput by 20%". How the fuck you think we have mobile devices, and tables? By fuckin re-inventing the fuckin wheel.

Offline ArkPhaze

  • Peasant
  • *
  • Posts: 136
  • Cookies: 20
  • null terminated
    • View Profile
Re: Check if a string is inside a string
« Reply #12 on: December 13, 2015, 05:31:42 am »
You have the misunderstanding of "re-inventing the wheel" vs. "innovation" which is mistake #1. If tablets and such were the results of "re-inventing the wheel" they would not contain the same core code as most of the desktop counterparts. Additionally, most innovative ideas from my experience were the result of "_expanding_ the wheel" not "re-inventing" it unless there is a fundamental issue with the pre-existing "wheel" so to speak.
sig=: ArkPhaze

[ J/ASM/.NET/C/C++ - Software Engineer ]

Offline novaccainne

  • Serf
  • *
  • Posts: 29
  • Cookies: 2
    • View Profile
Re: Check if a string is inside a string
« Reply #13 on: December 16, 2015, 09:51:59 am »
Hi,

First, I would like to thank you for your kind coment :) Your ability to communicate with other people is simple amazing. I think if you want to learn something then you need to buy a few textbook/coursebook and you have to learn how other people did things and how certain techologies work.  I guess the reverse engineering and code analyzing means nothing for you because you can solve everything by youself  :) The fact that we are using mobile phones now is the result of a long process that built on each other. It means that peope were taught each other. Othewise, it wouldn't hurt you to read a publication which is written by a professional engineer since it is easy to learn stupid things (and use them later) by yoursef.
You might learn reading on your own way... but I think someone has taught you to read and you didn't discover the letters by yourself ... I think I can not say more for this abusive and unimportand and wiseacre post... "re-inveting the wheel" means something completely different thing...

novaccaine - You would actually listen to someone cheap and stupid enough to use wordpress websites?? Fuicious is correct, it is a proper mind set, to both question, and understand the simple things we take for granted. You may see it as "re-inventing the wheel", but real hackers see it as "Oh, that's how it works", or "Hey, if we try this, we can increase our overall throughput by 20%". How the fuck you think we have mobile devices, and tables? By fuckin re-inventing the fuckin wheel.

Offline Crostal

  • NULL
  • Posts: 1
  • Cookies: 0
    • View Profile
Re: Check if a string is inside a string
« Reply #14 on: January 03, 2016, 01:13:33 am »
I read so many comments about reinventing the wheel. In learning especially with the basics there is nothing as reinventing the wheel.
I would say too that for a more efficient way to solve this problem (In your case your algorithm is a O(n2) but KMP is only O(k+n) where k is the length of the main string and n is the length of the desired word you are searching for.
-for more on complexity http://www.dreamincode.net/forums/topic/125427-determining-big-o-notation/.
-KMP https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm.
I know the wiki of KMP is a little hard to read and understand, it's a bit complixe approach to solve this problem, but it's very time efficient.