Thread: c equal string

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    17

    c equal string

    i made my own function to search if two given strins in my function are equal but the problem is if i pass two variable like hello,hello
    result is string equal but if i pass hello , gello also give me string equal because last 4 characters same to last 4 characters of hello how i can solve this problem thanks very much
    Code:
    int getSimilarityOfTwoStrings(const char str1[],const char str2[]){
        int str1Len = getStringLength(str1);
        int str2Len = getStringLength(str2);
        int i = 0;
        int j = 0;
        bool truefalse;
        if(str1Len > str2Len || str2Len > str1Len || str1Len < str2Len || str2Len < str1Len){
            truefalse = false;
            exit(EXIT_FAILURE);
        }else{
            for(i = 0; i < str1Len; i++){
                if(tolower(str1[i]) == tolower(str2[j++])){
                  truefalse = true;
                }else{
                  truefalse = false;
                }
            }
        }
    
    
        return truefalse;
    }

  2. #2
    Registered User
    Join Date
    Jul 2012
    Posts
    51
    You are overwriting your bool value truefalse. If your strings differ on the first character, why continue checking?

    Also, line 7 could be written with the "not equal to" operator in your function.

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    17
    ok i change my line to str1len != str2len or str2len != str1len and also i use break into if to stop overwriting in this way works . but my question when we use c function strcmp they check all words or also they check first character into string ? thanks very much

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by shivi_php View Post
    but my question when we use c function strcmp they check all words or also they check first character into string ? thanks very much
    strcmp has no concept of "words." it does a character-by-character comparison of the whole string, returning when it finds the first difference, or when the entire strings have been compared.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strlen does not equal string length
    By MK27 in forum C Programming
    Replies: 2
    Last Post: 10-09-2008, 02:59 PM
  2. std::string::operator== fails for equal strings?
    By pheres in forum C++ Programming
    Replies: 5
    Last Post: 03-25-2008, 11:21 AM
  3. compare if 2 string types are equal
    By sujeet1 in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2007, 06:37 PM
  4. How do I check if a cin string is equal to something?
    By XunTric in forum C++ Programming
    Replies: 2
    Last Post: 12-13-2005, 05:15 AM
  5. set string array equal variable
    By WaterNut in forum C++ Programming
    Replies: 3
    Last Post: 06-29-2004, 05:02 PM