Thread: Function issue

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    187

    Function issue

    hello I am trying to make a strstr implmention but havent completed it yet coz i have something seriously wrong in my function there is if test inside which test char if there = then it should excute statements but it does excute statements even if there not =
    !!
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    char *strstr2(char *SzName1,char *SzName2) {
        bool isFound=false;
        int record=0;
        char *SzPtr;
        int i;
        int Size;
        int count=0;
        int x=0;
        //run loop to the biggest count of the 2
        Size = strlen(SzName1) > strlen(SzName2) ? strlen(SzName1)-1 : strlen(SzName2)-1 ;
        for(i=0; i < Size ; i++ ) {
            if(SzName2[x]==SzName1[i]) {
                printf("SzName2[x]=%c and SzName[1]=%c\n",SzName2[x],SzName1[x]);
                getchar();
                x++;
                record=i;
                isFound=true;
                count++;
            }
            if(isspace(SzName1[i]) && count!=(strlen(SzName2)-1) ) {
                isFound=false;
                record=0;
                count=0;
            }
        }
        return isFound ? SzPtr : SzPtr='\0';
    }
    int main(void)
    {
        char *SzPtr;//return ptr where it find the name the address
        char SzName1[]="name";
        char SzName2[]="thats a good name bro";
        SzPtr=strstr2(SzName2,SzName1);
        printf("%s",SzPtr ? SzPtr : "didnt find specefied string" );
        return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your if statement says [x] and [i], but you're printing [x] and [x].

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    yes i m testing if chars are = and i made printf as debug to see whats going on inside the if statement but it shouldnt excute if there not = i dunno why it does ...

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by elwad View Post
    hello I am trying to make a strstr implmention but havent completed it yet coz i have something seriously wrong in my function there is if test inside which test char if there = then it should excute statements but it does excute statements even if there not =
    !!
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    char *strstr2(char *SzName1,char *SzName2) {
        bool isFound=false;
        int record=0;
        char *SzPtr;
        int i;
        int Size;
        int count=0;
        int x=0;
        //run loop to the biggest count of the 2
        Size = strlen(SzName1) > strlen(SzName2) ? strlen(SzName1)-1 : strlen(SzName2)-1 ;
        for(i=0; i < Size ; i++ ) {
            if(SzName2[x]==SzName1[i]) {
                printf("SzName2[x]=%c and SzName[1]=%c\n",SzName2[x],SzName1[x]);
                getchar();
                x++;
                record=i;
                isFound=true;
                count++;
            }
            if(isspace(SzName1[i]) && count!=(strlen(SzName2)-1) ) {
                isFound=false;
                record=0;
                count=0;
            }
        }
        return isFound ? SzPtr : SzPtr='\0';
    }
    int main(void)
    {
        char *SzPtr;//return ptr where it find the name the address
        char SzName1[]="name";
        char SzName2[]="thats a good name bro";
        SzPtr=strstr2(SzName2,SzName1);
        printf("%s",SzPtr ? SzPtr : "didnt find specefied string" );
        return 0;
    }
    There are a lot of issues in your code.
    1.
    Code:
    return isFound ? SzPtr : SzPtr='\0';
    This statement is incorrect. The error it gives is "Lvalue required", so it should have been
    Code:
    return isFound ? SzPtr : '\0';
    2. What is Szptr? It's an uninitialized pointer, so the code will produce undefined results.
    3.
    Code:
    if(isspace(SzName1[i]) && count!=(strlen(SzName2)-1) )
    Your count condition is incorrect because of which as soon as the space comes after "that's", it makes this if condition true and thus isFound becomes false. It should be ''==" instead of "!=".
    Correct all these issues and your code will work.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    i m not making an lvu i forgot that record to set the & for record also i m wondering now it works but problem not kinda problem actually but the printf statement shows weird result it should show n with n not t with n
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    char *strstr2(char *SzName1,char *SzName2) {
        bool isFound=false;
        int record=0;
        char *SzPtr;
        int i;
        int Size;
        int count=0;
        int x=0;
        //run loop to the biggest count of the 2
        Size = strlen(SzName1) > strlen(SzName2) ? strlen(SzName1)-1 : strlen(SzName2)-1 ;
        for(i=0; i < Size ; i++ ) {
            if(SzName2[x]==SzName1[i]) {
                printf("SzName2[x]=%c and SzName[1]=%c\n",SzName2[x],SzName1[x]);
                getchar();
                x++;
                record=i;
                isFound=true;
                count++;
            }
            if(isspace(SzName1[i]) && count==(strlen(SzName2)-1) ) {
                isFound=false;
                record=0;
                count=0;
            }
        }
        return isFound ? SzPtr=&SzName1[record+2] : SzPtr='\0';
    }
    int main(void)
    {
        char *SzPtr;//return ptr where it find the name the address
        char SzName1[]="name";
        char SzName2[]="thats a good name bro";
        SzPtr=strstr2(SzName2,SzName1);
        printf("%s",SzPtr ? SzPtr : "didnt find specefied string" );
        return 0;
    }

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    At the end "record" is at the last char of name, so [record-3] will print the correct output. But still I'll say that you should make the code more general i.e it should work for different inputs.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function issue
    By Learner87 in forum C++ Programming
    Replies: 1
    Last Post: 04-09-2008, 01:31 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM