top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Find if two strings only differ by n characters; Return True if it does, false if it doesn't?

–2 votes
365 views
Find if two strings only differ by n characters; Return True if it does, false if it doesn't?
posted Mar 1, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Please provide more detail

1 Answer

0 votes

Its simple iterative procedure see code as follows (tested one)

#include <stdio.h>
#include <string.h>    

int countdiffs(char* w1, char* w2, int diffs) {

   char *lw, *sw;
   int i, curdiffs = 0;

   if ((NULL == w1) || (NULL == w2)) {
     return 0; 
   }

   if (strlen(w2) > strlen(w1)) {
      lw = w2; 
      sw = w1; 
   } else {
     lw = w1; 
     sw = w2;
   }
   curdiffs += strlen(lw) - strlen(sw);
   if (curdiffs > diffs) {
     return 0;
   }

   for (i=0; i<strlen(sw); i++) {
     if (sw[i] != lw[i]) {
        curdiffs++;
        if (curdiffs > diffs) {
          return 0;
        }
     }
   }
   if (curdiffs == diffs) {
      return 1; 
   }
   return 0;
}


int main() {
  char *word1="QueryHome";
  char *word2="MueryHome";
  int diffs = 1; 
  int answer;

  answer = countdiffs(word1, word2, diffs);
  printf("Answer:%d\n", answer);


  answer = countdiffs("QueryHome", "MurryHome", 1);
  printf("Answer:%d\n", answer);

  return 0;
}
answer Mar 2, 2016 by Salil Agrawal
Similar Questions
+2 votes

Write a C program which accept two strings and print characters in second string which are not present in first string?

Example:
String 1: apple
String 2: aeroplane

output:
ron

+4 votes

Given a dictionary of strings and another string find out if the string is an exact match to some words in the dictionary or varies at most in only one place of some word of the dictionary?

+6 votes

Take input 'n' as number of strings and find the common alphabet among them.
For example:
Input:

Number of Strings - 3
List of Strings - 
abcdde
baccd
eeabg

output:

2   

as 'a' and 'b' are only two common alphabet among those three strings

+1 vote

Given a string and dictionary of words, form a word by removing minimum number of characters.
Characters can be removed in-order only.

...