despertly need your help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madeofdata
    New Member
    • Dec 2008
    • 13

    despertly need your help

    I need the code to implement those two functions;
    myStrCmp
    Compares two strings, returns true if equal and false otherwise
    int myStrCmp(char* str1, char* str2)
    myStrNCpy
    Copies N characters from str2 to Str1
    void myStrNCpy(char* str1, char* str2, int n)

    please help me

    this is the code of the first fucntion ...where is the error?
    Code:
    #include<stdio.h>
    #include"myString.h"
    int main (){
    char* str1;
    char* str2; 
    char result; 
    printf("Enter string1:\n"); 
    scanf("%s",str1); 
    printf("Enter string2:");
    scanf("%s",str2);
    result=myStrCmp(str1,str2); 
     if(result==1) 
      printf(" same"); 
     else 
       printf(" different"); 
     } 
    
    
    int myStrCmp(char* str1, char* str2){
    int i; 
    
    if (myStrLen(str1) - myStrLen(str2))
    
     return(1);
    
    for ( i = 0 ; i < myStrLen(str1) ; i++ ) { 
      if (*(str1+i) - *(str2+i)) 
      return(1); 
          } 
    
    return(0);
    }
    Last edited by Frinavale; Aug 31 '09, 08:31 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You can't use the standard library functions because?

    Comment

    • madeofdata
      New Member
      • Dec 2008
      • 13

      #3
      because I need to create my own function !!!

      Comment

      • madeofdata
        New Member
        • Dec 2008
        • 13

        #4
        Originally posted by Banfa
        You can't use the standard library functions because?
        I NEED TO CREATE MY OWN function

        Comment

        • newb16
          Contributor
          • Jul 2008
          • 687

          #5
          Originally posted by madeofdata
          if (myStrLen(str1) - myStrLen(str2))
          ....
          if (*(str1+i) - *(str2+i))
          Why not ( mystrlen(str1)= =mystrlen(str2) ) instead of subtraction?
          Why not str1[i] - str2[i] ?
          I don't see any errors, can you do it by just looking at the code?
          Oh wait you are scanf'ing your input to nowhere.

          Comment

          • madeofdata
            New Member
            • Dec 2008
            • 13

            #6
            I did not understand where is the error ?? (scanin nowhere ???

            Comment

            • madeofdata
              New Member
              • Dec 2008
              • 13

              #7
              what should I write in the main function ?

              Comment

              • newb16
                Contributor
                • Jul 2008
                • 687

                #8
                Originally posted by madeofdata
                what should I write in the main function ?
                google for scanf %s example of usage. Your str1 pointer is pointing to nowhere, when you call scanf, it takes string from input and puts in to nowhere, possibly causing error. As you are reluctant to specify what exactly your error is, this is advice.
                ps there is no need of braces () around return value.

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  Originally posted by madeofdata
                  I did not understand where is the error ?? (scanin nowhere ???
                  Look closely at your main function ... where you accept the input strings. The user types in the string "compare". That's eight characters including the terminal NUL. Where are those characters stored?

                  By the way, it would be immensely easier to help you if you told us what's going wrong with your code.

                  You don't tell us what myStrLen does, but I assume it traverses the string until it finds the NUL character. myStrCmp calls myStrLen three times, plus it traverses the string locally. That means myStrCmp traverses the string four times if the inputs match. That's pretty inefficient. Can you think of a way to restructure your code so the string is traversed only once?

                  Comment

                  • madeofdata
                    New Member
                    • Dec 2008
                    • 13

                    #10
                    I tried lot of things ; still it gives me an error message ...can someone tell me what I need to do exactly?

                    Comment

                    • vmpstr
                      New Member
                      • Nov 2008
                      • 63

                      #11
                      It would help if you post what messages you are getting...

                      Maybe you forgot to return 0 in main? Or maybe your version of strcmp return 1 when the strings are not equal, yet the if statement in your main implies that it returns 1 when they are equal?

                      (and, of course, you don't allocate any memory to your pointers, as mentioned above)...

                      Comment

                      • madeofdata
                        New Member
                        • Dec 2008
                        • 13

                        #12
                        how should I allocate memory to my pointer ?( it may seem dumy but I am a beginner so;;;;)

                        Comment

                        • madeofdata
                          New Member
                          • Dec 2008
                          • 13

                          #13
                          I know that IT is a runtime error ..since there is the compiler do not detect it ...and the message is '' that ;exe has encounter a problem and have to close .....)

                          Comment

                          • vmpstr
                            New Member
                            • Nov 2008
                            • 63

                            #14
                            If you are a beginner, use a character array (like char array[50] instead of char *array).

                            Comment

                            • madeofdata
                              New Member
                              • Dec 2008
                              • 13

                              #15
                              it does not work ..there still a runtime error !!!!!

                              Comment

                              Working...