Thread: Strcmp Question - C

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    16

    Strcmp Question - C

    Hello,

    I'm trying to quickly learn C as I have a program is write in about 2 weeks using it at uni. Its client/server stuff.

    To the point I can get this string compare code to work. The client is sending a value like "1" or "2" up to "10" but the server only responds with the text "One". Even if a input a letter like "h" or a number like "23" it responds with "One". I'm pretty sure the problems with the code bellow. Everything else seems fine.

    Code:
         if(strcmp(echoBuffer, "1"))
         {
             strcpy(echoBuffer, "One ");
         }
         else if(strcmp(echoBuffer, "2"))
         {
             strcpy(echoBuffer, "Two ");
         }
         else if(strcmp(echoBuffer, "3"))
         {
             strcpy(echoBuffer, "Three ");
         }
        else  if(strcmp(echoBuffer, "4"))
         {
             strcpy(echoBuffer, "Four ");
         }
         else if(strcmp(echoBuffer, "5"))
         {
             strcpy(echoBuffer, "Five ");
         }
         else if(strcmp(echoBuffer, "6"))
         {
             strcpy(echoBuffer, "Six ");
         }
         else if(strcmp(echoBuffer, "7"))
         {
             strcpy(echoBuffer, "Seven ");
         }
         else if(strcmp(echoBuffer, "8"))
         {
             strcpy(echoBuffer, "Eight ");
         }
         else if(strcmp(echoBuffer, "9"))
         {
             strcpy(echoBuffer, "Nine ");
         }
         else
         {
             strcpy(echoBuffer, "Ten ");
         }
    I tried some stuff like if(strcmp(echoBuffer, "1") == 0) but that would just give an answer of 2 etc..

    echoBuffer is just a number like 1, 2 10 depending on client input etc..

    Any ideas?

    Thanks

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Here's a crazy idea -- you don't even need strcmp. Evidently echoBuffer is a char*. Google "ASCII table" if you do not understand why to "-48".
    Code:
    int EB=echoBuffer[0]-48;
    switch (EB) {
    	case '1': if (echoBuffer[1]='0') strcpy(echoBuffer,"Ten");
    		  else strcpy(echoBuffer,"One"); break;
    	case '2': strcpy...;break;
    	case '4': strcpy...;break;
    
    and so on}
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    Google "ASCII table" if you do not understand why to "-48".
    Just make it "-'0'" instead.
    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

  4. #4
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    In C what you put inside the if brackets can be false (=0) or true (!=0) so you need to put a '!' in this way:
    Code:
    if (!strcmp(str1, str2));
    //or
    if (strcmp(str1, str2)==0);
    what you wrote is exactly the opposite, so if the string echoBuffer is different from the string "1" it will write "One ";
    If you are sure you are going to compare a prefixed number of characters, use tha strncmp(), that takes a parameter more which specifies the upper bound of the characters to be compared.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    16
    I've tried a load of stuff just now including switch statements and I still cant get anywhere. When using the code bellow I just get an output of "Ten" so its skipping over all the other if's. I was originally using == 0 on the statements but my tutor said that wasnt needed. I thought he was wrong..

    Code:
    check code bellow
    Thanks!
    Last edited by Dr.Zoidburg; 01-21-2009 at 06:10 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So what is in echoBuffer -- print it out and be sure.

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    16
    Still can't get it working.

    I edited out the rcode. I tried it commenting the if statements and stren out and whatever I input on the client be it 1, 2, 3 I get the same response so echoBuffer should be the same as the user input. An example of this is in the screenshot below.

    http://www.numyspace.co.uk/~unn_r008573/Screenshot.jpg = url

    What I'm trying to do is intercept echoBuffer string and turn its numerical value into textual, then change recvMsgSize accordingly as when echoBuffer is sent back, it has a different length in memory.


    The code below has rcode stuff removed.

    Code:
    #include <stdio.h>      /* for printf() and fprintf() */
    #include <sys/socket.h> /* for recv() and send() */
    #include <unistd.h>     /* for close() */
    #include <string.h>
    #define RCVBUFSIZE 32   /* Size of receive buffer */
    
    void DieWithError(char *errorMessage);  /* Error handling function */
    
    void HandleTCPClient(int clntSocket)
    {
        char echoBuffer[RCVBUFSIZE];        /* Buffer for echo string */
        int recvMsgSize;                    /* Size of received message */
        
        /* Receive message from client */
        if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0)
            DieWithError("300");
        
    
         if(strcmp(echoBuffer, "1") == 0) 
         {
             strcpy(echoBuffer, "One ");
         }
         else if(strcmp(echoBuffer, "2") == 0)
         {
             strcpy(echoBuffer, "Two ");
         }
         else if(strcmp(echoBuffer, "3") == 0)
         {
             strcpy(echoBuffer, "Three ");
         }
        else  if(strcmp(echoBuffer, "4") == 0)
         {
             strcpy(echoBuffer, "Four ");
         }
         else if(strcmp(echoBuffer, "5") == 0)
         {
             strcpy(echoBuffer, "Five ");
         }
         else if(strcmp(echoBuffer, "6") == 0)
         {
             strcpy(echoBuffer, "Six ");
         }
         else if(strcmp(echoBuffer, "7") == 0)
         {
             strcpy(echoBuffer, "Seven ");
         }
         else if(strcmp(echoBuffer, "8") == 0)
         {
             strcpy(echoBuffer, "Eight ");
         }
         else if(strcmp(echoBuffer, "9") == 0)
         {
             strcpy(echoBuffer, "Nine ");
         }
         else
         {
             strcpy(echoBuffer, "Ten ");
         }
    
        recvMsgSize = strlen(echoBuffer);
    
    
    
        /* Send received string and receive again until end of transmission */
        while (recvMsgSize > 0)      /* zero indicates end of transmission */
        {
            /* Echo message back to client */
            if (send(clntSocket, echoBuffer, recvMsgSize, 0) != recvMsgSize)
                DieWithError("send() failed");
    
            /* See if there is more data to receive */
            if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0)
                DieWithError("recv() failed");
        }
    
        close(clntSocket);    /* Close client socket */
    }
    Thanks for the replies.
    Last edited by Dr.Zoidburg; 01-21-2009 at 06:55 PM.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A first caveat: I don't know stink about client/server code.

    However:

    In C "1" is not the same as '1', and neither "1" nor '1', is the same as 1.

    "1" is a string of one letter, followed by an end of string char: '\0'. The end of string char appended to it, elevates the char (or chars), from just char(s), into a string that C will recognize.

    '1' is a single char, and what it's followed by, is irrelevant.

    1 is an integer, and should be separated from other char's or numbers, if it is not a part of it.

    So I have no idea why you're trying to do a string comparison with what you describe in your problem description, as a number.

    Treat a number as a number, a char as a char, and a string as a string, please.

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    16
    I got it working with strncmp. I have one more quick noob question though.

    I want the server to validate the user input so if there is more than one character in echoString the message "message too big" is displayed. I've got to code bellow to display a message if a letter is input however if a number greater to 9 such as 10, 21 it would display 1 or 2 respectively. Its just using the if statements to its textual value and ignoring the if statement to detect its size.

    echoBuffer is defined at 32 bits long. I'm 99% sure strlen will only pick up the length of characters entered by the user in echoBuffer[32] as if it isnt present in the send socket, only one character is sent instead of several.

    I've tried it as length == 1.

    Code:
    
        int length = strlen(echoBuffer);
        
        
        if(length != 1)
        {
    	    if(strncmp(echoBuffer, "1", 1) == 0) 
    	     {
    		 strcpy(echoBuffer, "One ");
    	     }
    	     else if(strncmp(echoBuffer, "2", 1) == 0)
    	     {
    		 strcpy(echoBuffer, "Two ");
    	     }
    	     else if(strncmp(echoBuffer, "3", 1) == 0)
    	     {
    		 strcpy(echoBuffer, "Three ");
    	     }
    	    else  if(strncmp(echoBuffer, "4", 1) == 0)
    	     {
    		 strcpy(echoBuffer, "Four ");
    	     }
    	     else if(strncmp(echoBuffer, "5", 1) == 0)
    	     {
    		 strcpy(echoBuffer, "Five ");
    	     }
    	     else if(strncmp(echoBuffer, "6", 1) == 0)
    	     {
    		 strcpy(echoBuffer, "Six ");
    	     }
    	     else if(strncmp(echoBuffer, "7", 1) == 0)
    	     {
    		 strcpy(echoBuffer, "Seven ");
    	     }
    	     else if(strncmp(echoBuffer, "8", 1) == 0)
    	     {
    		 strcpy(echoBuffer, "Eight ");
    	     }
    	     else if(strncmp(echoBuffer, "9", 1) == 0)
    	     {
    		 strcpy(echoBuffer, "Nine ");
    	     }
    	     else
    	     {
    		 strcpy(echoBuffer, "300 ");
    	     }
         }
         else
         {
    		 strcpy(echoBuffer, "Message too big");
         }
    
        recvMsgSize = strlen(echoBuffer);
    Thanks for any tips. I'm working from tcp/ip sockets and c/the c programming language books to do this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  3. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM