Thread: strcomp of 3 strings

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    strcomp of 3 strings

    i want to find the smallest index of 3 string indexes
    if i use strcomp then ill have to do 6 srcomps ?? its very complicated
    is there any simpler way

    and another problem is that when strcomp will compare these

    000321
    001230

    it will say that 000321 is bigger

    what to do??

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    i want to find the smallest index of 3 string indexes
    if i use strcomp then ill have to do 6 srcomps ?? its very complicated
    You only need three comparisons. (EDIT: oh wait, I think you only need two comparisons.)

    Quote Originally Posted by transgalactic2
    and another problem is that when strcomp will compare these

    000321
    001230

    it will say that 000321 is bigger
    I don't know what is strcomp, but strcmp("000321", "001230") will return a negative integer, meaning that "000321" is "less than" "001230".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    It's 'strcmp'. And it should work, since '000321' is lexicographically 'smaller' than '001230'. But then, why are you using a string-comparison function on numbers in the first place?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_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;
    }

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    strcmp returns the bigger lexicographic value
    in our case 00321 is bigger

    ??
    Last edited by transgalactic2; 03-27-2009 at 09:34 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    strcomp returns the bigger lexicographic value
    Right, so this is a non-standard function. Provide the function prototype and state exactly what are its pre-conditions and post-conditions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by Sebastiani View Post
    It's 'strcmp'. And it should work, since '000321' is lexicographically 'smaller' than '001230'. But then, why are you using a string-comparison function on numbers in the first place?
    sorry i ment strcmp.
    000321 is bigger then 001230

    its like in the dictionary
    cba will come after abcaaaaa
    correct?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    sorry i ment strcmp.
    000321 is bigger then 001230

    its like in the dictionary
    cba will come after abc
    correct?
    but aaabcd comes before aabcde - the zero's count here!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The third digit of the first string is '0' - in the second string it's '1'. Which do you think would be greater?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    its like in the dictionary
    cba will come after abc
    correct?
    Yes, but the correct analogy is between aaadcb and aabcda. In the dictionary, aaadcb will come before aabcda. If the leading zeroes do not count, then perhaps you should be comparing integers instead of strings, or at least remove the leading zeroes first.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    the zeros do count you are correct
    000321 is smaller then 001230
    so if i have 3 string how to find their lexicographic order using only two checks
    instead of 3!=6 checks??

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It's a simple case of tracking the smallest (or largest, if you prefer - if this is for your "reading three files", keeping track of the largest is probably a bit better - since you want to move forward in all files that aren't the largest).
    Code:
    smallest = a;
    if (smallest > b) smallest = b;
    if (smallest > c) smallest = c;
    I wrote that before I thought of the "largest is better" point - but the principle is the same, just different names and change the < for > .

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Quote Originally Posted by transgalactic2 View Post
    i want to find the smallest index of 3 string indexes
    if i use strcomp then ill have to do 6 srcomps ?? its very complicated
    is there any simpler way

    and another problem is that when strcomp will compare these

    000321
    001230

    it will say that 000321 is bigger

    what to do??
    so you have 3 numbers like 000321...as string? and you want to find smallest of the 3? is that it?

    if thats your question, then why not use atoi() function?

    Code:
    #include<stdio.h>
    
    int main()
    {
        char a[7]="000321";
        char b[7]="001230";
        int n1,n2;
        n1=atoi(a);
        n2=atoi(b);
        printf("%d,%d",n1,n2);
    }
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by creeping death View Post
    so you have 3 numbers like 000321...as string? and you want to find smallest of the 3? is that it?

    if thats your question, then why not use atoi() function?

    Code:
    #include<stdio.h>
    
    int main()
    {
        char a[7]="000321";
        char b[7]="001230";
        int n1,n2;
        n1=atoi(a);
        n2=atoi(b);
        printf("%d,%d",n1,n2);
    }
    But there is no need for that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i tried to find the smallest using these two operations
    but still those "if" combination are too many
    how to shorten the process of finding the smallest??
    Code:
    if(strcmp(f1,f2)>0)
    	 {
            if(strcmp(f2,f3)>0)
    		{
                //smallest is f3
    		}
    	 }
    i dont have such option as minimum in strcmp there is only
    >0 or <0

    and it depends on many cases
    so i have 6 if cases
    how to shorten it?

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Think about using else.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM