answersLogoWhite

0


Best Answer

A string is a character array, so you can compare them one character at a time:

String x = "test"

String y = "test"

for(int i = 0; i < x.length && i < y.length; i++)

{

if(x[i] != y[i])

return false;

}

return true;

User Avatar

Wiki User

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

Wiki User

14y ago

char one [] = "first string" ;
char two [] = "second string" ;

if (strcmp (one, two) == 0)
{
puts ("The strings are the same") ;
}
else if (strcmp (one, two) < 0)
{
puts ("String one is less than string two") ;
}
else
{
puts ("String one is greater than string two") ;
}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

#include<stdio.h> int str_cmp(const char *s1, const char *s2) { unsigned int i = 0, diff; while(*(s1+i) && *(s2+i)) { diff = (*(s1+i)-*(s2+i)); if(!diff)i++; else break; } return diff; } int main() { printf("chuma %d ", str_cmp("abcd","abcde")); return 0; } U can use this as a prototype and enhance this. I havent even tried compilng this. Sujith

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

There are several comparison functions in C. If you read that section in your C library reference you will have no problems. The question is intended to make you study and understand comparison functions.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Make use of the fact that strings are null terminated character arrays in c and that the array variable is a reference to the starting address of the string.


#include

int stringComp( char str1[], char str2[]){

while( *str1 && *str2){//check for termination of any of the strings

if( *str1 < *str2)

return -1;

else if( *str1 > *str2)

return 1;

str1++;

str2++;

}

if( !(*str1) && !(*str2))//both the strings have terminated - checking for difference in length

return 0;

else if( !(*str1))//only str1 has terminated

return -1;

return 1;//only str2 has terminated

}


int main(){

char str1[20], str2[20];

int equal;

printf("Enter the two strings:\t");

scanf("%s%s", str1, str2);

equal = stringComp(str1, str2);

if( !equal)

printf("The two strings are equal\n");

else if( equal == -1)

printf("str1 is lesser than str2\n");

else

printf("str1 is greater than str2\n");

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

The stricmp() function in C language compares two strings with case insensitivity. Java provides equalsIgnoreCase() method in Strings class for the same function.

void stringTest() {

String s1 = "foo";

String s2 = "Foo";

if (s1.equalsIgnoreCase(s1, s2))

System.out.println("strings equal");

else

System.out.println("strings not equal");

}

Output for this method would be:

> strings equal

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

using built in function

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Use functions strlen and memcpy.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a c program to compare two strings without using builtin function?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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


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"); }


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.


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 (&lt;, &lt;=, &gt;, &gt;=, == 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.


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


Write a c program to accept alist of 10 namews input by user then display the sorted list of names back to the user?

, since you seem to be needing help with your homework, I can give you some psuedo code to help you on your way! [CODE] DECLARE ARRAY names[10] FOR i = 0 TO 9 names[i] = READ INPUT FROM USER ENDFOR CALL FUNCTION sort WITH PARAMETERS names, 10 [/CODE] In the event that you have to create your own sorting function, I can give you some pseudo-code for the selection sort - a relatively easy-to-write sorting algorithm which doesn't involve any complicated data structures, or more advanced programming patterns, such as recursion. [CODE] FUNCTION sort WITH PARAMETER ARRAY names[], size FOR i = 0 TO size min = i FOR j = i + 1 TO size //Find the smallest unsorted element in the list IF names[min] &gt; names[j] THEN min = j END FOR SWAP names[i] AND names[min] END FOR END FUNCTION [/CODE] The above function sorts in ascending order. The smallest element will be located at names[0], and the largest will be at names[size]. If you are allowed to, you may be able to use C's builtin strcmp() function to compare strings (found in #include &lt;string.h&gt;, or #include &lt;cstring&gt; for a C++ compiler). PLEASE REMEMBER that you can not compare strings directly in C!!! A C-string is nothing more than a memory address. If you attempt to compare two strings directly, then the compiler will NOT compare the strings, but the memory addresses where the strings are located! If you can't use strcmp(), you will have to write your own string comparing function. When reading input from the user, you will need to use scanf() for C. Also remember that C does not do any memory management for you, so you need to make sure you have enough space in your character arrays to read in a string from the user. Because you have multiple names, you will need to create an array of arrays. Something similar to this: "char names[NUMBER_OF_NAMES][CHARACTERS_IN_A_NAME];" where NUMBER_OF_NAMES = 10 and CHARACTERS_IN_A_NAME is an arbitrary number, large enough to store a name (I would pick something like 100).


How can you do the same thing as the program below but using strings and arrays in C language?

Program below?!


What is the Program for compare two strings using OR operator in java?

The String class includes two helpful methods: equals and compareTo.string1.equals(string2) will return true if the two strings contain the exact same charactersstring1.compareTo(string2) will return an int which describes the lexicographic relationship between the two strings. It will return a negative value if string1 is "less than" string2, a positive value if string1 is "greater than" string2, or zero if the two are equivalent strings.