Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 16-10-2009
Member
 
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?
Reply With Quote
  #2  
Old 16-10-2009
Member
 
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
Reply With Quote
  #3  
Old 16-10-2009
Member
 
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))
Reply With Quote
  #4  
Old 16-10-2009
Member
 
Join Date: May 2008
Posts: 2,293
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 *); 
   )
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Create a function for comparing character strings"
Thread Thread Starter Forum Replies Last Post
Can we ski in sky and create your own character in SSX game Hard.Kaur Video Games 4 01-03-2012 08:38 PM
Strings function in MySQL Dino M Software Development 6 09-12-2010 04:01 AM
Mailto function problem when subject contains % character in safari Bryn Technology & Internet 4 13-11-2010 05:11 PM
Read and compare character strings TechGate Software Development 5 27-01-2010 01:30 PM
Random Function to Concatenate 2 strings klite Software Development 3 14-10-2009 11:48 AM


All times are GMT +5.5. The time now is 10:45 AM.