answersLogoWhite

0


Best Answer

In most programming languages you will be relying on pre-existing library functions for all your string comparison needs.

The algorithm is:

  • Consider both strings as arrays of characters (depending on the language, they will either have a specified maximum length, eg in Pascal or a terminating symbol - usually a zero - eg in C and C++)
  • Iterator n represents the position in the character string, with its' initial value representing the first index in the array. If the language permits it, you can use pointer arithmetic instead.
  • For each character in a string:
  1. Take the n-th character from string 1
  2. Take the n-th character from string 2
  3. Compare both characters (the exact comparison method may vary)
  4. If not equal, return false
  5. Increase n by 1
  • If reached end of string, return true

Here is an example function in C (returning a 1 if the strings are identical, 0 if not). This implementation forgoes use of an iterator in favor of pointer arithmetic and comparison in favor of XORing the characters with each other (a XOR of identical values returns a zero):

int string_compare(char* a, char* b)

{

while (*a)

{

if (*a++^*b++) return 0;

}

return 1;

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you compare strings?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you campaire Strings in java?

String class in Java has an 'equals' method that can be used to compare strings.


Which operator is used to compare strings in Perl?

eq


Does C allows you to use equality operators to compare strings?

Yes, it does.


How do electric bass strings compare to upright bass strings?

Well, for one, string bass strings are a lot longer than electric bass strings. I wouldn't recommend putting string bass strings on an electric bass and vice versa.


Write a script that accepts two strings from the user and compare two strings in web technology?

how to compare two strings that take input from the user and compare it. For example: i give first string as "THE" and give second string as "HTE" then return "match" if i give first as"THE" nd second string as "EHI" then return "NOtMatch" witout using STRCMP ... please help me


'c' code to compare two strings using strcmp function?

char one [] = "A string" ;char two [] = "Different String" ;if (strcmp (one, two) == 0){puts ("The two strings are identical") ;}else{puts ("The two strings are different") ;}


How do you compare a string puppet and a finger puppet?

Well, a finger puppet is a cloth or felt puppet put on your finger during plays. A string puppet is a puppet hung by strings. When the person pulls the strings, the puppet moves. There are some other ways you could compare because of quality, efficency etcetera, but this is the basics.


What are the advantages of a diaphragm spring clutch?

Diaphragm clutch is smaller specially on size compare to other spring clutch. It also transmits much more torque as diaphragm exerts more force as compare to other strings.


What is the use of strcmp function in c plus plus?

strcmp is used to compare two strings. If the return value is zero, the two strings are the same. If the return value is less than 0, then the first string is less than the second string, otherwise the first string is greater than the second string. Strings are compared lexicographically, character by character.


How many strings does veena have?

It has 7 strings 4 main strings and 3 thala strings


How many strings does the veena have?

It has 7 strings 4 main strings and 3 thala strings


What is the C plus plus program for string compare?

You don't need a program to compare strings since std::string already provides support for all the comparison operators (<, <=, >, >=, == and !=). To roll your own you must first create a string class and then provide operator overloads for the comparison operators. To compare strings, start at the first character in each string and compare. So long as they remain equal, move onto the next character. The comparison ends as soon as any character differs. You need only compare these two characters to decide which string is the lesser. To perform a case insensitive comparison, copy the two characters and convert the copies to lower case (or upper case, it doesn't matter). Then compare the copies. Do this for each character as you compare them rather than converting the entire string.