Results 1 to 4 of 4

Thread: Create a function for comparing character strings

  1. #1
    Join Date
    Apr 2009
    Posts
    87

    Create a function for comparing character strings

    I want to create a function equivalent to the function strcmp available with # include <string.h>.
    Code:
    # include <stdio.h> 
      # include <stdlib.h> 
      # include <string.h> 
    
      char comparison (ch, cha); 
    
      int main (int argc, char * argv []) 
      ( 
      char string1 [10], string2 [10]; 
    
      printf ( "Enter two words in validating one after the other, below: \ n"); 
      scanf ( "% 10s% 10s", string1, string2); 
    
      printf ( "You took the words% s and% s \ n", string1, string2); 
    
      compare (string1, string2); 
    
    
      / / Function comparison 
     
      char comparison (ch, cha) 
      ( 
          if (ch == CHAINED) 
              ( 
              printf ( "comparing:% s and% s are identical \ n", ch, cha); 
              ) 
          else 
              ( 
              printf ( "comparing:% s and% s are different \ n", ch, cha); 
              ) 
      )
    Outside, if I compare two strings of similar nature, the program reports me as different. Can you tell me where is the mistake?

  2. #2
    Join Date
    Jan 2009
    Posts
    199

    Re: Create a function for comparing character strings

    Code:
    if (ch == CHAINED)
    You can not compare 2 strings like that! If you want to reproduce strcmp you have compare it, Suppose we have 2 character that have value as 'hello' and 'hello', In your comparison function you'll compare : s and b > Not equal you can say that the strings are different, no need to test other characters.

    If it was: 'hi' and salt 'comparison:
    • s and s -> Equal to continue
    • a and a -> Equal to continue
    • l and l -> Equal to continue
    • u and t -> Not equal it stops there

  3. #3
    Join Date
    Dec 2008
    Posts
    177

    Re: Create a function for comparing character strings

    To Create a function for comparing character strings follow the example given below:
    Code:
    char *s1, *s2;
    char* s = "Hello";
    s[1] = 'a';     
    s = new char[strlen("Hello") + 1]; 
    strcpy(s, "Hello");
    if(!strcmp(s1, s2))

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: Create a function for comparing character strings

    You have misunderstood the use of the loop. Here is another function based on function to returns 0 if identical, another value if different:
    Code:
     SCMP int (char * ch1, char * ch2) 
       ( 
            while ((* ch1 - ch2 *) & & (* ch1! = '\ 0')) 
            (Ch1 + +; ch2 + +;) 
    
            return (* ch1 - ch2 *); 
       )

Similar Threads

  1. Replies: 4
    Last Post: 01-03-2012, 08:38 PM
  2. Strings function in MySQL
    By Dino M in forum Software Development
    Replies: 6
    Last Post: 09-12-2010, 04:01 AM
  3. Replies: 4
    Last Post: 13-11-2010, 05:11 PM
  4. Read and compare character strings
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 27-01-2010, 01:30 PM
  5. Random Function to Concatenate 2 strings
    By klite in forum Software Development
    Replies: 3
    Last Post: 14-10-2009, 11:48 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,340,823.36326 seconds with 16 queries