Program to compare two strings without using strcmp() function.

Posted by Unknown Jumat, 10 Januari 2014 0 komentar
Friends I'm sharing a program where you can compare two strings without using strcmp() function. we all know that strcmp() function compares two strings. strcmp is declared in stdio.h

Case 1: when the strings are equal, it returns zero.


Case 2: when the strings are unequal, it returns the difference between ASCII values of the characters that differ.


a) When string1 is greater than string2, it returns positive value.


b) When string1 is lesser than string2, it returns negative value.


Syntax:
int strcmp (const char *s1, const char *s2);



#include
#include
int cmpstr(cha r s1[10], char s2[10]);
int main(){
char arr1[10] ="Nodalo";
char arr2[10] ="nodalo";
printf("%d", cmpstr(arr 1, arr2));
// cmpstr() is equivalent of strcmp()
return 0;
}/
/s1, s2 are strings to be compared
int cmpstr(cha r s1[10], char s2[10]){
//strlen function returns the length of argument string passed
int i = strlen(s1) ;
int k = strlen(s2) ;
int bigger;
if (i<k){
bigger = k;
}
else if (i>k){
bigger = i;
}
else{
bigger = i;
}
//loops'bigger'times
for (i = 0; i<bigger; i++){
// if ascii values of characters s1[i], s2[i] are equal do nothing
if (s1[i] == s2[i]){
}
//else return the ascii difference
else{
return (s1[i] - s2[i]);
}
}
//return 0 when both strings are same
//This statement is executed only when both strings are equal
return (0);
}
 
Output:
-32

NOTE:
cmpstr() is a function that illustrate s C standard function strcmp(). Strings to be compared are sent as arguments to cmpstr().


Each character in string1 is compared to its corresponding character in string2. Once the loop encounters a
differing character in the strings, it would return the ASCII difference of the differing characters and exit.
TERIMA KASIH ATAS KUNJUNGAN SAUDARA
Judul: Program to compare two strings without using strcmp() function.
Ditulis oleh Unknown
Rating Blog 5 dari 5
Semoga artikel ini bermanfaat bagi saudara. Jika ingin mengutip, baik itu sebagian atau keseluruhan dari isi artikel ini harap menyertakan link dofollow ke https://androidjones7.blogspot.com/2014/01/program-to-compare-two-strings-without.html. Terima kasih sudah singgah membaca artikel ini.

0 komentar:

Posting Komentar

Trik SEO Terbaru support Online Shop Baju Wanita - Original design by Bamz | Copyright of android jones.