How to optimise strcmp c-string comparison

This is a discussion on How to optimise strcmp c-string comparison within the C++ Programming forums, part of the General Programming Boards category; Hi guys I have written some code and it works fine, the only problem is I think is not that ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    10

    Cool How to optimise strcmp c-string comparison

    Hi guys I have written some code and it works fine, the only problem is I think is not that mobile, I mean it looks big for what it does. Basically I have three names in c-string and I like to arrange them alphabetically, but I am not allowed to use arrays.
    So the question is how to optimise my code? Thanks


    Code:
    #include<iostream>
    #include<cstring>
    
    using namespace std;
    
    int main( )
    {
    
    	char name1[16] = {'\0'},
    		name2[16] = {'\0'}, 
    		name3[16] = {'\0'};
    
    	cout <<"The program will display entered names in alphabetical order.\n";
    	cout <<"Enter three names pressing the Return key after each name\n";
    	cin >> name1 >> name2 >> name3;
    	cout <<"The alphabetical order of the name is:\n";
    	if(strcmp(name1, name2) < 0 && strcmp(name1, name3) < 0)        
    	{
    		if(strcmp(name2, name3) < 0)
    			cout << name1 << endl << name2 << endl <<name3 << endl;
    		else 
    			cout << name1 << endl << name3 << endl << name2 << endl;
    	}
    	else if(strcmp(name2, name1) < 0 && strcmp(name2, name3) < 0)
    	{
    		if(strcmp(name1, name3) < 0)
    			cout << name2 << endl << name1 << endl << name3 << endl;
    		else 
    			cout << name2 << endl << name3 << endl << name1 << endl;
    	}
    	else if(strcmp(name3, name1) < 0 && strcmp(name3, name2) < 0)
    	{
    		if(strcmp(name1, name2) < 0)
    			cout << name3 << endl << name1 << endl << name2 << endl;
    		else 
    			cout << name3 << endl << name2 << endl << name1 << endl;
    	}
    	else if(strcmp(name1, name2) == 0 && strcmp(name2, name3) == 0)
    	{
    		cout << name1 << endl << name2 << endl <<name3 << endl;
    	}
    	else if(strcmp(name1, name2) == 0)
    	{
    		if(strcmp(name2, name3) < 0)
    			cout << name1 << endl << name2 << endl <<name3 << endl;
    		else 
    			cout << name3 << endl << name1 << endl << name2 << endl;
    	}
    	else if(strcmp(name1, name3) == 0)
    	{
    		if(strcmp(name1, name2) < 0)
    			cout << name1 << endl << name3 << endl << name2 << endl;
    		else 
    			cout << name2 << endl << name1 << endl << name3 << endl;
    	}
    	else if(strcmp(name2, name3) == 0)
    	{
    		if(strcmp(name2, name1) < 0)
    			cout << name2 << endl << name3 << endl << name1 << endl;
    		else
    			cout << name1 << endl << name2 << endl <<name3 << endl;
    	}
    	return 0;
    }

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    So the question is how to optimise my code? Thanks
    Stop calling strcmp so much?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    10
    But then how I will do the comparison

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1) You could store the results.

    2) Why don't you just sort the names? You can do that in place using bubblesort.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    Save the results of some of the comparisons, and use their meaning to determine the order. The logic is only a little different.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Note: This is a stupid idea but I am bored. Don't do this.
    Code:
    int bits = 0;
    if (strcmp(name1, name2) < 0) bits++;
    bits <<= 1;
    if (strcmp(name1, name3) < 0) bits++;
    bits <<= 1;
    if (strcmp(name2, name3) < 0) bits++;
    
    switch(bits) {
        case 7:
            cout << name1 << endl << name2 << endl <<name3 << endl;
        break;
        case 6:
            cout << name1 << endl << name3 << endl <<name2 << endl;
        break;
        case 4:
            cout << name3 << endl << name1 << endl <<name2 << endl;
        break;
        case 3:
            cout << name2 << endl << name1 << endl <<name3 << endl;
        break;
        case 1:
            cout << name2 << endl << name3 << endl <<name1 << endl;
        break;
        case 0:
            cout << name3 << endl << name2 << endl <<name1 << endl;
        break;
    }
    No really, don't do this.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    10
    Hi tabstop, thanks for the replay, I am tiring to understand the algorithm you used, I see the incremation and how it changes if it meets the if statement , but can you explain with couple of words, specially if the names are Ana, Boni, Roni it increments to 7…?? Please explain… this is not the bubble sort algorithm is it?

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,705
    Quote Originally Posted by tabstop View Post
    Note: This is a stupid idea but I am bored. Don't do this.
    Code:
    int bits = 0;
    if (strcmp(name1, name2) < 0) bits++;
    bits <<= 1;
    if (strcmp(name1, name3) < 0) bits++;
    bits <<= 1;
    if (strcmp(name2, name3) < 0) bits++;
    
    switch(bits) {
        case 7:
            cout << name1 << endl << name2 << endl <<name3 << endl;
        break;
        case 6:
            cout << name1 << endl << name3 << endl <<name2 << endl;
        break;
        case 4:
            cout << name3 << endl << name1 << endl <<name2 << endl;
        break;
        case 3:
            cout << name2 << endl << name1 << endl <<name3 << endl;
        break;
        case 1:
            cout << name2 << endl << name3 << endl <<name1 << endl;
        break;
        case 0:
            cout << name3 << endl << name2 << endl <<name1 << endl;
        break;
    }
    No really, don't do this.
    Stupid, maybe..yet somehow ingenious.
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Svetlin View Post
    Hi tabstop, thanks for the replay, I am tiring to understand the algorithm you used, I see the incremation and how it changes if it meets the if statement , but can you explain with couple of words, specially if the names are Ana, Boni, Roni it increments to 7…?? Please explain… this is not the bubble sort algorithm is it?
    No, this is not bubble sort. If you want to follow along, you should look at what << does (hint: it's not just for output).

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,266
    Quote Originally Posted by Bubba View Post
    Stop calling strcmp so much?
    Quote Originally Posted by Svetlin View Post
    But then how I will do the comparison
    Use properties of strcmp(). With a bit of logic, it is possible to work out ways to call strcmp() less.

    For example (assuming a, b are valid strings) then it is guaranteed

    1) strcmp(a,b) will always return the same result, for a given pair of a and b.

    2) (strcmp(a,b) > 0) == (strcmp(b,a) < 0)

    There are other properties too if you want to work with three strings ..... tabstop actually used some of them
    Last edited by grumpy; 12-09-2010 at 07:36 PM.
    Right 98% of the time, and don't care about the other 3%.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    10
    Ok now I understand what you are doing tabstop, you are using binary positions to calculate the possible choices…

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    Why use strcmp in the first place? Why use C-style strings?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using strcmp to find if a string ends in...
    By frankchester in forum C Programming
    Replies: 10
    Last Post: 12-09-2010, 07:09 PM
  2. Polymorphism and generic lists
    By Shibby3 in forum C# Programming
    Replies: 9
    Last Post: 07-26-2010, 05:27 AM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21