Thread: strcmp() \ flushing output

  1. #1
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472

    strcmp() \ flushing output

    Platform : WindowsXP
    Compiler : VC++ 6


    Question 1 :

    Here's a program using strcmp() :

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    	char s1[50], s2[50];
    
    	printf("s1 : ");
    	fgets(s1, sizeof(s1), stdin);
    
    	printf("s2 : ");
    	fgets(s2, sizeof(s2), stdin);
    
    	
    	if(strcmp(s1, s2) == 0)
    		printf("s1 == s2\n");
    	else if(strcmp(s1, s2) < 0)
    		printf("s1 is less than s2\n");
    	else if(strcmp(s1, s2) > 0)
    		printf("s1 is greater than s2\n");
    
    
    	return 0;
    }
    I already know what strcmp() does , but my question is how does it actually compare the two strings? if you enter "aa" as s1 and "AAAAAAAAA" as s2 (for example) ; strcmp() would return '+1' wich means s1 is greater than s2



    Question 2 :

    I never had to flush the output buffer in my programs before, because they seemed to run well and updating ouput as they should. But some programmers told me that some systems won't update the output untill it recieves a new line (\n) or when the output is manually flushed with fflush(stdout), Is this true? and if so , is this the only reason that programmers need to flush output?



    Thanks in advance
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Question #1: Been answered many times on the board. A simple method is to compare their values one position at a time. Once you find one that is different you return the value of subtraction
    Question #2: If you are running a processor intensive appication output could easily be skipped over until it was done with the processing. So in that case you really need to tell it to flush the buffer otherwise you might not get any output.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    1) strcmp doesn't care about the length of the string, if that's what you're asking. it compares the strings until it encounters unmatching characters (or the null-terminator of one of the strings). strcmp() considers 'a' to be greater than 'A'. the function is used often for sorting strings of course.

    2) flushing output buffers isn't always necessary, but it never hurts, either (especially with files).

    [edit]
    beat me to it (I actually like your explanation better anyway )
    [/edit]
    Last edited by Sebastiani; 01-16-2005 at 08:07 PM.
    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
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    so strcmp() uses the ASCII\EBCDIC table and compare characters by their value, right?
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Not really. He's a simple one:
    Code:
    int strcmp(const char *s1, const char *s2)
    {
      while (*s1 && *s2)
        if ( *s1 != *s2 )
          break;
        else
        {
          s1++;
          s2++;
        }
      return *s1 - *s2;
    }
    Of course there are versions that do it in fewer lines and faster but this should show you how its done
    Last edited by Thantos; 01-16-2005 at 11:01 PM. Reason: Fixed code in [color=red]red[/color]

  6. #6
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    I'm afraid that your strcmp() shows 'aaa' less than 'AAAAAA' and returns -65 .

    I tried to make my own and here's what i got :

    Code:
    int mystrcmp(const char *s1, const char *s2)
    {
      for( ; *s1 != '\0' && *s2 != '\0'; s1++,s2++)
      {
    	  if(*s1 > *s2)
    		  return 1;
    	  else if(*s1 < *s2)
    		  return -1;
      }
    
      return 0;
    
    }
    It works exactly like strcmp() (i mean produce the same results) , but i don't know if my comparsions are valid (the > and < between characters) , are they?
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Sorry I had a typo:
    Code:
    if ( *s2 != *s2 )
    should be
    Code:
    if ( *s1 != *s2 )
    Sorry about that

    It works exactly like strcmp() (i mean produce the same results)
    I doubt the results are exactly the same. But of course the exact value does not matter. Only if its above, below, or equal to 0

  8. #8
    .
    Join Date
    Nov 2003
    Posts
    307
    The equal to, less than, greater than convention is used as "input" by other library functions like qsort() and bsearch().

    The magnitude of a non-zero result is not important, the sign is what is important.
    This is a lot less efficient code but satisfies the requirements for strcmp() all the same:
    Code:
    int strcmp(const char *s1, const char *s2)
    {
      while (*s1 && *s2)
      {
        if ( *s1 != *s2 )
          break;
        else
        {
          s1++;
          s2++;
        }
      }
      if(*s1>*s2) return 1;
      if(*s1<*s2) return (-1);
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  2. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  3. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM