answersLogoWhite

0


Best Answer

The simplest answer is to XOR (eXclusive OR) each corresponding character. If the result is non-zero, you have a mismatch so you increment the count. Note that you must always loop through the shorter of the two strings. The difference in length is also a mismatch.

The following function shows the basic principal:

unsigned mismatch_count (const char* str1, const char* str2) {

/* some variables */

unsigned len, len1, len2, index, count;

/* determine length of each string */

len1=strlen (str1);

len2=strlen (str2);

/* store the shortest length */

len=len1<len2?len1:len2;

/* if the strings are different lengths, the difference is a mismatch */

count=(len==len1)?len2-len1:len1-len2;

/* XOR each corresponding character, incrementing the count when non-zero */

for (index=0; index<len; ++index) {

if ((str1[index]^str2[index])!=0) {

++count;

}

}

return count;

}

User Avatar

Wiki User

8y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you get mismatch count between two string c language?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you write a program to check the string of a given grammar?

#include&lt;stdio.h&gt; #include&lt;conio.h&gt; #include&lt;string.h&gt; void main() { char string[50]; int flag,count=o; clrscr(); printf("The grammar is: S-&gt;aS, S-&gt;Sb, S-&gt;ab\n"); printf("Enter the string to be checked:\n"); gets(string); if(string[0]=='a') { flag=0; for(count=1;string[count-1]!='\0';count++) { if(string[count=='b']) { flag=1; continue; } else if((flag==1)&amp;&amp;(string[count]=='a')) { printf("The string does not belong to the specified grammar"); break; } else if(string[count=='a']) continue; else if(flag==1)&amp;&amp;(string[count]='\0')) { printf("The string accepted"); break; } else { printf("String not accepted"); } getch():


C plus plus program to count digit in a string?

Use the following function to count the number of digits in a string. size_t count_digits (const std::string&amp; str) { size_t count = 0; for (std::string::const_iterator it=str.begin(); it!=str.end(); ++it) { const char&amp; c = *it; if (c&gt;='0' &amp;&amp; c&lt;='9'); ++count; } return count; }


How you count lentgh of string without using strlen?

Basically in C language string is NULL (0x00) byte ending char array. So in order to find out the length of the string you need to count all elements in array until you reach NULL. But that is what strlen does. There are two links with information about strlen implementation and null-terminated strings.


What is the program to find the number of each word in the string array?

#include&lt;iostream&gt; #include&lt;string&gt; using namespace std; int main() { int count=0; string b; string a[6]={"technical","school","technical","hawler","school","technical"}; for(int i=0;i&lt;6;i++) { b=a[i]; for(int j=i;j&lt;6;j++) { if(a[j]==b) count++; } cout&lt;&lt;a[i]&lt;&lt;" "&lt;&lt;count&lt;&lt;endl; count=0; } return 0; }


Count characters in an input string using c sharp?

Console.WriteLine("Please input a string:"); string str = Console.ReadLine(); Console.WriteLine("Number of characters: " + str.Length);

Related questions

How do you write a program to check the string of a given grammar?

#include&lt;stdio.h&gt; #include&lt;conio.h&gt; #include&lt;string.h&gt; void main() { char string[50]; int flag,count=o; clrscr(); printf("The grammar is: S-&gt;aS, S-&gt;Sb, S-&gt;ab\n"); printf("Enter the string to be checked:\n"); gets(string); if(string[0]=='a') { flag=0; for(count=1;string[count-1]!='\0';count++) { if(string[count=='b']) { flag=1; continue; } else if((flag==1)&amp;&amp;(string[count]=='a')) { printf("The string does not belong to the specified grammar"); break; } else if(string[count=='a']) continue; else if(flag==1)&amp;&amp;(string[count]='\0')) { printf("The string accepted"); break; } else { printf("String not accepted"); } getch():


How would you implement a substr function that extracts a sub string from a given string?

substr(string, position [, count]) It extract substring starting from start and going for count characters. If count is not specified, the string is clipped from the start till the end


C plus plus program to count digit in a string?

Use the following function to count the number of digits in a string. size_t count_digits (const std::string&amp; str) { size_t count = 0; for (std::string::const_iterator it=str.begin(); it!=str.end(); ++it) { const char&amp; c = *it; if (c&gt;='0' &amp;&amp; c&lt;='9'); ++count; } return count; }


How you count lentgh of string without using strlen?

Basically in C language string is NULL (0x00) byte ending char array. So in order to find out the length of the string you need to count all elements in array until you reach NULL. But that is what strlen does. There are two links with information about strlen implementation and null-terminated strings.


How do you find a vowel in given string?

#include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; void main() { char string[]="anystring"; int i=0,j,count=0; j=strlen(string); while(i!=j) { if(string[i]=='a'string[i]=='e'string[i]=='o'string[i]=='u'string[i]='i') count++; i++; } }


What is the program to find the number of each word in the string array?

#include&lt;iostream&gt; #include&lt;string&gt; using namespace std; int main() { int count=0; string b; string a[6]={"technical","school","technical","hawler","school","technical"}; for(int i=0;i&lt;6;i++) { b=a[i]; for(int j=i;j&lt;6;j++) { if(a[j]==b) count++; } cout&lt;&lt;a[i]&lt;&lt;" "&lt;&lt;count&lt;&lt;endl; count=0; } return 0; }


Count characters in an input string using c sharp?

Console.WriteLine("Please input a string:"); string str = Console.ReadLine(); Console.WriteLine("Number of characters: " + str.Length);


Character count java?

Assuming you want to count the number of characters in a String, you just need to call the length() method on the object. For example: String foo = "This is a string"; System.out.println(foo.length()); The above code would result in the number 16 being printed out.


Do you use a singular verb for string as an uncountable nouns?

The non-count noun 'string' takes a singular verb form; for example:The string is tied too tight.The string is in the top drawer.


How do you count words in a paragraph using string?

use Microsoft word


How many words in the Lingala language?

There are approximately 10,000 words in the Lingala language.


What is count in c language?

count is a function that counts the variable name.