#include
main()
{
//please read two strings int str1 and str2//
while(str1[i]!='/0' &&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' &&str2[i]!='/0')
if(str1[i]!=str2[i])
flag=1;
if(flag==1)
printf("equal");
}
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") ;}
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.
It is called strcmp, part of the standard run-time library. Returns 0 if the two strings are equals, non-zero otherwise.
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
"strcmp" accepts two strings and compares them, returning a positive or negative number representing the difference between their first differing characters. for example: strcmp('abc', 'abc'); // returns 0 strcmp('abc', 'abd'); // returns -1 strcmp('abc', 'abb'); // returns 1 strcmp('abc', 'tnthdaou'); // returns -1
int string_equal (char *p1,char *p2) { int status = 1; while ((*p1 *p2) && status==1) { if (*p1++ != *p2++) status = 0; } return status; }
strcmp
Bubble sort will be able to sort the items lexicographically, if you first assign the strings a relative value. The way strcmp in the C library does this is by returning the difference of the first different characters between the strings. For instance, if you call strcmp("Hello", "Highway"), strcmp will return 'e'-'i'. The ascii value of e on a unix system is 101 and the ascii value of i is 105, so it is returning the value 101-105 = -4 which will tell you that "Hello" is lexicographically smaller than "Highway". Doing this, you should be able to use any algorithm you want to sort the strings.
, 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] > 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 <string.h>, or #include <cstring> 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).
Comparison of str1 with str2 (str1 == str2) always returns false, because the addresses of the two strings are different - you are comparing addresses, not contents. (str1 and str2 are pointers) To compare the contents, use strcmp(str1, str2) or one of its equivalent library functions.Note:strcmp will return 0 as well, so use like this:if (strcmp (str1, str2)==0) puts ("They are equals");
/* 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; }
#include <stdio.h> #include <string.h> void main() { char a[4][25],temp[25]; int i,j; clrscr(); printf("Enter the names\n"); for (i=0;i<4;i++) gets(a[i]); for (i=0;i<3;i++) for (j=i+1;j<4;j++) { if (strcmp(a[i],a[j])>0) { strcpy(temp,a[i]); strcpy(a[i],a[j]); strcpy(a[j],temp); } } printf("Sorted strings are \n"); for (i=0;i<4;i++) puts (a[i]); getch(); }