Thread: Need some help pls guy..

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    10

    Need some help pls guy..

    This program should tell me are two string equal or less.. But ain't working..

    Code:
    cmp(char *p1, char *p2){
             int i = 0,
                  cont = 1;
       while( *p1 != '\0' && *p2 != '\0' && cont != 0  ){
                    
           if ( *( p1 + i) == *( p2 + i ) )
               i++;   
                          
           else
               cont = 0;           
                    
         }
            if ( *p1 == '\0' || *p2 == '\0' )
                 cont = 0;
             printf("%d",cont);
             return cont;
             }
                    
                                 
    #include <stdio.h>  int main()     {
     char s1[20],
          s2[20],
          *p1,
          *p2;
     
     printf("Input String ");
     gets(s1);
     p1 = &s1;
     
     printf("Input second String ");
     gets(s2);
     p2 = &s2;
    return cmp(p1,p2);
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    int compare(char *s1, char *s2)
      { int z;
         while(*s1 != 0)
           { z = *(s1++) - *(s2++);
              if (z != 0)
                return z; }
          return 0; }

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What does the following mean?
    But ain't working..
    What "ain't" working? Be specific. Does the program compile without errors? If not post the complete error message exactly as it appears in your development environment. If it does compile what does your program output? What do you want it to output? What did you input into your program.

    Jim

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    no errors.. just gives me 0 every time...

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by cacca View Post
    no errors.. just gives me 0 every time...
    First fix line 20 from your original posting... move the #include to the TOP of the page.

    Lose the intermediate variables... call your function as...
    Code:
    return cmp(s1,s2);
    Also putting it in a return from main you'll never see the results... except in the value returned to the OS.
    You probably want a printf() in there to say what came back so you can see it.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also stop using gets(), this is a very dangerous function and is considered deprecated and is scheduled to be removed in future versions of the standard.

    Jim

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    guys i started with C about 2 weeks i'm not wery expert.... I didnt know this thing about gets()...

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    gets() will allow you to overrun a buffer...
    Code:
    char input[10];
    
    gets(input);
    Go ahead type in 100 charcters and watch what happens...

    If you use fgets() instead... fgets(input,10,stdin) ... you have a limit on the input length and you don't overflow the buffer.

    Hence... gets() is depricated (disliked) because it is inherrently dangerous.

Popular pages Recent additions subscribe to a feed