need some help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe Wright

    #16
    Re: need some help

    shailesh.budhan er@gmail.com wrote:[color=blue]
    > Hi
    > u can use strstr function which returns int telling position of first
    > occurance of one string in other string
    > by using that position(index) u can return poiter.
    > ash wrote:
    >[/color]
    On my system, string.h has an entry..

    char * strstr(const char *_s1, const char *_s2);

    ...suggesting strstr does not return int.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---

    Comment

    • av

      #17
      Re: need some help

      On Sat, 10 Jun 2006, "Malcolm" <regniztar@btin ternet.com> wrote:[color=blue]
      >"ash" <ashishmourya21 @rediffmail.com > wrote[color=green]
      >> one friend advised me to use "strstr" function, this is a easy way to
      >> solve that question by use built in function but actually i was trying
      >> to make this function and i want help in writing that function.
      >>[/color]
      >First write the skeleton
      >
      >/*
      > mystrstr - find the first occurrence of substring in string
      > Params: substring - string to search for
      > string - string to search in.
      > Returns: pointer to occurence of substring in string, NULL if none found.
      >*/
      >char *mystrstr(char *substring, char *string)
      >{
      >}
      >
      >Now for the algorithm.[/color]

      easy if i have the right book

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

      #define F for
      #define R return
      #define W while
      #define P printf
      #define G goto
      #define uns unsigned

      // cerca in una stringa "a" la sottostringa "p"
      // la stringa da cercare "p" deve essere di lunghezza
      // minore di 1023 chars
      // se errore: qualche stringa è il vettore nullo, oppure
      // se errore di lunghezza massima di "p" (>=1023chars) ritorna 0
      //
      // altrimenti ritorna un puntatore alla stringa "a"
      // ove ha trovato la sottostringa "p" o eventualmente
      // (se non l'ha trovata) punta alla fine di "a" in &a[len]
      char* kmns(char* p, char* a)
      {int i,j, k;
      int next[1024];
      if(p==0||a==0) R 0;
      F(i=0, j=-1, next[0]=-1; p[i]&&i<1023; )
      {W(j>=0 && p[i]!=p[j] ) j=next[j];
      ++i; ++j;
      next[i]=((j>=0&&p[i]==p[j])?next[j]:j);
      }
      if(i>=1023) R 0;
      F(k=0, j=0; j<i&&a[k]; ++j, ++k)
      W(j>=0 && a[k]!=p[j]) j=next[j];
      R (j==i? a+(k-i): a+k);
      }


      // cerca in un FILE* "a" la stringa "p"
      // la stringa da cercare "p" deve essere si lunghezza
      // minore di 1022 chars
      // se stringa non trovata ritorna 0
      // se stringa trivata ritorna 1
      // se errore in qualsiasi fase ritorna -1
      int kmnsf(char* p, FILE* a)
      {int i,j, k;
      int next[1024], c;
      if(p==0||a==0) R -1;
      F(i=0, j=-1, next[0]=-1; p[i]&&i<1023; )
      {W(j>=0 && p[i]!=p[j] ) j=next[j];
      ++i; ++j;
      next[i]=((j>=0&&p[i]==p[j])?next[j]:j);
      }
      if(i>=1023) R -1;
      F(j=0; j<i&&(c=getc(a) )!=EOF; ++j)
      W(j>=0 && c!=p[j]) j=next[j];
      R (j==i? 1: 0);
      }


      /* Pacific Standard Time & Daylight Savings */
      // char *tzstr = "TZ=PST8PDT ";

      int main(void)
      {char *sub;

      sub=kmns("Fred" , "My name is Fred and i am dead");
      // should print "Fred and i am dead"
      P("1 sub=[%s]\n", (sub==0? "NULL":sub) );
      sub=kmns("Frede rick", "My name is Fred and i am dead");
      P("2 sub=[%s]\n", (sub==0? "NULL":sub) );
      // should print out that sub is NULL
      sub = kmns("Frederick ", "Fred");
      P("3 sub=[%s]\n", (sub==0? "NULL":sub) );
      // Null here too
      sub=kmns("Fred" , "Fred is Fred dead?");
      // here "Fred is Fred dead"
      P("4 sub=[%s]\n", (sub==0? "NULL":sub) );
      // here "Fred is Fred dead"
      sub=kmns("Fred" , sub+1);
      P("5 sub=[%s]\n", (sub==0? "NULL":sub) );
      // here "Fred dead"

      exit(0);
      }

      C:>string
      1 sub=[Fred and i am dead]
      2 sub=[]
      3 sub=[]
      4 sub=[Fred is Fred dead?]
      5 sub=[Fred dead?]

      Comment

      • CBFalconer

        #18
        Re: need some help

        av wrote:[color=blue]
        >[/color]
        .... snip ...[color=blue]
        >
        > easy if i have the right book
        >
        > #include <stdio.h>
        > #include <stdlib.h>
        >
        > #define F for
        > #define R return
        > #define W while
        > #define P printf
        > #define G goto
        > #define uns unsigned[/color]

        RosIwhatzis has reappeared under another name. PLONK

        --
        Some informative links:
        news:news.annou nce.newusers
        Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!





        Comment

        • Default User

          #19
          Re: need some help

          av wrote:


          [color=blue]
          > #define F for
          > #define R return
          > #define W while
          > #define P printf
          > #define G goto
          > #define uns unsigned[/color]

          Skunk cabbage by any other name . . .

          *plonk*



          Brian

          Comment

          • av

            #20
            Re: need some help

            On Sun, 11 Jun 2006 11:08:02 +0200, av <av@ala.a> wrote:
            [color=blue]
            >#include <stdio.h>
            >#include <stdlib.h>
            >
            >#define F for
            >#define R return
            >#define W while
            >#define P printf
            >#define G goto
            >#define uns unsigned
            >
            >// cerca in una stringa "a" la sottostringa "p"
            >// la stringa da cercare "p" deve essere di lunghezza
            >// minore di 1023 chars
            >// se errore: qualche stringa è il vettore nullo, oppure
            >// se errore di lunghezza massima di "p" (>=1023chars) ritorna 0
            >//
            >// altrimenti ritorna un puntatore alla stringa "a"
            >// ove ha trovato la sottostringa "p" o eventualmente
            >// (se non l'ha trovata) punta alla fine di "a" in &a[len]
            >char* kmns(char* p, char* a)
            >{int i,j, k;
            > int next[1024];
            > if(p==0||a==0) R 0;
            > F(i=0, j=-1, next[0]=-1; p[i]&&i<1023; )
            > {W(j>=0 && p[i]!=p[j] ) j=next[j];
            > ++i; ++j;
            > next[i]=((j>=0&&p[i]==p[j])?next[j]:j);[/color]

            is it better here next[i]=(p[i]==p[j]?next[j]:j); ?
            i say yes. j should be >0 here or not?

            [color=blue]
            >int kmnsf(char* p, FILE* a)
            >{int i,j, k;
            > int next[1024], c;
            > if(p==0||a==0) R -1;
            > F(i=0, j=-1, next[0]=-1; p[i]&&i<1023; )
            > {W(j>=0 && p[i]!=p[j] ) j=next[j];
            > ++i; ++j;
            > next[i]=((j>=0&&p[i]==p[j])?next[j]:j);[/color]

            the same here
            [color=blue]
            > }
            > if(i>=1023) R -1;
            > F(j=0; j<i&&(c=getc(a) )!=EOF; ++j)[/color]

            Comment

            Working...