answersLogoWhite

0


Best Answer

/* ellipses, ..., used to emulate indentation */

int strcmp (const char* s1, const char* s2) {

... while (s1 != NULL && s2 != NULL) { /* scan both strings */

... ... if (*s1 < *s2) return -1; /* first string less */

... ... if (*s1 > *s2) return 1; /* first string greater */

... ... s1++;

... ... s2++;

... }

... if (s1 == s2) return 0; /* strings equal or both empty */

... if (s1 != NULL) return 1 else return -1; /* first string longer(1) / shorter(-1) */

}

A slightly less computation-intensive version is:

int strcmp(const char* s1, const char* s2)

{

... if (s1==s2)

... ... return 0;

... while (*s1)

... ... if (*s1++^*s2++)

... ... ... if (--s1<--s2)

... ... ... ... return -1;

... ... ... else

... ... ... ... return 1;

... return 0;

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

#include <stdio.h>

/* my_strcmp has functionality equivalent to standard library strcmp */

int my_strcmp(const char *str1, const char *str2)

{ do {

char diff = *str1 - *str2;

if (diff != 0) return diff;

} while (*str1++ && *str2++);

return 0;

}

int main(void)

{

printf("%d\n", my_strcmp("ALA", "ALA"));

printf("%d\n", my_strcmp("AL", "ALA"));

printf("%d\n", my_strcmp("ALB", "ALA"));

return 0;

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How would you write a user defined function to compare two strings without using the library function?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Which operator is used to compare strings in Perl?

eq


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

Yes, it does.


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.


Can you tell me a C program to find if the strings are equal using pointers?

It is called strcmp, part of the standard run-time library. Returns 0 if the two strings are equals, non-zero otherwise.


What is the difference between Oracle data types char and varchar2?

Character string values storage:1. CHAR:&sect; Stores strings of fixed length.&sect; The length parameter s specifies the length of the strings.&sect; If the string has smaller length it padded with space at the end&sect; It will waste of a lot of disk space.&sect; If the string has bigger length it truncated to the scale number of the string.2. VARCHAR:&sect; Stores strings of variable length.&sect; The length parameter specifies the maximum length of the strings&sect; It stores up to 2000 bytes of characters&sect; It will occupy space for NULL values&sect; The total length for strings is defined when database was created.3. VARCHAR(2):&sect; Stores strings of variable length.&sect; The length parameter specifies the maximum length of the strings&sect; It stores up to 4000 bytes of characters&sect; It will not occupy space for NULL values&sect; The total length of strings is defined when strings are given

Related questions

'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 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.


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.


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


What is the the function of the heart strings?

to keep the heart open


What is the function chromatids?

Chromatids are the little strings of DNA inthe process called mitoisis


Write a program in C to concatenante 2 strings?

Here is very small application written in C, that concatenates strings.#include #include int main() {char str[100];strcpy(str, "Hello ");strcat(str, "World!");printf("%s\n", str);return 0;}The result is "Hello World!".Concatenation is done by function strcat which is defined in string.h.


Write a program to compare two strings with out using strcmp function?

#include main() { //please read two strings int str1 and str2// while(str1[i]!='/0' &amp;&amp;str2[i]!='/0') if(str1[i]!=str2[i]) flag=1; if(flag==1) printf("equal"); } #include main() { //please read two strings int str1 and str2// while(str1[i]!='/0' &amp;&amp;str2[i]!='/0') if(str1[i]!=str2[i]) flag=1; if(flag==1) printf("equal"); }


Is it possibly to return an array of strings in a function without using pointers in C plus plus?

No.