spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Substring in c without using functions


There are a number of ways to recreate strstr. The following is a quick implementation using the inch-worm method, where you simply use pointers to search for the beginning of the substring in string, then if found, compare every character in substring with the corresponding character in string. If all characters match, the substring is found, return a pointer to the beginning of substring in string.

If a character fails the test, look for another character in string that matches the first character in substring, until string is exhausted.

There are probably several more checks that can be inplemented, but this example should get you started:

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

char *strstr2 (char *str, char *sub)
{
    if (!str || !sub) return NULL;              /*
validate both strings    */

    char *p = NULL;                             /*
general pointer          */
    char *sp = NULL;                            /*
substring pointer        */
    char *rp = NULL;                            /*
return pointer           */
    char matched = 0;                           /*
matched flag             */
    size_t szstr = 0;                           /*
string length            */
    size_t szsub = 0;                           /*
substring length         */

    p = sub;
    while (*p++) szsub++;                       /*
strlen of substr         */

    p = str;
    while (*p++) szstr++;                       /*
strlen of str            */

    if (szsub > szstr) return NULL;            
/* szstr < szsub - no match */

    p = str;

    while (p < (p + szstr - szsub + 1))
    {
        while (*p && *p != *sub) p++;     
     /* find start of sub in str */

        if ((str + szstr) == p) return NULL;    /*
if end reached - no sub  */

        rp = p;                                 /*
save return pointer      */
        sp = sub;                               /*
set sp to sub            */
        matched = 1;                            /*
presume will match       */
        while (*sp)                             /*
for each in substring    */
            if (*p++ != *sp++) {                /*
check if match fails     */
                matched = 0;                    /*
if failed, no match      */
                break;                          /*
break & find new start   */
            }
        if (matched)                            /*
if matched, return ptr   */
            return rp;                          /*
to start of sub in str   */
    }

    return NULL;                                /*
no match, return NULL    */
}

int main() {

    char *string = NULL;
    char *substr = NULL;
    char *begin = NULL;

    printf ("
Enter string : ");
    scanf ("%m[^
]%*c", &string);

    printf ("
Enter substr : ");
    scanf ("%m[^
]%*c", &substr);

    if ((begin = strstr2 (string, substr)) !=
NULL)
        printf ("
Substring found beginning at : %s

", begin);
    else
        printf ("
Substring NOT in string.

");

    if (string) free (string);
    if (substr) free (substr);

    return 0;
}

output:

$ ./bin/strstr

Enter string : This is the full string or
"haystack".

Enter substr : g or "

Substring found beginning at : g or "haystack".

$ ./bin/strstr

Enter string : This is the full string or
"haystack".

Enter substr : g or '

Substring NOT in string.

Categories : C

Related to : Substring in c without using functions
Dynamically Replace Substring With Substring Using PHP
Regex and regex only.... Just use the below regex to match the text which you don't want and then replace it with an empty string. (?<=[[)(?:(?!]]||).)*||[[|]] DEMO Code: <?php $str = "loads of text [[gibberish text|Text i Want]] more text [[gibberish text|Text i Want]] more text [[if no separator then just remove tags]]"; echo preg_replace("/(?<=[[)(?:(?!]]||).)*||[[|]]/m", "", $st

Categories : PHP
Should we use macros, inline functions or just normal functions and trust the compiler?
You probably should declare these as static inline functions and define them in a header file. (Alternatively, enable link-time optimization by compiling and linking with gcc -flto -O2, using a recent GCC)

Categories : C
Get the last substring in string?
You can use awk to print the n:th column: awk '{print $14}' file You can filter out all lines that does not have at least 14 columns with the following syntax: awk 'NF==14 {print $14}' file

Categories : Python
How can I query for a substring whose value is >= 60000
I think the problem is the "None Recorded" value. I don't know if it is going to run the first where to exclude the first or not. Try this: SELECT exec_time FROM (SELECT exec_time FROM myTable WHERE exec_time NOT LIKE 'N%') as foo WHERE cast(split_part(foo.exec_time, ' ', 1) as int) >= 60000 ORDER by length(foo.exec_time) desc, foo.exec_time desc limit 10

Categories : SQL
Palindrome substring sorter in C
Your "supposed to be like" list doesn't seem to include ama despite the fact it appears to meet the criteria. You should probably clarify that with the educator. Once clarified, the next step you need to take is to remove the duplicates from your output. Your output actually seems correct but for the fact that it still has duplicated values so I'd start with what you have so far. Given that it's

Categories : C
Recently Add
Rewrite of IDA decompiled function
Computing the average of grades in C
passing structures to functions
Use semaphores for handling sockets in C
How to search for a string pattern inside html, coding in C?
sprintf invalid format '%d'
Why can't this c program run correctly?
Low Pass filter in C
Child process does not print anything
C: datatypes. sqrt function working with int why?
How to implement Serial Port Profile Link Command used in ConnectBlue Bluetooth Module?
Rerunning cancelled pthread
How to print out a string that is pointed to by a char pointer?
Can unverified scanf call cause an undefined behavior?
nested structures in C
Portable Makevars for R package using C, GSL and OpenMP with help of Rcpp
program doesn't run(getting RUN FINISHED Segmentation fault core dumped)
Finding the sign of a Lua number, from C
Width of symbols created by gcc's objectcopy
Strange behavior when using free in c program
Can't open image C
Strange C code - dynamic arrays?
C program printing weird characters
Fault in decimal to binary in c
How to detect broken pipe on input?
How can I ivestigate use of the register keyword in C?
File scaning with fscanf
why gcc generates "leal -8240(%ebp), %eax; cmpl $1, %eax; ja XXX" instead of jmp instruction?
Wierd Problems With My Own Custom Written Word Search Algorithm
Trouble with making a shell in C
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.